Skip to content

Commit

Permalink
use githubslugger
Browse files Browse the repository at this point in the history
  • Loading branch information
tesseralis committed Feb 7, 2019
1 parent 6a50996 commit 5a37071
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions scripts/generateHeadingIDs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const GithubSlugger = require('github-slugger');

function walk(dir) {
let results = [];
Expand All @@ -24,7 +25,7 @@ function generateID(text) {
.replace(/[^-a-z0-9]/g, '');
}

function addHeaderID(line) {
function addHeaderID(line, slugger) {
// check if we're a header at all
if (!line.startsWith('#')) {
return line;
Expand All @@ -35,10 +36,12 @@ function addHeaderID(line) {
}
const headingText = line.slice(line.indexOf(' ')).trim();
const headingLevel = line.slice(0, line.indexOf(' '));
return `${headingLevel} ${headingText} {#${generateID(headingText)}}`;
return `${headingLevel} ${headingText} {#${slugger.slug(headingText)}}`;
}

function addHeaderIDs(lines) {
// Sluggers should be per file
const slugger = new GithubSlugger();
let inCode = false;
const results = [];
lines.forEach(line => {
Expand All @@ -53,7 +56,7 @@ function addHeaderIDs(lines) {
return;
}

results.push(addHeaderID(line));
results.push(addHeaderID(line, slugger));
});
return results;
}
Expand Down

0 comments on commit 5a37071

Please sign in to comment.