Repository navigation
Expand file tree
/
Copy patharticle-content.js
More file actions
196 lines (172 loc) · 7.47 KB
/
Copy patharticle-content.js
File metadata and controls
196 lines (172 loc) · 7.47 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Writing 与书房共用的 Markdown 加载、路径映射和安全渲染。 */
(function () {
'use strict';
const siteRoot = new URL('./', document.currentScript.src);
const validSource = source => typeof source === 'string'
&& /^content\/[\w\u0080-\uFFFF/.-]+\.md$/.test(source)
&& !source.split('/').some(part => part === '.' || part === '..');
const escapeHtml = value => String(value || '').replace(/[&<>"']/g, char => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[char]));
function renderMarkdownSafely(markdown) {
if (window.marked && window.DOMPurify) return window.DOMPurify.sanitize(window.marked.parse(String(markdown || '')));
return '<pre>' + escapeHtml(markdown) + '</pre>';
}
function articleHref(src) { return new URL('article.html?src=' + encodeURIComponent(src), siteRoot).href; }
let bundlePromise;
function ensureBundle() {
if (window.ALEKSI_MARKDOWN_BUNDLE) return Promise.resolve();
if (!bundlePromise) bundlePromise = new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = new URL('content/content-bundle.js', siteRoot).href;
script.onload = resolve;
script.onerror = () => { bundlePromise = null; script.remove(); reject(new Error('文章暂时无法打开,请稍后重试。')); };
document.head.append(script);
});
return bundlePromise;
}
function normalizePath(path) {
const parts = String(path || '').replace(/\\/g, '/').split('/');
const stack = [];
parts.forEach((part) => {
if (!part || part === '.') return;
if (part === '..') stack.pop();
else stack.push(part);
});
return stack.join('/');
}
function decodeHashUnicode(path) {
return String(path || '').replace(/#U([0-9a-fA-F]{4,6})/g, (_, hex) => {
try {
return String.fromCodePoint(parseInt(hex, 16));
} catch (error) {
return _;
}
});
}
function encodeCjkHashUnicode(path) {
return String(path || '').replace(/[^\x00-\x7F]/g, (char) => {
return `#U${char.codePointAt(0).toString(16)}`;
});
}
function safeDecodeURIComponent(value) {
try {
return decodeURIComponent(value);
} catch (error) {
return value;
}
}
function getMarkdownKeys() {
return Object.keys(window.ALEKSI_MARKDOWN_BUNDLE || {});
}
function resolveMarkdownSource(src) {
const decodedInput = safeDecodeURIComponent(src || '').replace(/\\/g, '/');
const normalized = normalizePath(decodedInput);
// Old links sometimes contain only a filename. A complete source path must
// never resolve to an unrelated article merely because its filename matches.
const legacyLeaf = Boolean(decodedInput) && !decodedInput.includes('/');
const bundle = window.ALEKSI_MARKDOWN_BUNDLE || {};
const keys = getMarkdownKeys();
const candidates = [
normalized,
normalizePath(decodeHashUnicode(normalized)),
normalizePath(encodeCjkHashUnicode(normalized)),
normalizePath(encodeCjkHashUnicode(decodeHashUnicode(normalized)))
].filter(Boolean);
for (const candidate of candidates) {
if (bundle[candidate]) return candidate;
}
const decodedTarget = normalizePath(decodeHashUnicode(normalized));
const encodedTarget = normalizePath(encodeCjkHashUnicode(decodedTarget));
const matchedKey = keys.find((key) => {
const decodedKey = normalizePath(decodeHashUnicode(key));
return key === normalized
|| key === encodedTarget
|| decodedKey === decodedTarget
|| (legacyLeaf && decodedKey.endsWith(`/${decodedTarget}`));
});
if (matchedKey) return matchedKey;
const markdownIndex = window.ALEKSI_JSON_BUNDLE && window.ALEKSI_JSON_BUNDLE['content/markdown-index.json'];
const indexFiles = markdownIndex && Array.isArray(markdownIndex.files) ? markdownIndex.files : [];
const indexed = indexFiles.find((item) => {
const source = normalizePath(item.source || '');
const decodedSource = normalizePath(decodeHashUnicode(source));
return source === normalized || decodedSource === decodedTarget || source === encodedTarget;
});
if (indexed && bundle[indexed.source]) return indexed.source;
return normalized;
}
function renderArticleBody(markdown, src) {
// Parse first: reference links and HTML receive the same path handling, while
// literal Markdown inside code remains untouched.
const article = document.createElement('div');
article.innerHTML = renderMarkdownSafely(renderCallouts(String(markdown || '')));
const base = new URL(src ? resolveMarkdownSource(src) : './', siteRoot);
article.querySelectorAll('a[href], img[src]').forEach(element => {
const attribute = element.tagName === 'A' ? 'href' : 'src';
const target = element.getAttribute(attribute);
if (!target || target.startsWith('#')) return;
let resolved;
try { resolved = new URL(decodeHashUnicode(target), base); } catch (_) { return; }
if (attribute === 'href' && resolved.origin === siteRoot.origin
&& resolved.pathname.startsWith(siteRoot.pathname) && /\.md$/i.test(resolved.pathname)) {
const source = safeDecodeURIComponent(resolved.pathname.slice(siteRoot.pathname.length));
if (validSource(source)) {
element.setAttribute(attribute, articleHref(source) + resolved.hash);
return;
}
}
element.setAttribute(attribute, resolved.href);
});
return article.innerHTML;
}
async function loadMarkdown(src) {
const normalized = resolveMarkdownSource(src);
if (!validSource(normalized)) throw new Error('文章路径无效。');
if (location.protocol === 'file:') await ensureBundle();
const bundled = window.ALEKSI_MARKDOWN_BUNDLE?.[normalized];
if (typeof bundled === 'string') return bundled;
try {
const response = await fetch(new URL(normalized, siteRoot));
if (!response.ok) throw new Error('Missing article');
return await response.text();
} catch (error) {
await ensureBundle();
const offline = window.ALEKSI_MARKDOWN_BUNDLE?.[normalized];
if (typeof offline === 'string') return offline;
throw new Error('文章暂时无法打开,请稍后重试。');
}
}
function parseFrontmatter(markdown) {
if (!markdown.startsWith('---')) return { meta: {}, body: markdown };
const end = markdown.indexOf('\n---', 3);
if (end === -1) return { meta: {}, body: markdown };
const rawMeta = markdown.slice(3, end).trim().split(/\r?\n/);
const body = markdown.slice(end + 4).trim();
const meta = {};
let currentKey = null;
rawMeta.forEach((line) => {
const listItem = line.match(/^\s*-\s+(.+)$/);
if (listItem && currentKey) {
meta[currentKey] = Array.isArray(meta[currentKey]) ? meta[currentKey] : [];
meta[currentKey].push(listItem[1].trim());
return;
}
const pair = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
if (!pair) return;
currentKey = pair[1];
const value = pair[2].trim();
meta[currentKey] = value || [];
});
return { meta, body };
}
function renderCallouts(markdown) {
const types = ['definition', 'proof', 'error', 'toolbox', 'revision'];
return markdown.replace(/:::(definition|proof|error|toolbox|revision)\s*([\s\S]*?):::/g, (_, type, body) => {
const label = types.includes(type) ? type : 'revision';
return `\n<section class="callout callout-${label}">\n<p class="callout-label">${label}</p>\n${body.trim()}\n</section>\n`;
});
}
function stripLeadingTitleHeading(body) { return String(body || '').replace(/^\s*#\s+[^\n]+\n+/, ''); }
window.AleksiArticleContent = Object.freeze({ siteRoot: siteRoot.href, validSource,
loadMarkdown, parseFrontmatter, renderArticleBody, renderCallouts,
renderMarkdownSafely, stripLeadingTitleHeading, articleHref });
})();