-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
172 lines (147 loc) · 4.13 KB
/
Copy pathpost.js
File metadata and controls
172 lines (147 loc) · 4.13 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
163
164
165
166
167
168
169
170
171
172
const article = document.querySelector("#article");
const params = new URLSearchParams(window.location.search);
const source = params.get("src");
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function inlineMarkdown(value) {
return escapeHtml(value)
.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")
.replace(/`(.+?)`/g, "<code>$1</code>");
}
function renderTable(lines) {
const rows = lines
.filter((line) => !/^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)+\|?\s*$/.test(line))
.map((line) => line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((cell) => inlineMarkdown(cell.trim())));
if (rows.length === 0) return "";
const [head, ...body] = rows;
const header = `<thead><tr>${head.map((cell) => `<th>${cell}</th>`).join("")}</tr></thead>`;
const rowsHtml = body.map((row) => `<tr>${row.map((cell) => `<td>${cell}</td>`).join("")}</tr>`).join("");
return `<div class="table-wrap"><table>${header}<tbody>${rowsHtml}</tbody></table></div>`;
}
function markdownToHtml(markdown) {
const lines = markdown.replace(/\r\n/g, "\n").split("\n");
const html = [];
let paragraph = [];
let list = [];
let quote = [];
let table = [];
let code = [];
let inCode = false;
function flushParagraph() {
if (paragraph.length) {
html.push(`<p>${inlineMarkdown(paragraph.join(" "))}</p>`);
paragraph = [];
}
}
function flushList() {
if (list.length) {
html.push(`<ul>${list.map((item) => `<li>${inlineMarkdown(item)}</li>`).join("")}</ul>`);
list = [];
}
}
function flushQuote() {
if (quote.length) {
html.push(`<blockquote>${quote.map((item) => `<p>${inlineMarkdown(item)}</p>`).join("")}</blockquote>`);
quote = [];
}
}
function flushTable() {
if (table.length) {
html.push(renderTable(table));
table = [];
}
}
for (const line of lines) {
if (line.trim().startsWith("```")) {
if (inCode) {
html.push(`<pre><code>${escapeHtml(code.join("\n"))}</code></pre>`);
code = [];
inCode = false;
} else {
flushParagraph();
flushList();
flushQuote();
flushTable();
inCode = true;
}
continue;
}
if (inCode) {
code.push(line);
continue;
}
if (!line.trim()) {
flushParagraph();
flushList();
flushQuote();
flushTable();
continue;
}
if (/^\s*\|.+\|\s*$/.test(line)) {
flushParagraph();
flushList();
flushQuote();
table.push(line);
continue;
}
flushTable();
const heading = /^(#{1,6})\s+(.+)$/.exec(line);
if (heading) {
flushParagraph();
flushList();
flushQuote();
const level = Math.min(heading[1].length, 3);
html.push(`<h${level}>${inlineMarkdown(heading[2])}</h${level}>`);
continue;
}
if (/^---+$/.test(line.trim())) {
flushParagraph();
flushList();
flushQuote();
html.push("<hr>");
continue;
}
const bullet = /^\s*[-*]\s+(.+)$/.exec(line);
if (bullet) {
flushParagraph();
flushQuote();
list.push(bullet[1]);
continue;
}
const quoted = /^\s*>\s?(.+)$/.exec(line);
if (quoted) {
flushParagraph();
flushList();
quote.push(quoted[1]);
continue;
}
paragraph.push(line.trim());
}
flushParagraph();
flushList();
flushQuote();
flushTable();
return html.join("");
}
async function loadArticle() {
if (!source || !source.startsWith("posts/") || !source.endsWith(".md")) {
article.innerHTML = '<p class="empty-state">文章地址无效。</p>';
return;
}
try {
const response = await fetch(`${source}?v=2026080403`);
const markdown = await response.text();
article.innerHTML = markdownToHtml(markdown);
const title = article.querySelector("h1")?.textContent;
if (title) document.title = `${title} - Zoro-wH Blog`;
} catch (error) {
article.innerHTML = '<p class="empty-state">文章加载失败。</p>';
}
}
loadArticle();