Skip to content

Commit 5a37071

Browse files
committed
use githubslugger
1 parent 6a50996 commit 5a37071

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

scripts/generateHeadingIDs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs');
2+
const GithubSlugger = require('github-slugger');
23

34
function walk(dir) {
45
let results = [];
@@ -24,7 +25,7 @@ function generateID(text) {
2425
.replace(/[^-a-z0-9]/g, '');
2526
}
2627

27-
function addHeaderID(line) {
28+
function addHeaderID(line, slugger) {
2829
// check if we're a header at all
2930
if (!line.startsWith('#')) {
3031
return line;
@@ -35,10 +36,12 @@ function addHeaderID(line) {
3536
}
3637
const headingText = line.slice(line.indexOf(' ')).trim();
3738
const headingLevel = line.slice(0, line.indexOf(' '));
38-
return `${headingLevel} ${headingText} {#${generateID(headingText)}}`;
39+
return `${headingLevel} ${headingText} {#${slugger.slug(headingText)}}`;
3940
}
4041

4142
function addHeaderIDs(lines) {
43+
// Sluggers should be per file
44+
const slugger = new GithubSlugger();
4245
let inCode = false;
4346
const results = [];
4447
lines.forEach(line => {
@@ -53,7 +56,7 @@ function addHeaderIDs(lines) {
5356
return;
5457
}
5558

56-
results.push(addHeaderID(line));
59+
results.push(addHeaderID(line, slugger));
5760
});
5861
return results;
5962
}

0 commit comments

Comments
 (0)