interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / docs / archive / drakestapleton.com-old / src / pages / WorkReaderPage.tsx

129 lines · 3567 bytes Raw Blame History
1import { Link, useParams } from "react-router-dom";
2import { WORKS } from "../data/worksCatalog";
3import "../styles/reader.css";
4
5function escapeHtml(value: string): string {
6 return value
7 .replaceAll("&", "&")
8 .replaceAll("<", "&lt;")
9 .replaceAll(">", "&gt;")
10 .replaceAll('"', "&quot;")
11 .replaceAll("'", "&#39;");
12}
13
14function renderInline(source: string): string {
15 const escaped = escapeHtml(source);
16 return escaped.replace(/\[([^\]\n]+)\]\(([^)\s]+)\)/g, (full, label: string, href: string) => {
17 const allowed = /^(https?:\/\/|mailto:|#|\/(?!\/))/i.test(href);
18 if (allowed) return `<a href="${href}">${label}</a>`;
19 return full;
20 });
21}
22
23function bodyMarkdown(markdown: string): string {
24 if (!markdown.startsWith("---\n")) return markdown;
25 const end = markdown.indexOf("\n---\n", 3);
26 if (end === -1) return markdown;
27 return markdown.slice(end + 5);
28}
29
30function markdownToHtml(markdown: string): string {
31 const lines = bodyMarkdown(markdown).replaceAll("\r\n", "\n").split("\n");
32 const blocks: string[] = [];
33 let paragraph: string[] = [];
34 let index = 0;
35
36 const flushParagraph = () => {
37 if (paragraph.length === 0) return;
38 blocks.push(`<p>${renderInline(paragraph.join(" "))}</p>`);
39 paragraph = [];
40 };
41
42 while (index < lines.length) {
43 const line = lines[index].trim();
44 const headingMatch = /^(#{1,6})\s+(.*)$/.exec(line);
45 if (headingMatch) {
46 flushParagraph();
47 const level = headingMatch[1].length;
48 blocks.push(`<h${level}>${renderInline(headingMatch[2])}</h${level}>`);
49 index += 1;
50 continue;
51 }
52
53 if (line.startsWith(">")) {
54 flushParagraph();
55 const quote: string[] = [];
56 while (index < lines.length && lines[index].trim().startsWith(">")) {
57 quote.push(lines[index].trim().replace(/^>\s?/, ""));
58 index += 1;
59 }
60 blocks.push(`<blockquote><p>${renderInline(quote.join(" "))}</p></blockquote>`);
61 continue;
62 }
63
64 if (/^[-*]\s+/.test(line)) {
65 flushParagraph();
66 const items: string[] = [];
67 while (index < lines.length) {
68 const item = /^[-*]\s+(.*)$/.exec(lines[index].trim());
69 if (!item) break;
70 items.push(`<li>${renderInline(item[1])}</li>`);
71 index += 1;
72 }
73 blocks.push(`<ul>${items.join("")}</ul>`);
74 continue;
75 }
76
77 if (/^\d+\.\s+/.test(line)) {
78 flushParagraph();
79 const items: string[] = [];
80 while (index < lines.length) {
81 const item = /^\d+\.\s+(.*)$/.exec(lines[index].trim());
82 if (!item) break;
83 items.push(`<li>${renderInline(item[1])}</li>`);
84 index += 1;
85 }
86 blocks.push(`<ol>${items.join("")}</ol>`);
87 continue;
88 }
89
90 if (line === "") {
91 flushParagraph();
92 index += 1;
93 continue;
94 }
95
96 paragraph.push(line);
97 index += 1;
98 }
99
100 flushParagraph();
101 return blocks.join("");
102}
103
104export function WorkReaderPage() {
105 const { slug } = useParams();
106 const work = WORKS.find((item) => item.slug === slug);
107
108 if (!work) {
109 return (
110 <main className="reader">
111 <article>
112 <p>
113 This piece is absent from this volume. <Link to="/works">Return to works</Link>
114 </p>
115 </article>
116 </main>
117 );
118 }
119
120 return (
121 <main className="reader">
122 <article>
123 <h1>{work.title}</h1>
124 <p className="reader-date">{work.date}</p>
125 <div className="reader-body" dangerouslySetInnerHTML={{ __html: markdownToHtml(work.markdown) }} />
126 </article>
127 </main>
128 );
129}