fix(v1): consistent slug & hash-link generation (#2019)

This commit is contained in:
Endi 2019-11-20 23:35:47 +07:00 committed by GitHub
parent 6595f00fe6
commit e544dc960a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 81 deletions

View file

@ -8,6 +8,7 @@
const {Remarkable} = require('remarkable');
const mdToc = require('markdown-toc');
const striptags = require('striptags');
const GithubSlugger = require('github-slugger');
const toSlug = require('./toSlug');
const tocRegex = new RegExp('<AUTOGENERATED_TABLE_OF_CONTENTS>', 'i');
@ -29,19 +30,15 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') {
const md = new Remarkable();
const headings = mdToc(content).json;
const toc = [];
const context = {};
const slugger = new GithubSlugger();
let current;
headings.forEach(heading => {
// we need always generate slugs to ensure, that we will have consistent
// slug indexes for headings with the same names
const rawContent = heading.content;
const safeContent = striptags(rawContent);
const rendered = md.renderInline(safeContent);
// We striptags again here as to not end up with html tags
// from markdown or markdown in our links
const hashLink = toSlug(striptags(rendered), context);
const hashLink = toSlug(rawContent, slugger);
if (!allowedHeadingLevels.includes(heading.lvl)) {
return;
}