From 0a45ebfa5ff6fb858ac5f2c074c9187dd0ba9800 Mon Sep 17 00:00:00 2001 From: Nat Alison Date: Wed, 6 Feb 2019 16:56:54 -0800 Subject: [PATCH] update script --- scripts/generateHeaderIDs.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/generateHeaderIDs.js b/scripts/generateHeaderIDs.js index cdcd4a5e3..77153ecee 100644 --- a/scripts/generateHeaderIDs.js +++ b/scripts/generateHeaderIDs.js @@ -26,12 +26,12 @@ function generateID(text) { function addHeaderID(line) { // check if we're a header at all - if (!line.startsWith('#')) return; + if (!line.startsWith('#')) return line; // check if it already has an id - if (/\{#[-A-Za-z0-9]+\}/.match(line)) return; + if (/\{#[-A-Za-z0-9]+\}/.test(line)) return line; const headingText = line.slice(line.indexOf(' ')).trim(); const headingLevel = line.slice(0, line.indexOf(' ')); - return `${headingLevel} ${headingText} ${generateID(headingText)}`; + return `${headingLevel} ${headingText} {#${generateID(headingText)}}`; } function addHeaderIDs(lines) { @@ -41,14 +41,17 @@ function addHeaderIDs(lines) { // Ignore code blocks if (line.startsWith('```')) { inCode = !inCode; + results.push(line); return; } if (inCode) { results.push(line); + return; } results.push(addHeaderID(line)); }); + return results; } const [path] = process.argv.slice(2); @@ -57,8 +60,8 @@ const files = walk(path); files.forEach(file => { if (!file.endsWith('.md')) return; - const file = fs.readFileSync(file, 'utf8'); - const lines = file.split('\n'); + const content = fs.readFileSync(file, 'utf8'); + const lines = content.split('\n'); const updatedLines = addHeaderIDs(lines); fs.writeFileSync(file, updatedLines.join('\n')); });