54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Strips truncated/orphan `<!-- en…` (or `<!-- en` followed by ellipsis)
|
|
* fragments from .md files. These were produced by a display-truncated
|
|
* comment that got round-tripped back into the source.
|
|
*/
|
|
|
|
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
function walkMd(dir: string): string[] {
|
|
const out: string[] = [];
|
|
for (const name of readdirSync(dir)) {
|
|
const full = join(dir, name);
|
|
const s = statSync(full);
|
|
if (s.isDirectory()) {
|
|
if (name === "node_modules" || name === "dist" || name === ".turbo")
|
|
continue;
|
|
out.push(...walkMd(full));
|
|
} else if (name.endsWith(".md")) {
|
|
out.push(full);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function clean(src: string): string {
|
|
let next = src;
|
|
// Strip `<!-- en…` (Unicode ellipsis) on its own line or inline.
|
|
next = next.replace(/[ \t]*<!--\s*en…[ \t]*\n?/g, "");
|
|
// Also strip any unterminated `<!-- end:auto-docs -->` (missing closing -->).
|
|
next = next.replace(/[ \t]*<!-- end:auto-docs[^>]*?\n/g, (m) => {
|
|
if (m.includes("-->")) return m;
|
|
return "";
|
|
});
|
|
return next;
|
|
}
|
|
|
|
function main(): void {
|
|
const files = walkMd("packages");
|
|
let changed = 0;
|
|
for (const file of files) {
|
|
const src = readFileSync(file, "utf8");
|
|
const next = clean(src);
|
|
if (next !== src) {
|
|
writeFileSync(file, next);
|
|
changed++;
|
|
}
|
|
}
|
|
console.log(`Cleaned ${changed} .md files.`);
|
|
}
|
|
|
|
main();
|