1import { ASSERT, UNWRAP } from "../../assert.ts";
2import { escapeHtmlAttr, escapeHtmlContent } from "../../string.ts";
3
4export interface Page {
5 title: string;
6 url: string;
7}
8
9export function formatList(pages: Array<Page>) {
10 return `<ul>${
11 pages
12 .map(x => `<li><a href="${escapeHtmlAttr(x.url)}">${escapeHtmlContent(x.title)}</a></li>`)
13 .join("")
14 }</ul>`;
15}
16
17/** example fake markdown processor */
18export function formatPage(text: string) {
19 let title = "blog";
20
21 if (text.startsWith("---")) {
22 const frontmatterEnd = text.slice(3).indexOf("---");
23 ASSERT(frontmatterEnd !== -1, "missing frontmatter");
24 const frontmatter = text.slice(3, 3 + frontmatterEnd);
25 text = text.slice(6 + frontmatterEnd).trim();
26 title = UNWRAP(frontmatter.match(/title:\s*(.*)\n/)?.[1]);
27 }
28
29 // four line markdown library lmao
30 text = text.replaceAll(/\*\*([^*]+?)\*\*/g, "<strong>$1</strong>");
31 text = text.replaceAll(/\b_([^_]+?)_\b/g, "<em>$1</em>");
32 text = text.replaceAll(/^## (.+)/gm, "<h2>$1</h2>");
33 text = text.replaceAll(/^# (.+)/gm, "<h1>$1</h1>");
34 text = text.replaceAll(/\n\n+/g, "\n");
35
36 text = `<html>
37 <title>${title}</title>
38 <body>
39 <h1><a href="index.html">blog!</a></h1>
40 <hr>
41 <pre>${text}</pre>
42 </body>
43 </html>`;
44
45 return { text, title };
46}