hexo-theme-linen 1.0.50 → 1.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/layout/layout.ejs
CHANGED
|
@@ -21,7 +21,12 @@ const version = theme.version;
|
|
|
21
21
|
<meta name="robots" content="noindex, nofollow">
|
|
22
22
|
<% } %>
|
|
23
23
|
<meta name="description" content="<%=page?.excerpt||config?.description||''%>" />
|
|
24
|
-
<%-partial('partials/share-meta')%>
|
|
24
|
+
<%-partial('partials/share-meta')%>
|
|
25
|
+
<% const latexCssUrl = getLatexCssUrl(body); %>
|
|
26
|
+
<% if (latexCssUrl) { %>
|
|
27
|
+
<link rel="stylesheet" href="<%=latexCssUrl%>" />
|
|
28
|
+
<% } %>
|
|
29
|
+
<% if (theme.stylesheets !== undefined
|
|
25
30
|
&& theme.stylesheets.length > 0) { %>
|
|
26
31
|
<% if (is_post()) { %>
|
|
27
32
|
<link rel="stylesheet" href="<%=url_for(`/linen-theme/css/linkcard.css?v=${version}`)%>" />
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const text = content
|
|
4
4
|
.replace(/<figure[^>]*>.*?<\/figure>/gs, "")
|
|
5
5
|
.replace(/<span class=\"pswp-caption-content\">.*?<\/span>/gs, "")
|
|
6
|
+
.replace(/<span class=\"katex-mathml\">.*?<\/span>/gs, "")
|
|
6
7
|
.replace(/<noscript[^>]*>.*?<\/noscript>/gs, "")
|
|
7
8
|
.replace(/<style[^>]*>.*?<\/style>/gs, "")
|
|
8
9
|
.replace(/<script[^>]*>.*?<\/script>/gs, "")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hexo-theme-linen",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.51",
|
|
4
4
|
"description": "A clean Hexo theme",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"homepage": "https://github.com/lynanbreeze/hexo-theme-linen",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"hexo-renderer-dartsass": "^1.1.0",
|
|
26
26
|
"hexo-renderer-ejs": "^2.0.0",
|
|
27
27
|
"hexo-util": "^4.0.0",
|
|
28
|
+
"katex": "^0.19.1",
|
|
28
29
|
"moment": "^2.30.1"
|
|
29
30
|
},
|
|
30
31
|
"engines": {
|
package/scripts/helpers/tools.js
CHANGED
|
@@ -208,6 +208,9 @@ hexo.extend.helper.register("extractTocFromHtml", function (htmlString) {
|
|
|
208
208
|
let id = match[2]; // 已存在的 id
|
|
209
209
|
let content = match[3].trim(); // 提取标签内容
|
|
210
210
|
|
|
211
|
+
// 移除 KaTeX 隐藏的 MathML,避免纯文本提取出现重复公式内容
|
|
212
|
+
content = content.replace(/<span class="katex-mathml">[\s\S]*?<\/span>/g, "");
|
|
213
|
+
|
|
211
214
|
// 去除所有 HTML 标签,提取纯文本内容
|
|
212
215
|
content = content.replace(/<[^>]*>/g, "");
|
|
213
216
|
|
package/scripts/latex.js
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const katex = require("katex");
|
|
6
|
+
const { url_for } = require("hexo-util");
|
|
7
|
+
|
|
8
|
+
const katexPkg = require("katex/package.json");
|
|
9
|
+
const katexDistDir = path.dirname(require.resolve("katex/dist/katex.min.css"));
|
|
10
|
+
|
|
11
|
+
const DEFAULT_CSS_PATH = "linen-theme/katex/katex.min.css";
|
|
12
|
+
const DEFAULT_FONTS_DIR = "linen-theme/katex/fonts";
|
|
13
|
+
|
|
14
|
+
const renderStore = new WeakMap();
|
|
15
|
+
let nonceCounter = 0;
|
|
16
|
+
|
|
17
|
+
function getThemeLatexConfig(hexo) {
|
|
18
|
+
const raw = hexo?.config?.theme_config?.latex;
|
|
19
|
+
if (raw === true) {
|
|
20
|
+
return { enable: true, css: "", options: {} };
|
|
21
|
+
}
|
|
22
|
+
if (raw && typeof raw === "object") {
|
|
23
|
+
return {
|
|
24
|
+
enable: raw.enable === true,
|
|
25
|
+
css: typeof raw.css === "string" ? raw.css.trim() : "",
|
|
26
|
+
options:
|
|
27
|
+
raw.options && typeof raw.options === "object" ? raw.options : {},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return { enable: false, css: "", options: {} };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isLatexEnabled(data, hexo) {
|
|
34
|
+
if (!data) return false;
|
|
35
|
+
|
|
36
|
+
if (data.latex === false || data.math === false) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (data.latex === true || data.math === true) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (data.latex && typeof data.latex === "object") {
|
|
45
|
+
return data.latex.enable !== false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (data.math && typeof data.math === "object") {
|
|
49
|
+
return data.math.enable !== false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return getThemeLatexConfig(hexo).enable === true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getPageLatexOptions(data, hexo) {
|
|
56
|
+
const globalOpts = getThemeLatexConfig(hexo).options || {};
|
|
57
|
+
const pageConfig =
|
|
58
|
+
(data?.latex && typeof data.latex === "object" ? data.latex : null) ||
|
|
59
|
+
(data?.math && typeof data.math === "object" ? data.math : null) ||
|
|
60
|
+
{};
|
|
61
|
+
const pageOpts =
|
|
62
|
+
pageConfig.options && typeof pageConfig.options === "object"
|
|
63
|
+
? pageConfig.options
|
|
64
|
+
: pageConfig;
|
|
65
|
+
|
|
66
|
+
const merged = {
|
|
67
|
+
throwOnError: false,
|
|
68
|
+
strict: "ignore",
|
|
69
|
+
...globalOpts,
|
|
70
|
+
...pageOpts,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
delete merged.enable;
|
|
74
|
+
delete merged.css;
|
|
75
|
+
delete merged.options;
|
|
76
|
+
|
|
77
|
+
if (globalOpts.macros || pageOpts.macros) {
|
|
78
|
+
merged.macros = {
|
|
79
|
+
...(globalOpts.macros || {}),
|
|
80
|
+
...(pageOpts.macros || {}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return merged;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function stripLegacyFontFormats(css) {
|
|
88
|
+
return css.replace(
|
|
89
|
+
/,url\([^)]+\.(?:woff|ttf)\)\s*format\(["'](?:woff|truetype)["']\)/g,
|
|
90
|
+
"",
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function hasKatexMarkup(text) {
|
|
95
|
+
return typeof text === "string" && text.includes('class="katex"');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function createNonce(source) {
|
|
99
|
+
let nonce = `N${(++nonceCounter).toString(36)}`;
|
|
100
|
+
while (source.includes(`LINENKATEX${nonce}`)) {
|
|
101
|
+
nonce = `N${(++nonceCounter).toString(36)}`;
|
|
102
|
+
}
|
|
103
|
+
return nonce;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function extractBlockquotePrefix(linePrefix) {
|
|
107
|
+
if (!linePrefix.includes(">")) return "";
|
|
108
|
+
return linePrefix;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function stripBlockquotePrefix(expr, linePrefix) {
|
|
112
|
+
const quoteCount = (linePrefix.match(/>/g) || []).length;
|
|
113
|
+
if (quoteCount === 0 || !expr.includes("\n")) return expr;
|
|
114
|
+
|
|
115
|
+
const prefixRegex = new RegExp(`^(?:[ \\t]*>[ \\t]?){1,${quoteCount}}`);
|
|
116
|
+
return expr
|
|
117
|
+
.split("\n")
|
|
118
|
+
.map((line, index) => (index === 0 ? line : line.replace(prefixRegex, "")))
|
|
119
|
+
.join("\n");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function matchFencedCodeStart(src, lineStartIndex) {
|
|
123
|
+
const lineEnd = src.indexOf("\n", lineStartIndex);
|
|
124
|
+
const end = lineEnd === -1 ? src.length : lineEnd;
|
|
125
|
+
const line = src.slice(lineStartIndex, end).replace(/\r$/, "");
|
|
126
|
+
|
|
127
|
+
const match = line.match(/^((?:[ \t]*>[ \t]?)*[ \t]*)([`~]{3,})(.*)$/);
|
|
128
|
+
if (!match) return null;
|
|
129
|
+
|
|
130
|
+
const fence = match[2];
|
|
131
|
+
const fenceChar = fence[0];
|
|
132
|
+
const info = match[3];
|
|
133
|
+
|
|
134
|
+
// CommonMark: backtick fence info string cannot contain backticks
|
|
135
|
+
if (fenceChar === "`" && info.includes("`")) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
fenceChar,
|
|
141
|
+
fenceLen: fence.length,
|
|
142
|
+
nextLineStart: lineEnd === -1 ? src.length : lineEnd + 1,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function findFencedCodeEnd(src, startIndex, fenceChar, fenceLen) {
|
|
147
|
+
let pos = startIndex;
|
|
148
|
+
const len = src.length;
|
|
149
|
+
const closeRegex = new RegExp(
|
|
150
|
+
`^(?:[ \\t]*>[ \\t]?)*[ \\t]*\\${fenceChar}{${fenceLen},}[ \\t]*$`,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
while (pos < len) {
|
|
154
|
+
const lineEnd = src.indexOf("\n", pos);
|
|
155
|
+
const end = lineEnd === -1 ? len : lineEnd;
|
|
156
|
+
const line = src.slice(pos, end).replace(/\r$/, "");
|
|
157
|
+
const nextPos = lineEnd === -1 ? len : lineEnd + 1;
|
|
158
|
+
|
|
159
|
+
if (closeRegex.test(line)) {
|
|
160
|
+
return nextPos;
|
|
161
|
+
}
|
|
162
|
+
pos = nextPos;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return len;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function findInlineCodeEnd(src, startIndex) {
|
|
169
|
+
const len = src.length;
|
|
170
|
+
let tickLen = 0;
|
|
171
|
+
while (startIndex + tickLen < len && src[startIndex + tickLen] === "`") {
|
|
172
|
+
tickLen++;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
let j = startIndex + tickLen;
|
|
176
|
+
while (j < len) {
|
|
177
|
+
if (src[j] === "`") {
|
|
178
|
+
let run = 0;
|
|
179
|
+
while (j + run < len && src[j + run] === "`") {
|
|
180
|
+
run++;
|
|
181
|
+
}
|
|
182
|
+
if (run === tickLen) {
|
|
183
|
+
return j + run;
|
|
184
|
+
}
|
|
185
|
+
j += run;
|
|
186
|
+
} else {
|
|
187
|
+
j++;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return -1;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function findHtmlSkipBlockEnd(src, startIndex) {
|
|
195
|
+
const slice = src.slice(startIndex);
|
|
196
|
+
const openMatch = slice.match(/^<(pre|code|script|style)(?=[\s>/])/i);
|
|
197
|
+
if (!openMatch) return -1;
|
|
198
|
+
|
|
199
|
+
const tagName = openMatch[1];
|
|
200
|
+
const openTagEnd = slice.indexOf(">");
|
|
201
|
+
if (openTagEnd === -1) return -1;
|
|
202
|
+
|
|
203
|
+
// Self-closing tag
|
|
204
|
+
if (slice[openTagEnd - 1] === "/") {
|
|
205
|
+
return startIndex + openTagEnd + 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const closeRegex = new RegExp(`</${tagName}\\s*>`, "i");
|
|
209
|
+
const closeMatch = closeRegex.exec(src.slice(startIndex + openTagEnd + 1));
|
|
210
|
+
if (!closeMatch) return src.length;
|
|
211
|
+
|
|
212
|
+
return startIndex + openTagEnd + 1 + closeMatch.index + closeMatch[0].length;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function findHexoTagSkipBlockEnd(src, startIndex) {
|
|
216
|
+
const slice = src.slice(startIndex);
|
|
217
|
+
const openMatch = slice.match(/^\{%\s*(codeblock|raw)\b[\s\S]*?%\}/i);
|
|
218
|
+
if (!openMatch) return -1;
|
|
219
|
+
|
|
220
|
+
const tagName = openMatch[1];
|
|
221
|
+
const closeRegex = new RegExp(`\\{%\\s*end${tagName}\\s*%\\}`, "i");
|
|
222
|
+
const afterOpen = startIndex + openMatch[0].length;
|
|
223
|
+
const closeMatch = closeRegex.exec(src.slice(afterOpen));
|
|
224
|
+
if (!closeMatch) return src.length;
|
|
225
|
+
|
|
226
|
+
return afterOpen + closeMatch.index + closeMatch[0].length;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function renderLatexPlaceholders(source, katexOptions = {}) {
|
|
230
|
+
if (typeof source !== "string" || !source.includes("$")) {
|
|
231
|
+
return { content: source, placeholders: [], nonce: "" };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const nonce = createNonce(source);
|
|
235
|
+
const placeholders = [];
|
|
236
|
+
const len = source.length;
|
|
237
|
+
let out = "";
|
|
238
|
+
let i = 0;
|
|
239
|
+
let atLineStart = true;
|
|
240
|
+
|
|
241
|
+
while (i < len) {
|
|
242
|
+
// 1. Fenced code blocks
|
|
243
|
+
if (atLineStart) {
|
|
244
|
+
const fenceInfo = matchFencedCodeStart(source, i);
|
|
245
|
+
if (fenceInfo) {
|
|
246
|
+
const end = findFencedCodeEnd(
|
|
247
|
+
source,
|
|
248
|
+
fenceInfo.nextLineStart,
|
|
249
|
+
fenceInfo.fenceChar,
|
|
250
|
+
fenceInfo.fenceLen,
|
|
251
|
+
);
|
|
252
|
+
out += source.slice(i, end);
|
|
253
|
+
i = end;
|
|
254
|
+
atLineStart = true;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const ch = source[i];
|
|
260
|
+
|
|
261
|
+
// 2. Inline code spans
|
|
262
|
+
if (ch === "`") {
|
|
263
|
+
const end = findInlineCodeEnd(source, i);
|
|
264
|
+
if (end !== -1) {
|
|
265
|
+
out += source.slice(i, end);
|
|
266
|
+
i = end;
|
|
267
|
+
atLineStart = source[i - 1] === "\n";
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
let run = 0;
|
|
271
|
+
while (i + run < len && source[i + run] === "`") run++;
|
|
272
|
+
out += source.slice(i, i + run);
|
|
273
|
+
i += run;
|
|
274
|
+
atLineStart = false;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// 3. Raw HTML pre/code/script/style blocks
|
|
279
|
+
if (ch === "<") {
|
|
280
|
+
const end = findHtmlSkipBlockEnd(source, i);
|
|
281
|
+
if (end !== -1) {
|
|
282
|
+
out += source.slice(i, end);
|
|
283
|
+
i = end;
|
|
284
|
+
atLineStart = source[i - 1] === "\n";
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// 4. Hexo {% codeblock %} / {% raw %} tags
|
|
290
|
+
if (ch === "{" && source[i + 1] === "%") {
|
|
291
|
+
const end = findHexoTagSkipBlockEnd(source, i);
|
|
292
|
+
if (end !== -1) {
|
|
293
|
+
out += source.slice(i, end);
|
|
294
|
+
i = end;
|
|
295
|
+
atLineStart = source[i - 1] === "\n";
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 5. Backslash escapes (\\ and \$)
|
|
301
|
+
if (ch === "\\" && i + 1 < len) {
|
|
302
|
+
const next = source[i + 1];
|
|
303
|
+
if (next === "\\" || next === "$") {
|
|
304
|
+
out += source.slice(i, i + 2);
|
|
305
|
+
i += 2;
|
|
306
|
+
atLineStart = false;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 6. Display math $$...$$
|
|
312
|
+
if (ch === "$" && source[i + 1] === "$") {
|
|
313
|
+
let j = i + 2;
|
|
314
|
+
let closeIdx = -1;
|
|
315
|
+
|
|
316
|
+
while (j < len) {
|
|
317
|
+
if (source[j] === "\\" && j + 1 < len) {
|
|
318
|
+
j += 2;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (source[j] === "$" && source[j + 1] === "$") {
|
|
322
|
+
closeIdx = j;
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
j++;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (closeIdx !== -1) {
|
|
329
|
+
const lineStart = source.lastIndexOf("\n", i - 1) + 1;
|
|
330
|
+
const beforeOnLine = source.slice(lineStart, i);
|
|
331
|
+
const afterEnd = source.indexOf("\n", closeIdx + 2);
|
|
332
|
+
const afterOnLine = source.slice(
|
|
333
|
+
closeIdx + 2,
|
|
334
|
+
afterEnd === -1 ? len : afterEnd,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
const isBlankBefore = /^(?:[ \t]*>[ \t]?)*[ \t]*$/.test(beforeOnLine);
|
|
338
|
+
const isBlankAfter = /^[ \t]*\r?$/.test(afterOnLine);
|
|
339
|
+
|
|
340
|
+
const rawExpr = source.slice(i + 2, closeIdx);
|
|
341
|
+
const cleanedExpr = isBlankBefore
|
|
342
|
+
? stripBlockquotePrefix(rawExpr, extractBlockquotePrefix(beforeOnLine))
|
|
343
|
+
: rawExpr;
|
|
344
|
+
const expr = cleanedExpr.trim();
|
|
345
|
+
|
|
346
|
+
if (expr.length > 0) {
|
|
347
|
+
const html = katex.renderToString(expr, {
|
|
348
|
+
...katexOptions,
|
|
349
|
+
displayMode: true,
|
|
350
|
+
});
|
|
351
|
+
const idx = placeholders.length;
|
|
352
|
+
const isStandalone = isBlankBefore && isBlankAfter;
|
|
353
|
+
placeholders.push({ html, block: isStandalone });
|
|
354
|
+
out += isStandalone
|
|
355
|
+
? `LINENKATEX${nonce}B${idx}END`
|
|
356
|
+
: `LINENKATEX${nonce}I${idx}END`;
|
|
357
|
+
i = closeIdx + 2;
|
|
358
|
+
atLineStart = false;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
out += "$$";
|
|
364
|
+
i += 2;
|
|
365
|
+
atLineStart = false;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// 7. Inline math $...$ (single-line only)
|
|
370
|
+
if (ch === "$" && source[i + 1] !== "$") {
|
|
371
|
+
const validOpen =
|
|
372
|
+
i + 1 < len &&
|
|
373
|
+
!/\s/.test(source[i + 1]) &&
|
|
374
|
+
!(i > 0 && /[0-9]/.test(source[i - 1]));
|
|
375
|
+
|
|
376
|
+
if (validOpen) {
|
|
377
|
+
let j = i + 1;
|
|
378
|
+
let matched = false;
|
|
379
|
+
|
|
380
|
+
while (j < len) {
|
|
381
|
+
if (source[j] === "\\" && j + 1 < len) {
|
|
382
|
+
j += 2;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
// Inline math cannot cross line breaks
|
|
386
|
+
if (source[j] === "\n" || source[j] === "\r") {
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
if (source[j] === "$") {
|
|
390
|
+
const validClose =
|
|
391
|
+
j > i + 1 &&
|
|
392
|
+
source[j + 1] !== "$" &&
|
|
393
|
+
!/\s/.test(source[j - 1]) &&
|
|
394
|
+
!(j + 1 < len && /[0-9]/.test(source[j + 1]));
|
|
395
|
+
|
|
396
|
+
if (validClose) {
|
|
397
|
+
const expr = source.slice(i + 1, j);
|
|
398
|
+
const html = katex.renderToString(expr, {
|
|
399
|
+
...katexOptions,
|
|
400
|
+
displayMode: false,
|
|
401
|
+
});
|
|
402
|
+
const idx = placeholders.length;
|
|
403
|
+
placeholders.push({ html, block: false });
|
|
404
|
+
out += `LINENKATEX${nonce}I${idx}END`;
|
|
405
|
+
i = j + 1;
|
|
406
|
+
atLineStart = false;
|
|
407
|
+
matched = true;
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
j++;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (matched) {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
out += ch;
|
|
421
|
+
atLineStart = ch === "\n";
|
|
422
|
+
i++;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return { content: out, placeholders, nonce };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function restoreLatexPlaceholders(html, state) {
|
|
429
|
+
if (!state || !state.placeholders.length || typeof html !== "string") {
|
|
430
|
+
return html;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const { placeholders, nonce } = state;
|
|
434
|
+
|
|
435
|
+
// Unwrap standalone display equations wrapped in <p>...</p> by Markdown
|
|
436
|
+
const blockInP = new RegExp(
|
|
437
|
+
`<p>\\s*LINENKATEX${nonce}B(\\d+)END\\s*</p>`,
|
|
438
|
+
"g",
|
|
439
|
+
);
|
|
440
|
+
let result = html.replace(
|
|
441
|
+
blockInP,
|
|
442
|
+
(_, idx) => placeholders[Number(idx)]?.html ?? "",
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
// Restore any remaining block placeholders (e.g. inside lists or blockquotes)
|
|
446
|
+
const blockToken = new RegExp(`LINENKATEX${nonce}B(\\d+)END`, "g");
|
|
447
|
+
result = result.replace(
|
|
448
|
+
blockToken,
|
|
449
|
+
(_, idx) => placeholders[Number(idx)]?.html ?? "",
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
// Restore inline placeholders
|
|
453
|
+
const inlineToken = new RegExp(`LINENKATEX${nonce}I(\\d+)END`, "g");
|
|
454
|
+
result = result.replace(
|
|
455
|
+
inlineToken,
|
|
456
|
+
(_, idx) => placeholders[Number(idx)]?.html ?? "",
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
return result;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function register(hexo) {
|
|
463
|
+
hexo.extend.filter.register(
|
|
464
|
+
"before_post_render",
|
|
465
|
+
function (data) {
|
|
466
|
+
if (!isLatexEnabled(data, hexo)) {
|
|
467
|
+
return data;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const options = getPageLatexOptions(data, hexo);
|
|
471
|
+
const result = renderLatexPlaceholders(data.content, options);
|
|
472
|
+
if (result.placeholders.length > 0) {
|
|
473
|
+
data.content = result.content;
|
|
474
|
+
renderStore.set(data, result);
|
|
475
|
+
}
|
|
476
|
+
return data;
|
|
477
|
+
},
|
|
478
|
+
0,
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
hexo.extend.filter.register(
|
|
482
|
+
"after_post_render",
|
|
483
|
+
function (data) {
|
|
484
|
+
const state = renderStore.get(data);
|
|
485
|
+
if (!state) return data;
|
|
486
|
+
|
|
487
|
+
renderStore.delete(data);
|
|
488
|
+
data.content = restoreLatexPlaceholders(data.content, state);
|
|
489
|
+
return data;
|
|
490
|
+
},
|
|
491
|
+
1,
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
hexo.extend.helper.register("getLatexCssUrl", function (body) {
|
|
495
|
+
if (!hasKatexMarkup(body)) {
|
|
496
|
+
return "";
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const { css } = getThemeLatexConfig(hexo);
|
|
500
|
+
if (css) {
|
|
501
|
+
if (/^(https?:)?\/\//i.test(css)) {
|
|
502
|
+
return css;
|
|
503
|
+
}
|
|
504
|
+
return url_for.call(hexo, css);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return url_for.call(hexo, `/${DEFAULT_CSS_PATH}?v=${katexPkg.version}`);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
hexo.extend.generator.register("linen-latex-assets", function (locals) {
|
|
511
|
+
const { css } = getThemeLatexConfig(hexo);
|
|
512
|
+
if (css) {
|
|
513
|
+
return [];
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const posts = locals.posts ? locals.posts.toArray() : [];
|
|
517
|
+
const pages = locals.pages ? locals.pages.toArray() : [];
|
|
518
|
+
const siteUsesKatex = [...posts, ...pages].some(
|
|
519
|
+
(item) => hasKatexMarkup(item.content) || hasKatexMarkup(item.excerpt),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
if (!siteUsesKatex) {
|
|
523
|
+
return [];
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const cssPath = path.join(katexDistDir, "katex.min.css");
|
|
527
|
+
const fontsDir = path.join(katexDistDir, "fonts");
|
|
528
|
+
const cssContent = stripLegacyFontFormats(fs.readFileSync(cssPath, "utf8"));
|
|
529
|
+
|
|
530
|
+
const assets = [
|
|
531
|
+
{
|
|
532
|
+
path: DEFAULT_CSS_PATH,
|
|
533
|
+
data: cssContent,
|
|
534
|
+
},
|
|
535
|
+
];
|
|
536
|
+
|
|
537
|
+
const fontFiles = fs
|
|
538
|
+
.readdirSync(fontsDir)
|
|
539
|
+
.filter((file) => file.endsWith(".woff2"));
|
|
540
|
+
|
|
541
|
+
for (const file of fontFiles) {
|
|
542
|
+
assets.push({
|
|
543
|
+
path: `${DEFAULT_FONTS_DIR}/${file}`,
|
|
544
|
+
data: fs.readFileSync(path.join(fontsDir, file)),
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return assets;
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (typeof hexo !== "undefined") {
|
|
553
|
+
register(hexo);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
module.exports = {
|
|
557
|
+
register,
|
|
558
|
+
isLatexEnabled,
|
|
559
|
+
getPageLatexOptions,
|
|
560
|
+
renderLatexPlaceholders,
|
|
561
|
+
restoreLatexPlaceholders,
|
|
562
|
+
stripLegacyFontFormats,
|
|
563
|
+
hasKatexMarkup,
|
|
564
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
$font-version: "
|
|
1
|
+
$font-version: "231e72ec";
|
|
@@ -3985,6 +3985,22 @@ ul {
|
|
|
3985
3985
|
}
|
|
3986
3986
|
}
|
|
3987
3987
|
|
|
3988
|
+
.katex {
|
|
3989
|
+
font-size: 1.08em;
|
|
3990
|
+
}
|
|
3991
|
+
|
|
3992
|
+
.katex-display {
|
|
3993
|
+
margin: 1.1em 0;
|
|
3994
|
+
padding: 0.35em 0;
|
|
3995
|
+
overflow-x: auto;
|
|
3996
|
+
overflow-y: hidden;
|
|
3997
|
+
-webkit-overflow-scrolling: touch;
|
|
3998
|
+
}
|
|
3999
|
+
|
|
4000
|
+
.post-item .excerpt .katex-display {
|
|
4001
|
+
margin: 0.5em 0;
|
|
4002
|
+
}
|
|
4003
|
+
|
|
3988
4004
|
@include mq.rules;
|
|
3989
4005
|
|
|
3990
4006
|
@media (max-width: 900px) {
|