Browse Source

use githubslugger

main
Nat Alison 6 years ago
parent
commit
5a37071ecd
  1. 9
      scripts/generateHeadingIDs.js

9
scripts/generateHeadingIDs.js

@ -1,4 +1,5 @@
const fs = require('fs'); const fs = require('fs');
const GithubSlugger = require('github-slugger');
function walk(dir) { function walk(dir) {
let results = []; let results = [];
@ -24,7 +25,7 @@ function generateID(text) {
.replace(/[^-a-z0-9]/g, ''); .replace(/[^-a-z0-9]/g, '');
} }
function addHeaderID(line) { function addHeaderID(line, slugger) {
// check if we're a header at all // check if we're a header at all
if (!line.startsWith('#')) { if (!line.startsWith('#')) {
return line; return line;
@ -35,10 +36,12 @@ function addHeaderID(line) {
} }
const headingText = line.slice(line.indexOf(' ')).trim(); const headingText = line.slice(line.indexOf(' ')).trim();
const headingLevel = line.slice(0, line.indexOf(' ')); const headingLevel = line.slice(0, line.indexOf(' '));
return `${headingLevel} ${headingText} {#${generateID(headingText)}}`; return `${headingLevel} ${headingText} {#${slugger.slug(headingText)}}`;
} }
function addHeaderIDs(lines) { function addHeaderIDs(lines) {
// Sluggers should be per file
const slugger = new GithubSlugger();
let inCode = false; let inCode = false;
const results = []; const results = [];
lines.forEach(line => { lines.forEach(line => {
@ -53,7 +56,7 @@ function addHeaderIDs(lines) {
return; return;
} }
results.push(addHeaderID(line)); results.push(addHeaderID(line, slugger));
}); });
return results; return results;
} }

Loading…
Cancel
Save