import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const projectRoot = path.resolve(__dirname, '..'); const FILE_CONFIGS = [ { input: path.join(projectRoot, 'IVYZ3P9M_学历信息查询(实时版)_返回字段说明.md'), output: path.join(projectRoot, 'src', 'data', 'ivyz3p9m-dictionary.json'), handler: parseIvyz3p9mDictionary, }, { input: path.join(projectRoot, '多头借贷小时级_返回字段说明.md'), output: path.join(projectRoot, 'src', 'data', 'multiLoanHourlyDictionary.json'), handler: parseMultiLoanDictionary, }, ]; function parseMarkdownTable(sectionContent, predicate = () => true) { const rows = []; sectionContent.split('\n').forEach((line) => { if (!line.trim().startsWith('|')) return; const cells = line .split('|') .slice(1, -1) .map((cell) => cell.trim()); if (cells.length === 0) return; if (cells.some((cell) => /^-+$/.test(cell))) return; if (predicate(cells)) { rows.push(cells); } }); return rows; } function parseIvyz3p9mDictionary(markdown) { const result = { educationLevel: {}, learningForm: {}, specialties: {}, schools: {}, }; const sections = markdown.split(/\n(?=## )/); sections.forEach((rawSection) => { const headerMatch = rawSection.match(/^##\s*(.+)\n/); if (!headerMatch) return; const title = headerMatch[1].trim(); const body = rawSection.slice(headerMatch[0].length); const isEducation = /学历层次/.test(title); const isLearning = /学习形式/.test(title); const isSchool = /学校/.test(title); const isSpecialty = /专业/.test(title); if (!(isEducation || isLearning || isSchool || isSpecialty)) { return; } const rows = parseMarkdownTable(body, (cells) => cells.length >= 2); rows.forEach((cells) => { const code = cells[0]; const name = cells[1]; if (!code || !name) return; if (isEducation) { result.educationLevel[code] = name; } else if (isLearning) { result.learningForm[code] = name; } else if (isSchool) { result.schools[code] = name; } else if (isSpecialty) { result.specialties[code] = name; } }); }); return result; } function parseMultiLoanDictionary(markdown) { const dictionary = {}; let headerCells = []; markdown.split('\n').forEach((line) => { if (!line.trim().startsWith('|')) return; const cells = line .split('|') .slice(1, -1) .map((cell) => cell.trim()); if (cells.some((cell) => /^-+$/.test(cell))) return; if (cells.length < 2) return; if (cells.includes('标签代码')) { headerCells = cells; return; } if (headerCells.length === 0) return; const data = Object.fromEntries(headerCells.map((header, index) => [header, cells[index] || ''])); const code = data['标签代码']; if (!code) return; dictionary[code] = { productName: data['产品名称'] || '', labelCategory: data['标签类别'] || '', description: data['业务含义'] || '', dataType: data['类型'] || '', length: data['长度'] || '', }; }); return dictionary; } function ensureDirectoryExists(filePath) { const dir = path.dirname(filePath); fs.mkdirSync(dir, { recursive: true }); } function run() { FILE_CONFIGS.forEach(({ input, output, handler }) => { if (!fs.existsSync(input)) { console.warn(`Input file not found: ${input}`); return; } const markdown = fs.readFileSync(input, 'utf-8'); const data = handler(markdown); ensureDirectoryExists(output); fs.writeFileSync(output, JSON.stringify(data, null, 2), 'utf-8'); console.log(`Generated dictionary: ${path.relative(projectRoot, output)}`); }); } run();