Files
flutter_learning/scripts/extract-questions.js
T
2026-08-04 18:07:40 +08:00

32 lines
1.1 KiB
JavaScript

const fs = require('fs');
const vm = require('vm');
const html = fs.readFileSync('index.html', 'utf8');
const start = html.indexOf('const questions');
const arrStart = html.indexOf('[', start);
let depth = 0, inStr = false, esc = false, end = -1;
for (let i = arrStart; i < html.length; i++) {
const c = html[i];
if (inStr) {
if (esc) { esc = false; continue; }
if (c === '\\') { esc = true; continue; }
if (c === "'") inStr = false;
continue;
}
if (c === "'") { inStr = true; continue; }
if (c === '[') depth++;
else if (c === ']') { depth--; if (depth === 0) { end = i; break; } }
}
let src = html.slice(arrStart, end + 1);
src = src.replace(/\/\/\s*=====[^']*?=====\s*/g, ' ');
const questions = vm.runInNewContext(src);
const withIds = questions.map((q, i) => ({ id: i + 1, ...q }));
const data = {
sets: [{ id: 1, name: 'Dart & Flutter 学习测验', questions: withIds }],
nextSetId: 2,
nextQId: withIds.length + 1
};
fs.writeFileSync('questions.json', JSON.stringify(data, null, 2), 'utf8');
console.log('已提取', withIds.length, '道题 -> questions.json');