import { readFile } from "node:fs/promises";
import { join } from "node:path";
const esc = (s) => s.replace(/[&<>]/g, (c) => ({ "&": "&", "<": "<", ">": ">" })[c]);
export const PLACEHOLDER = /\[ЗАПОЛНИТЬ[^\]]*\]/;
// Простой рендер markdown: #, ##, - и абзацы.
function render(md) {
const out = [];
for (const line of md.split("\n")) {
if (line.startsWith("## ")) out.push(`
${esc(line.slice(3))}
`);
else if (line.startsWith("# ")) out.push(`${esc(line.slice(2))}
`);
else if (line.startsWith("- ")) out.push(`${esc(line.slice(2))}`);
else if (line.trim()) out.push(`${esc(line)}
`);
}
return `Документ${out.join("\n")}← На главную
`;
}
// Документ публикуется только при LEGAL_APPROVED=1 и если в тексте не осталось «[ЗАПОЛНИТЬ: …]».
export async function legalPage(config, name) {
if (!config.legalApproved) return null;
let md;
try {
md = await readFile(join(config.legalDir, `${name}.md`), "utf8");
} catch {
return null;
}
if (PLACEHOLDER.test(md)) return null;
return render(md);
}