Ensure anchor links are unique per document (#574)

This commit is contained in:
Sviatoslav 2018-05-04 05:36:12 +03:00 committed by Yangshun Tay
parent 2a83959ac1
commit 9c98142fea
12 changed files with 440 additions and 43 deletions

View file

@ -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);