From af19a010c573b8879f24e9afbdb48b20e7c836c3 Mon Sep 17 00:00:00 2001 From: Mikk Andresen Date: Tue, 6 Apr 2021 22:10:55 +0300 Subject: [PATCH] DOCS: Fix broken links in TOC - use Marked to generate ID slugs instead of local implementation that was giving out different IDs in some cases - https://github.com/citizenos/citizenos-fe/issues/535 --- src/bin/doc/html.js | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/bin/doc/html.js b/src/bin/doc/html.js index f4be7ce13..ff07ae7c8 100644 --- a/src/bin/doc/html.js +++ b/src/bin/doc/html.js @@ -146,15 +146,16 @@ const buildToc = (lexed, filename, cb) => { lexed.forEach((tok) => { if (tok.type !== 'heading') return; if (tok.depth - depth > 1) { - return cb(new Error(`Inappropriate heading level\n${ - JSON.stringify(tok)}`)); + return cb(new Error(`Inappropriate heading level\n${JSON.stringify(tok)}`)); } depth = tok.depth; - const id = getId(`${filename}_${tok.text.trim()}`); - toc.push(`${new Array((depth - 1) * 2 + 1).join(' ') - }* ${ - tok.text}`); + + const slugger = new marked.Slugger(); + const id = slugger.slug(`${filename}_${tok.text.trim()}`); + + toc.push(`${new Array((depth - 1) * 2 + 1).join(' ')}* ${tok.text}`); + tok.text += `#`; }); @@ -162,17 +163,3 @@ const buildToc = (lexed, filename, cb) => { toc = marked.parse(toc.join('\n')); cb(null, toc); }; - -const idCounters = {}; -const getId = (text) => { - text = text.toLowerCase(); - text = text.replace(/[^a-z0-9]+/g, '_'); - text = text.replace(/^_+|_+$/, ''); - text = text.replace(/^([^a-z])/, '_$1'); - if (Object.prototype.hasOwnProperty.call(idCounters, text)) { - text += `_${++idCounters[text]}`; - } else { - idCounters[text] = 0; - } - return text; -};