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

This commit is contained in:
Mikk Andresen 2021-04-06 22:10:55 +03:00 committed by webzwo0i
parent 78ea888cb7
commit af19a010c5
1 changed files with 7 additions and 20 deletions

View File

@ -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(' ')
}* <a href="#${id}">${
tok.text}</a>`);
const slugger = new marked.Slugger();
const id = slugger.slug(`${filename}_${tok.text.trim()}`);
toc.push(`${new Array((depth - 1) * 2 + 1).join(' ')}* <a href="#${id}">${tok.text}</a>`);
tok.text += `<span><a class="mark" href="#${id}" ` +
`id="${id}">#</a></span>`;
});
@ -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;
};