Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Created December 16, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderbyheart/502752c05ad0ca2350f9a9187450c543 to your computer and use it in GitHub Desktop.
Save coderbyheart/502752c05ad0ca2350f9a9187450c543 to your computer and use it in GitHub Desktop.
Fixes RST headline lenghts
import glob from "glob";
import { promises as fs } from "fs";
let changed = false;
Promise.all(
glob.sync("./**/*.rst").map((s) =>
fs.readFile(s, "utf-8").then((rst) => {
const lines = rst.split("\n");
lines.forEach((line, n) => {
const m = /^[#\*=-]+$/.exec(line);
if (m) {
lines[n] = m[0][0].repeat(lines[n - 1].length);
}
});
const updated = lines.join("\n");
if (updated.localeCompare(rst) !== 0) {
changed = true;
console.error(`Rewritten ${s}`);
return fs.writeFile(s, updated, "utf-8");
}
})
)
).then(() => {
if (changed) {
process.exit(1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment