Skip to content

Commit 0a45ebf

Browse files
committed
update script
1 parent 29e5855 commit 0a45ebf

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

scripts/generateHeaderIDs.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ function generateID(text) {
2626

2727
function addHeaderID(line) {
2828
// check if we're a header at all
29-
if (!line.startsWith('#')) return;
29+
if (!line.startsWith('#')) return line;
3030
// check if it already has an id
31-
if (/\{#[-A-Za-z0-9]+\}/.match(line)) return;
31+
if (/\{#[-A-Za-z0-9]+\}/.test(line)) return line;
3232
const headingText = line.slice(line.indexOf(' ')).trim();
3333
const headingLevel = line.slice(0, line.indexOf(' '));
34-
return `${headingLevel} ${headingText} ${generateID(headingText)}`;
34+
return `${headingLevel} ${headingText} {#${generateID(headingText)}}`;
3535
}
3636

3737
function addHeaderIDs(lines) {
@@ -41,14 +41,17 @@ function addHeaderIDs(lines) {
4141
// Ignore code blocks
4242
if (line.startsWith('```')) {
4343
inCode = !inCode;
44+
results.push(line);
4445
return;
4546
}
4647
if (inCode) {
4748
results.push(line);
49+
return;
4850
}
4951

5052
results.push(addHeaderID(line));
5153
});
54+
return results;
5255
}
5356

5457
const [path] = process.argv.slice(2);
@@ -57,8 +60,8 @@ const files = walk(path);
5760
files.forEach(file => {
5861
if (!file.endsWith('.md')) return;
5962

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');
6265
const updatedLines = addHeaderIDs(lines);
6366
fs.writeFileSync(file, updatedLines.join('\n'));
6467
});

0 commit comments

Comments
 (0)