Skip to content

Commit

Permalink
update script
Browse files Browse the repository at this point in the history
  • Loading branch information
tesseralis committed Feb 7, 2019
1 parent 29e5855 commit 0a45ebf
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions scripts/generateHeaderIDs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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'));
});

0 comments on commit 0a45ebf

Please sign in to comment.