// ============================================================================
// Project : anomaly-atlas
// File : web/lib/render.js
// Purpose : HTML layout, markdown/code rendering for the atlas platform
// Author : Simon-Pierre Boucher
// Contact : contact@spboucher.ai
// Data src : hfmarketdata.io (sole data source)
// Created : 2026-08-12
// Modified : 2026-08-12
// Platform : macOS / Apple Silicon (arm64) — Node.js (deployed on MacLustr)
// License : All rights reserved (research code)
// ============================================================================
"use strict";
const { Marked } = require("marked");
const hljs = require("highlight.js");
function esc(s) {
return String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
}
function slugify(s) {
return s.toLowerCase().replace(/[^\w\s-]/g, "").trim().replace(/\s+/g, "-").slice(0, 80);
}
/**
* Render markdown → { html, toc }. A fresh Marked instance per call (a shared
* global `marked.use()` would stack walkTokens handlers on every render).
* Headings get stable anchor ids collected into a table of contents.
*/
function markdownToHtml(md, baseRel) {
const toc = [];
const seen = new Set();
const m = new Marked({
renderer: {
code({ text, lang }) {
const html = lang && hljs.getLanguage(lang)
? hljs.highlight(text, { language: lang }).value
: esc(text);
return `
${html} `;
},
heading({ tokens, depth }) {
const html = this.parser.parseInline(tokens);
const plain = html.replace(/<[^>]+>/g, "");
let id = slugify(plain) || "section";
while (seen.has(id)) id += "-x";
seen.add(id);
if (depth >= 2 && depth <= 3) toc.push({ depth, id, text: plain });
return `${html}# \n`;
},
},
walkTokens(token) {
if (token.type === "link" && token.href && !/^(https?:|mailto:|#|\/)/.test(token.href)) {
const base = baseRel.includes("/") ? baseRel.slice(0, baseRel.lastIndexOf("/")) : "";
const joined = (base ? base + "/" : "") + token.href;
const norm = joined.split("/").reduce((acc, part) => {
if (part === "..") acc.pop();
else if (part !== "." && part !== "") acc.push(part);
return acc;
}, []).join("/");
token.href = norm.endsWith(".md") ? "/doc/" + norm : "/file/" + norm;
}
},
});
return { html: m.parse(md), toc };
}
/** Sticky table-of-contents sidebar for long documents. */
function tocHtml(toc) {
if (!toc || toc.length < 3) return "";
const items = toc
.map((t) => `${esc(t.text)} `)
.join("");
return `On this page `;
}
/** Professional document header from front matter. */
function docHeader(data, rel) {
const title = (data.document || rel).split("/").pop().replace(/[_-]/g, " ");
const status = data.status || "draft";
const meta = [
data.created ? `created ${data.created}` : null,
data.modified ? `updated ${data.modified}` : null,
data.author || null,
].filter(Boolean).join(" · ");
return ``;
}
/** Research-log timeline: entries already split by content.parseLogEntries. */
function logTimeline(entries, baseRel) {
const items = entries
.slice()
.reverse()
.map((e) => {
const [when, ...rest] = e.title.split(" — ");
const title = rest.join(" — ") || when;
const body = markdownToHtml(e.body, baseRel).html;
return `
${esc(rest.length ? when : "")}
${esc(title)}
${body}
`;
})
.join("");
return `${items}
`;
}
function highlightFile(text, filename) {
const ext = filename.slice(filename.lastIndexOf(".") + 1).toLowerCase();
const langMap = {
py: "python", sh: "bash", js: "javascript", css: "css", json: "json",
toml: "ini", yaml: "yaml", yml: "yaml", cff: "yaml", md: "markdown", sql: "sql",
};
const lang = filename === "Makefile" ? "makefile" : langMap[ext];
if (lang && hljs.getLanguage(lang)) return hljs.highlight(text, { language: lang }).value;
return esc(text);
}
/** Confidence-level badge (Levels 0-3, CLAUDE.md §10). */
const LEVEL_LABELS = {
0: "Level 0 — in-sample only",
1: "Level 1 — corrected & OOS",
2: "Level 2 — robust",
3: "Level 3 — cost-real & held-out",
};
function levelBadge(level) {
return `${LEVEL_LABELS[level] || "Level ?"} `;
}
const NAV = [
["/", "Home"],
["/atlas", "Atlas"],
["/publications", "Publications"],
["/research", "Research"],
["/experiments", "Experiments"],
["/results", "Results"],
["/code", "Code"],
["/comments", "Comments"],
["/about", "About"],
];
function layout({ title, active, body, buildInfo = {} }) {
const nav = NAV.map(
([href, label]) =>
`${label} `
).join("");
const commit = buildInfo.commit ? buildInfo.commit.slice(0, 7) : "dev";
const synced = buildInfo.generated ? buildInfo.generated.slice(0, 10) : "";
return `
${esc(title)} · anomaly-atlas
Honesty doctrine. Every candidate anomaly is an artifact until proven otherwise;
in-sample results are never findings; past statistical regularity does not imply future returns.
This is research on statistical properties of market data — not investment advice, not a trading system .
${body}
`;
}
module.exports = { esc, markdownToHtml, tocHtml, docHeader, logTimeline, highlightFile, layout, levelBadge, LEVEL_LABELS };