Skip to content

Commit

Permalink
chore: remove redundant TOC generation (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Aug 9, 2024
1 parent 0780b50 commit 9480dc5
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
9 changes: 0 additions & 9 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,6 @@ ${this.documentation.renderLinksInText(member.discouraged)}

generateDocFromMd(nodes, outName) {
this.documentation.renderLinksInNodes(nodes);
const tocIndex = nodes.findIndex(node => node.text === '<!-- TOC -->' || node.text === '<!-- TOC3 -->');
if (tocIndex !== -1) {
const node = nodes[tocIndex];
if (node.text === '<!-- TOC -->')
node.text = md.generateToc(nodes);
if (node.text === '<!-- TOC3 -->')
node.text = md.generateToc(nodes, true);
}

nodes = this.formatComment(nodes);
md.visitAll(nodes, node => {
if (node.children)
Expand Down
20 changes: 1 addition & 19 deletions src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,6 @@ function visit(node, visitor, depth = 0) {
visit(n, visitor, depth + 1);
}

/**
* @param {MarkdownNode[]} nodes
* @param {boolean=} h3
* @returns {string}
*/
function generateToc(nodes, h3) {
const result = [];
visitAll(nodes, (node, depth) => {
if (node.type === 'h1' || node.type === 'h2' || (h3 && node.type === 'h3')) {
let link = node.text.toLowerCase();
link = link.replace(/[ ]+/g, '-');
link = link.replace(/[^\w-_]/g, '');
result.push(`${' '.repeat(depth * 2)}- [${node.text}](#${link})`);
}
});
return result.join('\n');
}

/**
* @param {MarkdownNode[]} nodes
* @param {string} language
Expand Down Expand Up @@ -509,4 +491,4 @@ function filterNodesForLanguage(nodes, language) {
return result;
}

module.exports = { parse, render, clone, visitAll, visit, generateToc, filterNodesForLanguage, wrapText };
module.exports = { parse, render, clone, visitAll, visit, filterNodesForLanguage, wrapText };

0 comments on commit 9480dc5

Please sign in to comment.