|
|
@ -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')); |
|
|
|
}); |
|
|
|