@@ -26,12 +26,12 @@ function generateID(text) {
26
26
27
27
function addHeaderID ( line ) {
28
28
// check if we're a header at all
29
- if ( ! line . startsWith ( '#' ) ) return ;
29
+ if ( ! line . startsWith ( '#' ) ) return line ;
30
30
// check if it already has an id
31
- if ( / \{ # [ - A - Z a - z 0 - 9 ] + \} / . match ( line ) ) return ;
31
+ if ( / \{ # [ - A - Z a - z 0 - 9 ] + \} / . test ( line ) ) return line ;
32
32
const headingText = line . slice ( line . indexOf ( ' ' ) ) . trim ( ) ;
33
33
const headingLevel = line . slice ( 0 , line . indexOf ( ' ' ) ) ;
34
- return `${ headingLevel } ${ headingText } ${ generateID ( headingText ) } ` ;
34
+ return `${ headingLevel } ${ headingText } {# ${ generateID ( headingText ) } }` ;
35
35
}
36
36
37
37
function addHeaderIDs ( lines ) {
@@ -41,14 +41,17 @@ function addHeaderIDs(lines) {
41
41
// Ignore code blocks
42
42
if ( line . startsWith ( '```' ) ) {
43
43
inCode = ! inCode ;
44
+ results . push ( line ) ;
44
45
return ;
45
46
}
46
47
if ( inCode ) {
47
48
results . push ( line ) ;
49
+ return ;
48
50
}
49
51
50
52
results . push ( addHeaderID ( line ) ) ;
51
53
} ) ;
54
+ return results ;
52
55
}
53
56
54
57
const [ path ] = process . argv . slice ( 2 ) ;
@@ -57,8 +60,8 @@ const files = walk(path);
57
60
files . forEach ( file => {
58
61
if ( ! file . endsWith ( '.md' ) ) return ;
59
62
60
- const file = fs . readFileSync ( file , 'utf8' ) ;
61
- const lines = file . split ( '\n' ) ;
63
+ const content = fs . readFileSync ( file , 'utf8' ) ;
64
+ const lines = content . split ( '\n' ) ;
62
65
const updatedLines = addHeaderIDs ( lines ) ;
63
66
fs . writeFileSync ( file , updatedLines . join ( '\n' ) ) ;
64
67
} ) ;
0 commit comments