// Strip any model preamble/sign-off so the saved file is just the article.
// Opus 4.8 sometimes narrates ("I'll research the sources first...") before the
// H1 or meta comment; we cut everything before the article's real start.
function cleanArticle(text) {
if (!text) return "";
let t = text.replace(/\r\n/g, "\n");
const candidates = [t.indexOf("<!--"), t.indexOf("# ")].filter((i) => i >= 0);
if (candidates.length) t = t.slice(Math.min(...candidates));
// Drop a trailing ```/fence if the model wrapped the whole thing in a code block.
t = t.replace(/^```(?:markdown|md)?\n/i, "").replace(/\n```\s*$/i, "");
return t.trim() + "\n";
}
module.exports = { cleanArticle };
if (require.main === module) {
// CLI: clean a file in place. node lib/clean.js path/to/file.md
const fs = require("fs");
const p = process.argv[2];
if (!p) {
console.error("usage: node lib/clean.js <file.md>");
process.exit(1);
}
fs.writeFileSync(p, cleanArticle(fs.readFileSync(p, "utf8")));
console.log("cleaned", p);
}