代码拉取完成,页面将自动刷新
const fs = require('fs');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
// 扫描 Markdown 文件并生成目录
function generateTableOfContents(filePath) {
// 读取 Markdown 文件内容
const markdown = fs.readFileSync(filePath, 'utf8');
// 创建 Markdown 解析器实例,并添加 markdown-it-anchor 插件
const md = markdownIt().use(markdownItAnchor);
// 解析 Markdown 文件内容
const tokens = md.parse(markdown, {});
// 存储目录项的数组
const toc = [];
// 遍历解析后的 tokens
for (const token of tokens) {
if (token.type === 'heading_open' && token.tag.startsWith('h')) {
// 提取标题级别、标题文本和锚点链接
const level = parseInt(token.tag.slice(1));
const text = tokens[tokens.indexOf(token) + 1].content;
const anchorLink = tokens[tokens.indexOf(token)].attrs.find(attr => attr[0] === 'id')[1];
// 将目录项添加到数组
toc.push({ level, text, anchorLink });
}
}
// 打印目录
for (const item of toc) {
console.log(`${' '.repeat(item.level - 1)}- [${item.text}](#${item.anchorLink})`);
}
}
// 指定要扫描的 Markdown 文件路径
const filePath = './README.md';
// 调用方法生成目录
generateTableOfContents(filePath);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。