Help with proper toc labeling (#86)

Particularly to fix casing if somebody does "Table of Contents"
This commit is contained in:
Jon Meow
2020-06-22 11:36:16 -07:00
committed by GitHub
parent 444e041317
commit c0b63c5361
+33 -4
View File
@@ -17,12 +17,41 @@ from https://github.com/jonschlinkert/markdown-toc.
const mdtoc = require('markdown-toc');
const fs = require('fs');
var error = 0;
const files = process.argv.slice(2);
for (var i = 0; i < files.length; ++i) {
const oldContent = fs.readFileSync(files[i]).toString();
const newContent = mdtoc.insert(oldContent, { bullets: '-' });
const file = files[i];
const oldContent = fs.readFileSync(file).toString();
var newContent = oldContent;
// Only process files with the toc indicator.
if (!oldContent.match(/<!-- toc -->/m)) continue;
// If there's a case-incorrect toc section, fix casing.
newContent = newContent.replace(
/\n## Table of contents\n\n<!-- toc -->\n/im,
'\n## Table of contents\n\n<!-- toc -->\n'
);
if (oldContent != newContent) {
console.log(`Updating ${files[i]}`);
fs.writeFileSync(files[i], newContent);
console.log(`Fixed "Table of contents" header in ${file}`);
}
// Ensure the file properly labels the toc.
if (!newContent.match(/\n## Table of contents\n\n<!-- toc -->\n/m)) {
error = 1;
console.log(
`${file} has a toc without a "Table of contents" header. Use:\n` +
' ## Table of contents\n\n <!-- toc -->\n'
);
continue;
}
// Do the toc substitution.
newContent = mdtoc.insert(newContent, { bullets: '-' });
if (oldContent != newContent) {
console.log(`Updating ${file}`);
fs.writeFileSync(file, newContent);
}
}
process.exit(error);