File tree 1 file changed +6
-3
lines changed
1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change 1
1
const fs = require ( 'fs' ) ;
2
+ const GithubSlugger = require ( 'github-slugger' ) ;
2
3
3
4
function walk ( dir ) {
4
5
let results = [ ] ;
@@ -24,7 +25,7 @@ function generateID(text) {
24
25
. replace ( / [ ^ - a - z 0 - 9 ] / g, '' ) ;
25
26
}
26
27
27
- function addHeaderID ( line ) {
28
+ function addHeaderID ( line , slugger ) {
28
29
// check if we're a header at all
29
30
if ( ! line . startsWith ( '#' ) ) {
30
31
return line ;
@@ -35,10 +36,12 @@ function addHeaderID(line) {
35
36
}
36
37
const headingText = line . slice ( line . indexOf ( ' ' ) ) . trim ( ) ;
37
38
const headingLevel = line . slice ( 0 , line . indexOf ( ' ' ) ) ;
38
- return `${ headingLevel } ${ headingText } {#${ generateID ( headingText ) } }` ;
39
+ return `${ headingLevel } ${ headingText } {#${ slugger . slug ( headingText ) } }` ;
39
40
}
40
41
41
42
function addHeaderIDs ( lines ) {
43
+ // Sluggers should be per file
44
+ const slugger = new GithubSlugger ( ) ;
42
45
let inCode = false ;
43
46
const results = [ ] ;
44
47
lines . forEach ( line => {
@@ -53,7 +56,7 @@ function addHeaderIDs(lines) {
53
56
return ;
54
57
}
55
58
56
- results . push ( addHeaderID ( line ) ) ;
59
+ results . push ( addHeaderID ( line , slugger ) ) ;
57
60
} ) ;
58
61
return results ;
59
62
}
You can’t perform that action at this time.
0 commit comments