31 lines
1.6 KiB
JavaScript
31 lines
1.6 KiB
JavaScript
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(`<h2>${esc(line.slice(3))}</h2>`);
|
||
else if (line.startsWith("# ")) out.push(`<h1>${esc(line.slice(2))}</h1>`);
|
||
else if (line.startsWith("- ")) out.push(`<li>${esc(line.slice(2))}</li>`);
|
||
else if (line.trim()) out.push(`<p>${esc(line)}</p>`);
|
||
}
|
||
return `<!doctype html><html lang="ru"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Документ</title><style>body{font:17px/1.55 system-ui,sans-serif;max-width:760px;margin:0 auto;padding:24px 16px;background:#fff;color:#1d2733}@media (prefers-color-scheme:dark){body{background:#10161d;color:#e8edf2}}</style></head><body>${out.join("\n")}<p><a href="/">← На главную</a></p></body></html>`;
|
||
}
|
||
|
||
// Документ публикуется только при 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);
|
||
}
|