mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
fix(v1): consistent slug & hash-link generation (#2019)
This commit is contained in:
parent
6595f00fe6
commit
e544dc960a
6 changed files with 25 additions and 81 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue