-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (134 loc) · 4.08 KB
/
Copy pathscript.js
File metadata and controls
162 lines (134 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const toggles = document.querySelectorAll('.work-toggle');
async function loadContent(toggle) {
const targetId = toggle.dataset.target;
const content = document.getElementById(targetId);
const icon = toggle.querySelector('.work-icon');
if (!content) return;
const isOpen = !content.hasAttribute('hidden');
if (isOpen) {
content.setAttribute('hidden', '');
if (icon) icon.textContent = '+';
return;
}
content.removeAttribute('hidden');
if (icon) icon.textContent = '−';
if (content.dataset.loaded === 'true') return;
try {
const response = await fetch('./content/police-records-access-project.md');
if (!response.ok) {
throw new Error('Unable to load content.');
}
const markdown = await response.text();
content.innerHTML = renderMarkdown(markdown);
content.dataset.loaded = 'true';
} catch (error) {
content.innerHTML = '<p>Project details will appear here once the article content is available.</p>';
content.dataset.loaded = 'true';
}
}
function renderMarkdown(markdown) {
const lines = markdown.split(/\r?\n/);
const html = [];
const footnotes = {};
let paragraph = [];
let listType = null;
let listItems = [];
const formatInline = (text) => {
return text
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\[\^([^^\]]+)\]/g, (_, id) => {
const label = id.replace(/^e/, '');
return `<sup class="footnote-ref"><a href="#fn-${id}" id="fnref-${id}">${label}</a></sup>`;
});
};
const flushParagraph = () => {
if (paragraph.length) {
const text = paragraph.join(' ').trim();
if (text) {
html.push(`<p>${formatInline(text)}</p>`);
}
paragraph = [];
}
};
const flushList = () => {
if (listItems.length) {
const tag = listType === 'ordered' ? 'ol' : 'ul';
html.push(`<${tag}>${listItems.map((item) => `<li>${formatInline(item)}</li>`).join('')}</${tag}>`);
listItems = [];
listType = null;
}
};
const bodyLines = [];
for (const line of lines) {
const trimmed = line.trim();
const noteMatch = trimmed.match(/^\[\^([^\]]+)\]:\s*(.*)$/);
if (noteMatch) {
footnotes[noteMatch[1]] = noteMatch[2];
continue;
}
bodyLines.push(trimmed);
}
for (const line of bodyLines) {
const trimmed = line.trim();
if (!trimmed) {
flushParagraph();
flushList();
continue;
}
if (/^#{1,6}\s/.test(trimmed)) {
flushParagraph();
flushList();
const level = trimmed.match(/^#+/)[0].length;
const text = formatInline(trimmed.replace(/^#{1,6}\s/, ''));
html.push(`<h${level}>${text}</h${level}>`);
continue;
}
if (/^---$/.test(trimmed)) {
flushParagraph();
flushList();
html.push('<hr />');
continue;
}
if (/^[-*]\s/.test(trimmed)) {
flushParagraph();
const itemText = trimmed.replace(/^[-*]\s/, '');
listType = listType || 'unordered';
if (listType !== 'unordered') {
flushList();
listType = 'unordered';
}
listItems.push(itemText);
continue;
}
if (/^\d+\.\s/.test(trimmed)) {
flushParagraph();
const itemText = trimmed.replace(/^\d+\.\s/, '');
listType = listType || 'ordered';
if (listType !== 'ordered') {
flushList();
listType = 'ordered';
}
listItems.push(itemText);
continue;
}
paragraph.push(trimmed);
}
flushParagraph();
flushList();
if (Object.keys(footnotes).length) {
const footnoteList = Object.entries(footnotes)
.map(([id, content]) => `<li id="fn-${id}" class="footnote-item">${formatInline(content)} <a href="#fnref-${id}" class="footnote-backref">↩</a></li>`)
.join('');
html.push(`<section class="footnotes"><h3>Endnotes</h3><ol>${footnoteList}</ol></section>`);
}
return `<article class="markdown-body">${html.join('')}</article>`;
}
for (const toggle of toggles) {
toggle.addEventListener('click', () => {
loadContent(toggle);
});
}
const firstToggle = toggles[0];
if (firstToggle) {
loadContent(firstToggle);
}