Add TOCs to files with a TOC header but no body

This commit is contained in:
Natalie Weizenbaum 2020-11-23 17:12:51 -08:00
parent c08d3bc3c3
commit 826fe7d78d

View File

@ -5,11 +5,25 @@ toc.files.forEach(function (file) {
var markdown = fs.readFileSync(file).toString()
var currentToc = toc.getCurrent(markdown)
if (currentToc == null) return
if (currentToc == null) {
var match = markdown.match('## Table of Contents\n\n')
if (!match) return
var tocLocation = match.index + match[0].length
// If there's an empty TOC, fill it in.
fs.writeFileSync(
file,
markdown.substring(0, tocLocation) +
toc.generate(markdown) + '\n\n' +
markdown.substring(tocLocation))
console.log(`Added TOC to ${file}`)
return
}
var generatedToc = toc.generate(markdown)
if (currentToc === generatedToc) return
fs.writeFileSync(file, markdown.replace(currentToc, generatedToc))
console.log(`Updated ${file}`)
console.log(`Updated TOC in ${file}`)
})