Files

52 lines
1.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const md = require('markdown-it')({ html: false, breaks: false, linkify: false });
const TOC_GROUPS = [
{ group: '快速开始', nums: [1, 2] },
{ group: '业务操作', nums: [3, 4, 5, 6, 7, 8] },
{ group: '设置与答疑', nums: [9, 10, 11] },
];
module.exports = function() {
const raw = fs.readFileSync(path.join(__dirname, '../content/docs.md'), 'utf8');
const lines = raw.split('\n');
// Split into chapters by "## {n}. {title}" headings
const chapters = [];
let current = null;
for (const line of lines) {
const match = line.match(/^## (\d+)\. (.+)/);
if (match) {
if (current) chapters.push(current);
const num = parseInt(match[1], 10);
current = { num, id: 'ch-' + num, title: match[2].trim(), bodyLines: [] };
} else if (current) {
current.bodyLines.push(line);
}
}
if (current) chapters.push(current);
// Render each chapter's body to HTML
const rendered = chapters.map(ch => ({
num: ch.num,
id: ch.id,
title: ch.title,
html: md.render(ch.bodyLines.join('\n')),
}));
// Build TOC
const chapterMap = {};
rendered.forEach(ch => { chapterMap[ch.num] = ch; });
const toc = TOC_GROUPS.map(g => ({
group: g.group,
items: g.nums
.filter(n => chapterMap[n])
.map(n => ({ id: chapterMap[n].id, num: n, title: chapterMap[n].title })),
}));
return { toc, chapters: rendered };
};