mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
Help with proper toc labeling (#86)
Particularly to fix casing if somebody does "Table of Contents"
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user