mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
Ensure anchor links are unique per document (#574)
This commit is contained in:
parent
2a83959ac1
commit
9c98142fea
12 changed files with 440 additions and 43 deletions
|
@ -14,7 +14,7 @@ async function execute() {
|
|||
const readMetadata = require('./readMetadata.js');
|
||||
const path = require('path');
|
||||
const color = require('color');
|
||||
const toSlug = require('../core/toSlug.js');
|
||||
const getTOC = require('../core/getTOC.js');
|
||||
const React = require('react');
|
||||
const mkdirp = require('mkdirp');
|
||||
const glob = require('glob');
|
||||
|
@ -42,15 +42,12 @@ async function execute() {
|
|||
// takes the content of a doc article and returns the content with a table of
|
||||
// contents inserted
|
||||
const insertTableOfContents = rawContent => {
|
||||
const regexp = /\n###\s+(`.*`.*)\n/g;
|
||||
let match;
|
||||
const headers = [];
|
||||
while ((match = regexp.exec(rawContent))) {
|
||||
headers.push(match[1]);
|
||||
}
|
||||
const filterRe = /^`[^`]*`/;
|
||||
const headers = getTOC(rawContent, 'h3', null);
|
||||
|
||||
const tableOfContents = headers
|
||||
.map(header => ` - [${header}](#${toSlug(header)})`)
|
||||
.filter(header => filterRe.test(header.rawContent))
|
||||
.map(header => ` - [${header.rawContent}](#${header.hashLink})`)
|
||||
.join('\n');
|
||||
|
||||
return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue