// Reads deck.md into slides. Shared by build.mjs and check.mjs, so the checks see exactly the
// slides the build draws.
//
// Slides are separated by a line of three dashes. Each slide starts with a "# Title". A line
// "" picks a layout. Everything after a line "Notes:"
// is speaker notes.
export function parseDeck(text) {
const slides = [];
const seen = new Map();
for (const raw of text.replace(/\r\n?/g, "\n").split(/^---[ \t]*$/m)) {
const chunk = raw.trim();
if (!chunk) continue;
let layout = slides.length === 0 ? "title" : "default";
const lines = chunk.split("\n").filter((line) => {
const m = /^$/.exec(line.trim());
if (m) layout = m[1];
return !m;
});
const notesAt = lines.findIndex((l) => /^notes:/i.test(l.trim()));
const body = notesAt >= 0 ? lines.slice(0, notesAt) : lines;
const notes =
notesAt >= 0 ? [lines[notesAt].replace(/^\s*notes:\s*/i, ""), ...lines.slice(notesAt + 1)].join("\n").trim() : "";
const titleAt = body.findIndex((l) => /^#\s+/.test(l));
const title = titleAt >= 0 ? body[titleAt].replace(/^#\s+/, "").trim() : "";
const content = body
.filter((_, i) => i !== titleAt)
.join("\n")
.trim();
let id = `s-${
title
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "") || slides.length + 1
}`;
const n = (seen.get(id) ?? 0) + 1;
seen.set(id, n);
if (n > 1) id += `-${n}`;
slides.push({ n: slides.length + 1, id, layout, title, content, notes });
}
return slides;
}
export function countWords(text) {
return (
text
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
.replace(/\]\([^)]*\)/g, "]")
.match(/[\p{L}\p{N}]+(?:['’][\p{L}\p{N}]+)*/gu) ?? []
).length;
}
const ESC = { "&": "&", "<": "<", ">": ">", '"': """ };
const esc = (s) => s.replace(/[&<>"]/g, (c) => ESC[c]);
function inline(s) {
return esc(s)
.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, '')
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, '$1')
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/\*([^*]+)\*/g, "$1")
.replace(/`([^`]+)`/g, "
$1");
}
/** A slide's content as HTML: paragraphs, lists, quotes. */
export function renderContent(md) {
const out = [];
let list = null;
let para = [];
const flush = () => {
if (para.length) out.push(`
${inline(para.join(" "))}
`); para = []; if (list) out.push(`<${list.tag}>${list.items.map((i) => `${inline(quote[1])}`); } else para.push(line.trim()); } flush(); return out.join("\n"); } export { esc };