diff --git a/lib/extension/marked/pageforge-toc.js b/lib/extension/marked/pageforge-toc.js index 6901582..143a233 100644 --- a/lib/extension/marked/pageforge-toc.js +++ b/lib/extension/marked/pageforge-toc.js @@ -1,9 +1,15 @@ -const {marked} = require('marked'); -const {pinyin} = require('pinyin'); +const marked = require('./pageforge-marked'); function formatTOC(markdown) { + // 创建一个新的 marked 解析器实例 + const parser = new marked.Parser(); + const tokens = marked.lexer(markdown); - const headings = tokens.filter(token => token.type === 'heading'); + const headings = tokens.filter(token => token.type === 'pageforgeHeading') + .map(token => ({ + ...token, + text: parser.parseInline(token.tokens) // 使用解析器实例来处理 + })); function buildTree(headings) { const result = []; @@ -11,24 +17,15 @@ function formatTOC(markdown) { headings.forEach(heading => { const node = { - level: heading.depth, - text: heading.text, - slug: pinyin(heading.text.replace(/\s+/g, ''), { - style: pinyin.STYLE_NORMAL, - segment: true - }).flat().join('-'), + ...heading, children: [] }; - // 找到合适的父节点 - while (stack[stack.length - 1].level >= heading.depth) { + while (stack[stack.length - 1].level >= heading.level) { stack.pop(); } - // 添加到父节点的children中 stack[stack.length - 1].children.push(node); - - // 将当前节点加入stack stack.push(node); }); diff --git a/templates/components/p.js b/templates/components/p.js index 8e566a6..dc8a0e2 100644 --- a/templates/components/p.js +++ b/templates/components/p.js @@ -1,6 +1,6 @@ module.exports = function h1(item) { return ` -
+
${item.text}
`; diff --git a/templates/includes/toc.ejs b/templates/includes/toc.ejs index 2473800..c0170cc 100644 --- a/templates/includes/toc.ejs +++ b/templates/includes/toc.ejs @@ -8,7 +8,7 @@ - <%= item.text %> + <%- item.text %> <% if (item.children && item.children.length > 0) { %>