@wenyan-md/core 1.0.12 → 1.0.14
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/README.md +17 -2
- package/dist/atom-one-dark.min-Du_gemwz.js +4 -0
- package/dist/atom-one-light.min-BameqVOr.js +4 -0
- package/dist/browser/wenyan-core.js +53 -53
- package/dist/core.js +431 -272
- package/dist/default-CE6XHgHa.js +4 -0
- package/dist/dracula.min-DhYK88zj.js +4 -0
- package/dist/github-dark.min-rUG1U3df.js +4 -0
- package/dist/github.min-DEfTaph9.js +4 -0
- package/dist/hltheme.js +29 -22
- package/dist/juejin_default-B6AI_viJ.js +4 -0
- package/dist/lapis-BpjqywsV.js +4 -0
- package/dist/maize-BqG2l24H.js +4 -0
- package/dist/math/wenyan-math.js +9 -40
- package/dist/medium_default-DtspuXEE.js +4 -0
- package/dist/monokai.min-BszscX_9.js +4 -0
- package/dist/orangeheart-CWqZ_gXQ.js +4 -0
- package/dist/{phycat-C5ES4IMt.js → phycat-BNJAZy4V.js} +2 -6
- package/dist/pie-CVpIzs7v.js +4 -0
- package/dist/publish.js +137 -345
- package/dist/purple-ObLhqJQS.js +4 -0
- package/dist/rainbow-C5PT3WBL.js +4 -0
- package/dist/solarized-dark.min-mpQq1Jcj.js +4 -0
- package/dist/solarized-light.min-C5Ersr9g.js +4 -0
- package/dist/styles/wenyan-styles.js +21 -40
- package/dist/theme.js +40 -33
- package/dist/toutiao_default-BQ7BN6_-.js +4 -0
- package/dist/types/core.d.ts +17 -0
- package/dist/types/publish.d.ts +1 -1
- package/dist/types/wechatApi.d.ts +1 -1
- package/dist/types/wrapper.d.ts +3 -3
- package/dist/wrapper.js +15 -10
- package/dist/xcode.min-DWdHLdoo.js +4 -0
- package/dist/zhihu_default-DUBy_Cqy.js +4 -0
- package/package.json +5 -3
- package/dist/atom-one-dark.min-hABhDLRj.js +0 -4
- package/dist/atom-one-light.min-CwiVhPEv.js +0 -4
- package/dist/default-D-dyLptq.js +0 -184
- package/dist/dracula.min-OeyC4Nkp.js +0 -11
- package/dist/github-dark.min-DOlD5Ewr.js +0 -13
- package/dist/github.min-BZ2GvPsn.js +0 -14
- package/dist/juejin_default-CnUAGV72.js +0 -139
- package/dist/lapis-dRySN5pK.js +0 -194
- package/dist/maize-DFW0x6O3.js +0 -194
- package/dist/medium_default-BXHGUKdY.js +0 -135
- package/dist/monokai.min-CH2iHqHz.js +0 -4
- package/dist/orangeheart-Da7uQj8U.js +0 -184
- package/dist/pie-DUYodGli.js +0 -240
- package/dist/purple-Da1-Vfos.js +0 -183
- package/dist/rainbow-BtrbZco6.js +0 -167
- package/dist/solarized-dark.min-BxbYljx4.js +0 -11
- package/dist/solarized-light.min-Bb25xgOC.js +0 -11
- package/dist/toutiao_default-DthFV93V.js +0 -151
- package/dist/xcode.min-CG-lWQgl.js +0 -4
- package/dist/zhihu_default-7uVAZjKn.js +0 -138
package/dist/core.js
CHANGED
|
@@ -1,350 +1,509 @@
|
|
|
1
|
-
import { marked
|
|
2
|
-
import { markedHighlight
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import * as
|
|
6
|
-
import { mathjax
|
|
7
|
-
import { TeX
|
|
8
|
-
import { SVG
|
|
9
|
-
import { liteAdaptor
|
|
10
|
-
import { RegisterHTMLHandler
|
|
11
|
-
import { AllPackages
|
|
12
|
-
import { themes
|
|
13
|
-
import { hlThemes
|
|
14
|
-
const
|
|
15
|
-
inlineMath: [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { marked } from "marked";
|
|
2
|
+
import { markedHighlight } from "marked-highlight";
|
|
3
|
+
import hljs from "highlight.js";
|
|
4
|
+
import fm from "front-matter";
|
|
5
|
+
import * as csstree from "css-tree";
|
|
6
|
+
import { mathjax } from "mathjax-full/js/mathjax.js";
|
|
7
|
+
import { TeX } from "mathjax-full/js/input/tex.js";
|
|
8
|
+
import { SVG } from "mathjax-full/js/output/svg.js";
|
|
9
|
+
import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
|
|
10
|
+
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html.js";
|
|
11
|
+
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
|
|
12
|
+
import { themes } from "./theme.js";
|
|
13
|
+
import { hlThemes } from "./hltheme.js";
|
|
14
|
+
const texConfig = {
|
|
15
|
+
inlineMath: [
|
|
16
|
+
["$", "$"],
|
|
17
|
+
["\\(", "\\)"]
|
|
18
|
+
],
|
|
19
|
+
displayMath: [
|
|
20
|
+
["$$", "$$"],
|
|
21
|
+
["\\[", "\\]"]
|
|
22
|
+
],
|
|
23
|
+
processEscapes: true,
|
|
24
|
+
packages: AllPackages
|
|
25
|
+
};
|
|
26
|
+
const svgConfig = {
|
|
20
27
|
fontCache: "none"
|
|
21
28
|
};
|
|
22
|
-
let
|
|
23
|
-
if (!
|
|
24
|
-
|
|
29
|
+
let adaptor;
|
|
30
|
+
if (!adaptor) {
|
|
31
|
+
adaptor = liteAdaptor();
|
|
25
32
|
try {
|
|
26
|
-
|
|
27
|
-
} catch {
|
|
33
|
+
RegisterHTMLHandler(adaptor);
|
|
34
|
+
} catch (e) {
|
|
28
35
|
}
|
|
29
36
|
}
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
RegisterHTMLHandler(adaptor);
|
|
38
|
+
const tex = new TeX(texConfig);
|
|
39
|
+
const svg = new SVG(svgConfig);
|
|
40
|
+
function addContainer(math, doc) {
|
|
41
|
+
const tag = math.display ? "section" : "span";
|
|
42
|
+
const cls = math.display ? "block-equation" : "inline-equation";
|
|
43
|
+
const container = math.typesetRoot;
|
|
44
|
+
if (math.math) {
|
|
45
|
+
doc.adaptor.setAttribute(container, "math", math.math);
|
|
46
|
+
}
|
|
47
|
+
const node = doc.adaptor.node(tag, { class: cls }, [container]);
|
|
48
|
+
math.typesetRoot = node;
|
|
35
49
|
}
|
|
36
|
-
async function
|
|
50
|
+
async function renderMathInHtml(htmlString) {
|
|
37
51
|
try {
|
|
38
|
-
const
|
|
39
|
-
InputJax:
|
|
40
|
-
OutputJax:
|
|
52
|
+
const html = mathjax.document(htmlString, {
|
|
53
|
+
InputJax: tex,
|
|
54
|
+
OutputJax: svg,
|
|
41
55
|
renderActions: {
|
|
42
|
-
addContainer: [
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
addContainer: [
|
|
57
|
+
190,
|
|
58
|
+
(doc) => {
|
|
59
|
+
for (const math of doc.math) {
|
|
60
|
+
addContainer(math, doc);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
addContainer
|
|
64
|
+
]
|
|
46
65
|
}
|
|
47
66
|
});
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
return
|
|
51
|
-
} catch (
|
|
52
|
-
|
|
67
|
+
html.render();
|
|
68
|
+
const body = adaptor.body(html.document);
|
|
69
|
+
return adaptor.innerHTML(body);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error("Error rendering MathJax:", error);
|
|
72
|
+
throw error;
|
|
53
73
|
}
|
|
54
74
|
}
|
|
55
|
-
const
|
|
75
|
+
const macStyleCss = `#wenyan pre::before {
|
|
56
76
|
display: block;
|
|
57
77
|
content: "";
|
|
58
78
|
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="12" viewBox="0 0 450 130"><ellipse cx="65" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"/><ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"/><ellipse cx="385" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"/></svg>');
|
|
59
79
|
background-repeat: no-repeat;
|
|
60
80
|
width: 100%;
|
|
61
81
|
height: 16px;
|
|
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
|
-
|
|
82
|
+
}`;
|
|
83
|
+
const serif = "Georgia, Cambria, 'Noto Serif', 'Times New Roman', serif";
|
|
84
|
+
const sansSerif = "system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', 'Roboto', sans-serif";
|
|
85
|
+
const monospace = "Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace";
|
|
86
|
+
let markedConfigured = false;
|
|
87
|
+
let configurePromise = null;
|
|
88
|
+
async function configureMarked() {
|
|
89
|
+
if (markedConfigured) return;
|
|
90
|
+
if (configurePromise) return configurePromise;
|
|
91
|
+
configurePromise = (async () => {
|
|
92
|
+
const highlightExtension = markedHighlight({
|
|
93
|
+
emptyLangClass: "hljs",
|
|
94
|
+
langPrefix: "hljs language-",
|
|
95
|
+
highlight: function(code, lang, info) {
|
|
96
|
+
const language = hljs.getLanguage(lang) ? lang : "plaintext";
|
|
97
|
+
return hljs.highlight(code, { language }).value;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
marked.use(highlightExtension);
|
|
101
|
+
const attributeImageExtension = {
|
|
102
|
+
name: "attributeImage",
|
|
103
|
+
level: "inline",
|
|
104
|
+
start(src) {
|
|
105
|
+
return src.indexOf("![");
|
|
106
|
+
},
|
|
107
|
+
tokenizer(src) {
|
|
108
|
+
const rule = /^!\[([^\]]*)\]\(([^)]+)\)\{(.*?)\}/;
|
|
109
|
+
const match = rule.exec(src);
|
|
110
|
+
if (match) {
|
|
111
|
+
return {
|
|
112
|
+
type: "attributeImage",
|
|
113
|
+
raw: match[0],
|
|
114
|
+
alt: match[1],
|
|
115
|
+
href: match[2],
|
|
116
|
+
attrs: match[3]
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
renderer(token) {
|
|
121
|
+
const attrs = stringToMap(token.attrs);
|
|
122
|
+
const attrStr = Array.from(attrs).map(
|
|
123
|
+
([k, v]) => /^\d+$/.test(v) ? `${k}:${v}px` : `${k}:${v}`
|
|
124
|
+
).join("; ");
|
|
125
|
+
return `<img src="${token.href}" alt="${token.alt || ""}" title="${token.alt || ""}" style="${attrStr}">`;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
marked.use({ extensions: [attributeImageExtension] });
|
|
129
|
+
const renderer = marked.Renderer;
|
|
130
|
+
const parser = marked.Parser;
|
|
131
|
+
renderer.heading = function(heading) {
|
|
132
|
+
const text = parser.parseInline(heading.tokens);
|
|
133
|
+
const level = heading.depth;
|
|
134
|
+
return `<h${level}><span>${text}</span></h${level}>
|
|
135
|
+
`;
|
|
136
|
+
};
|
|
137
|
+
renderer.paragraph = function(paragraph) {
|
|
138
|
+
const text = paragraph.text;
|
|
139
|
+
if (text.length > 4 && (/\$\$[\s\S]*?\$\$/g.test(text) || /\\\[[\s\S]*?\\\]/g.test(text))) {
|
|
140
|
+
return `${text}
|
|
102
141
|
`;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return s.length > 4 && (/\$\$[\s\S]*?\$\$/g.test(s) || /\\\[[\s\S]*?\\\]/g.test(s)) ? `${s}
|
|
106
|
-
` : `<p>${o.parseInline(r.tokens)}</p>
|
|
142
|
+
} else {
|
|
143
|
+
return `<p>${parser.parseInline(paragraph.tokens)}</p>
|
|
107
144
|
`;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
renderer.image = function(token, title, text) {
|
|
148
|
+
const src = token.href;
|
|
149
|
+
return `<img src="${src}" alt="${token.text || ""}" title="${token.text || ""}">`;
|
|
150
|
+
};
|
|
151
|
+
marked.use({ renderer });
|
|
152
|
+
markedConfigured = true;
|
|
153
|
+
})();
|
|
154
|
+
return configurePromise;
|
|
111
155
|
}
|
|
112
|
-
function
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
const [
|
|
116
|
-
|
|
117
|
-
|
|
156
|
+
function stringToMap(str) {
|
|
157
|
+
const map = /* @__PURE__ */ new Map();
|
|
158
|
+
str.split(/\s+/).forEach((pair) => {
|
|
159
|
+
const [key, value] = pair.split("=");
|
|
160
|
+
if (key && value) {
|
|
161
|
+
map.set(key, value.replace(/^["']|["']$/g, ""));
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return map;
|
|
118
165
|
}
|
|
119
|
-
function
|
|
120
|
-
const { attributes
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
166
|
+
async function handleFrontMatter(markdown) {
|
|
167
|
+
const { attributes, body } = fm(markdown);
|
|
168
|
+
const result = {};
|
|
169
|
+
let head = "";
|
|
170
|
+
const { title, description, cover } = attributes;
|
|
171
|
+
if (title) {
|
|
172
|
+
result.title = title;
|
|
173
|
+
}
|
|
174
|
+
if (description) {
|
|
175
|
+
head += "> " + description + "\n\n";
|
|
176
|
+
result.description = description;
|
|
177
|
+
}
|
|
178
|
+
if (cover) {
|
|
179
|
+
result.cover = cover;
|
|
180
|
+
}
|
|
181
|
+
result.body = head + body;
|
|
182
|
+
return result;
|
|
126
183
|
}
|
|
127
|
-
async function
|
|
128
|
-
|
|
129
|
-
|
|
184
|
+
async function renderMarkdown(content) {
|
|
185
|
+
await configureMarked();
|
|
186
|
+
const html = marked.parse(content);
|
|
187
|
+
const htmlWithMath = await renderMathInHtml(html);
|
|
188
|
+
return htmlWithMath;
|
|
130
189
|
}
|
|
131
|
-
async function
|
|
132
|
-
let
|
|
133
|
-
if (
|
|
134
|
-
|
|
135
|
-
|
|
190
|
+
async function getContentForGzhBuiltinTheme(wenyanElement, themeId, hlThemeId, isMacStyle = true, isAddFootnote = true) {
|
|
191
|
+
let theme = themes["default"];
|
|
192
|
+
if (themeId) {
|
|
193
|
+
theme = themes[themeId];
|
|
194
|
+
if (!theme) {
|
|
195
|
+
theme = Object.values(themes).find(
|
|
196
|
+
(t) => t.name.toLowerCase() === themeId.toLowerCase()
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (!theme) {
|
|
136
201
|
throw new Error("主题不存在");
|
|
137
|
-
|
|
202
|
+
}
|
|
203
|
+
if (!(hlThemeId in hlThemes)) {
|
|
138
204
|
throw new Error("代码块主题不存在");
|
|
139
|
-
|
|
140
|
-
|
|
205
|
+
}
|
|
206
|
+
const customCss = replaceCSSVariables(await theme.getCss());
|
|
207
|
+
const hlTheme = hlThemes[hlThemeId];
|
|
208
|
+
const highlightCss = await hlTheme.getCss();
|
|
209
|
+
return getContentForGzhCustomCss(wenyanElement, customCss, highlightCss, isMacStyle, isAddFootnote);
|
|
141
210
|
}
|
|
142
|
-
async function
|
|
143
|
-
|
|
211
|
+
async function getContentForGzhCustomCss(wenyanElement, customCss, highlightCss, isMacStyle = true, isAddFootnote = true) {
|
|
212
|
+
if (isAddFootnote) {
|
|
213
|
+
addFootnotes(false, wenyanElement);
|
|
214
|
+
}
|
|
215
|
+
customCss = modifyCss(customCss, {
|
|
144
216
|
"#wenyan pre code": [
|
|
145
217
|
{
|
|
146
218
|
property: "font-family",
|
|
147
|
-
value:
|
|
148
|
-
append:
|
|
219
|
+
value: monospace,
|
|
220
|
+
append: true
|
|
149
221
|
}
|
|
150
222
|
],
|
|
151
223
|
"#wenyan pre": [
|
|
152
224
|
{
|
|
153
225
|
property: "font-size",
|
|
154
226
|
value: "12px",
|
|
155
|
-
append:
|
|
227
|
+
append: true
|
|
156
228
|
}
|
|
157
229
|
]
|
|
158
230
|
});
|
|
159
|
-
const
|
|
231
|
+
const ast = csstree.parse(customCss, {
|
|
160
232
|
context: "stylesheet",
|
|
161
|
-
positions:
|
|
162
|
-
parseAtrulePrelude:
|
|
163
|
-
parseCustomProperty:
|
|
164
|
-
parseValue:
|
|
165
|
-
})
|
|
233
|
+
positions: false,
|
|
234
|
+
parseAtrulePrelude: false,
|
|
235
|
+
parseCustomProperty: false,
|
|
236
|
+
parseValue: false
|
|
237
|
+
});
|
|
238
|
+
const ast1 = csstree.parse(highlightCss, {
|
|
166
239
|
context: "stylesheet",
|
|
167
|
-
positions:
|
|
168
|
-
parseAtrulePrelude:
|
|
169
|
-
parseCustomProperty:
|
|
170
|
-
parseValue:
|
|
240
|
+
positions: false,
|
|
241
|
+
parseAtrulePrelude: false,
|
|
242
|
+
parseCustomProperty: false,
|
|
243
|
+
parseValue: false
|
|
171
244
|
});
|
|
172
|
-
|
|
173
|
-
|
|
245
|
+
ast.children.appendList(ast1.children);
|
|
246
|
+
if (isMacStyle) {
|
|
247
|
+
const ast2 = csstree.parse(macStyleCss, {
|
|
174
248
|
context: "stylesheet",
|
|
175
|
-
positions:
|
|
176
|
-
parseAtrulePrelude:
|
|
177
|
-
parseCustomProperty:
|
|
178
|
-
parseValue:
|
|
249
|
+
positions: false,
|
|
250
|
+
parseAtrulePrelude: false,
|
|
251
|
+
parseCustomProperty: false,
|
|
252
|
+
parseValue: false
|
|
179
253
|
});
|
|
180
|
-
|
|
254
|
+
ast.children.appendList(ast2.children);
|
|
181
255
|
}
|
|
182
|
-
|
|
256
|
+
csstree.walk(ast, {
|
|
183
257
|
visit: "Rule",
|
|
184
|
-
enter(
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
258
|
+
enter(node, item, list) {
|
|
259
|
+
const selectorList = node.prelude.children;
|
|
260
|
+
if (selectorList) {
|
|
261
|
+
selectorList.forEach((selectorNode) => {
|
|
262
|
+
const selector = csstree.generate(selectorNode);
|
|
263
|
+
const declarations = node.block.children.toArray();
|
|
264
|
+
if (selector === "#wenyan") {
|
|
265
|
+
declarations.forEach((decl) => {
|
|
266
|
+
const value = csstree.generate(decl.value);
|
|
267
|
+
wenyanElement.style[decl.property] = value;
|
|
268
|
+
});
|
|
269
|
+
} else {
|
|
270
|
+
const elements2 = wenyanElement.querySelectorAll(selector);
|
|
271
|
+
elements2.forEach((element) => {
|
|
272
|
+
declarations.forEach((decl) => {
|
|
273
|
+
const value = csstree.generate(decl.value);
|
|
274
|
+
element.style[decl.property] = value;
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
196
278
|
});
|
|
197
|
-
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
let elements = wenyanElement.querySelectorAll("mjx-container");
|
|
283
|
+
elements.forEach((element) => {
|
|
284
|
+
const svg2 = element.querySelector("svg");
|
|
285
|
+
svg2.style.width = svg2.getAttribute("width");
|
|
286
|
+
svg2.style.height = svg2.getAttribute("height");
|
|
287
|
+
svg2.removeAttribute("width");
|
|
288
|
+
svg2.removeAttribute("height");
|
|
289
|
+
const parent = element.parentElement;
|
|
290
|
+
element.remove();
|
|
291
|
+
parent.appendChild(svg2);
|
|
292
|
+
if (parent.classList.contains("block-equation")) {
|
|
293
|
+
parent.setAttribute("style", "text-align: center; margin-bottom: 1rem;");
|
|
198
294
|
}
|
|
199
295
|
});
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const c = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map();
|
|
210
|
-
f.walk(s, {
|
|
296
|
+
elements = wenyanElement.querySelectorAll("pre code");
|
|
297
|
+
elements.forEach((element) => {
|
|
298
|
+
element.innerHTML = element.innerHTML.replace(/\n/g, "<br>").replace(/(>[^<]+)|(^[^<]+)/g, (str) => str.replace(/\s/g, " "));
|
|
299
|
+
});
|
|
300
|
+
elements = wenyanElement.querySelectorAll("h1, h2, h3, h4, h5, h6, blockquote, pre");
|
|
301
|
+
elements.forEach((element) => {
|
|
302
|
+
const afterResults = /* @__PURE__ */ new Map();
|
|
303
|
+
const beforeResults = /* @__PURE__ */ new Map();
|
|
304
|
+
csstree.walk(ast, {
|
|
211
305
|
visit: "Rule",
|
|
212
|
-
enter(
|
|
213
|
-
const
|
|
214
|
-
|
|
306
|
+
enter(node) {
|
|
307
|
+
const selector = csstree.generate(node.prelude);
|
|
308
|
+
const tagName = element.tagName.toLowerCase();
|
|
309
|
+
if (selector.includes(`${tagName}::after`)) {
|
|
310
|
+
extractDeclarations(node, afterResults);
|
|
311
|
+
} else if (selector.includes(`${tagName}::before`)) {
|
|
312
|
+
extractDeclarations(node, beforeResults);
|
|
313
|
+
}
|
|
215
314
|
}
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
315
|
+
});
|
|
316
|
+
if (afterResults.size > 0) {
|
|
317
|
+
element.appendChild(buildPseudoSpan(afterResults, wenyanElement.ownerDocument));
|
|
318
|
+
}
|
|
319
|
+
if (beforeResults.size > 0) {
|
|
320
|
+
element.insertBefore(buildPseudoSpan(beforeResults, wenyanElement.ownerDocument), element.firstChild);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
elements = wenyanElement.querySelectorAll("li");
|
|
324
|
+
elements.forEach((li) => {
|
|
325
|
+
const section = wenyanElement.ownerDocument.createElement("section");
|
|
326
|
+
while (li.firstChild) {
|
|
327
|
+
section.appendChild(li.firstChild);
|
|
328
|
+
}
|
|
329
|
+
li.appendChild(section);
|
|
330
|
+
});
|
|
331
|
+
wenyanElement.setAttribute("data-provider", "WenYan");
|
|
332
|
+
return `${wenyanElement.outerHTML.replace(/class="mjx-solid"/g, 'fill="none" stroke-width="70"')}`;
|
|
223
333
|
}
|
|
224
|
-
function
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
334
|
+
function replaceCSSVariables(css) {
|
|
335
|
+
const variablePattern = /--([a-zA-Z0-9\-]+):\s*([^;()]*\((?:[^()]*|\([^()]*\))*\)[^;()]*|[^;]+);/g;
|
|
336
|
+
const varPattern = /var\(--([a-zA-Z0-9\-]+)\)/g;
|
|
337
|
+
const cssVariables = {};
|
|
338
|
+
let match;
|
|
339
|
+
while ((match = variablePattern.exec(css)) !== null) {
|
|
340
|
+
const variableName = match[1];
|
|
341
|
+
const variableValue = match[2].trim().replaceAll("\n", "");
|
|
342
|
+
cssVariables[variableName] = variableValue;
|
|
231
343
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
344
|
+
if (!cssVariables["sans-serif-font"]) {
|
|
345
|
+
cssVariables["sans-serif-font"] = sansSerif;
|
|
346
|
+
}
|
|
347
|
+
if (!cssVariables["monospace-font"]) {
|
|
348
|
+
cssVariables["monospace-font"] = monospace;
|
|
349
|
+
}
|
|
350
|
+
function resolveVariable(value, variables, resolved = /* @__PURE__ */ new Set()) {
|
|
351
|
+
if (resolved.has(value)) return value;
|
|
352
|
+
resolved.add(value);
|
|
353
|
+
let resolvedValue = value;
|
|
354
|
+
let match2;
|
|
355
|
+
while ((match2 = varPattern.exec(resolvedValue)) !== null) {
|
|
356
|
+
const varName = match2[1];
|
|
357
|
+
if (variables[varName]) {
|
|
358
|
+
const resolvedVar = resolveVariable(variables[varName], variables, resolved);
|
|
359
|
+
resolvedValue = resolvedValue.replace(match2[0], resolvedVar);
|
|
242
360
|
}
|
|
243
361
|
}
|
|
244
|
-
return
|
|
362
|
+
return resolvedValue;
|
|
245
363
|
}
|
|
246
|
-
for (const
|
|
247
|
-
const
|
|
248
|
-
|
|
364
|
+
for (const key in cssVariables) {
|
|
365
|
+
const resolvedValue = resolveVariable(cssVariables[key], cssVariables);
|
|
366
|
+
cssVariables[key] = resolvedValue;
|
|
249
367
|
}
|
|
250
|
-
let
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
368
|
+
let modifiedCSS = css;
|
|
369
|
+
while ((match = varPattern.exec(css)) !== null) {
|
|
370
|
+
const varName = match[1];
|
|
371
|
+
if (cssVariables[varName]) {
|
|
372
|
+
modifiedCSS = modifiedCSS.replace(match[0], cssVariables[varName]);
|
|
373
|
+
}
|
|
254
374
|
}
|
|
255
|
-
return
|
|
375
|
+
return modifiedCSS.replace(/:root\s*\{[^}]*\}/g, "");
|
|
256
376
|
}
|
|
257
|
-
function
|
|
258
|
-
const
|
|
377
|
+
function modifyCss(customCss, updates) {
|
|
378
|
+
const ast = csstree.parse(customCss, {
|
|
259
379
|
context: "stylesheet",
|
|
260
|
-
positions:
|
|
261
|
-
parseAtrulePrelude:
|
|
262
|
-
parseCustomProperty:
|
|
263
|
-
parseValue:
|
|
380
|
+
positions: false,
|
|
381
|
+
parseAtrulePrelude: false,
|
|
382
|
+
parseCustomProperty: false,
|
|
383
|
+
parseValue: false
|
|
264
384
|
});
|
|
265
|
-
|
|
385
|
+
csstree.walk(ast, {
|
|
266
386
|
visit: "Rule",
|
|
267
|
-
leave: (
|
|
268
|
-
if (
|
|
269
|
-
const
|
|
270
|
-
if (
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
387
|
+
leave: (node, item, list) => {
|
|
388
|
+
if (node.prelude.type !== "SelectorList") return;
|
|
389
|
+
const selectors = node.prelude.children.toArray().map((sel) => csstree.generate(sel));
|
|
390
|
+
if (selectors) {
|
|
391
|
+
const selector = selectors[0];
|
|
392
|
+
const update = updates[selector];
|
|
393
|
+
if (!update) return;
|
|
394
|
+
for (const { property, value, append } of update) {
|
|
395
|
+
if (value) {
|
|
396
|
+
let found = false;
|
|
397
|
+
csstree.walk(node.block, (decl) => {
|
|
398
|
+
if (decl.type === "Declaration" && decl.property === property) {
|
|
399
|
+
decl.value = csstree.parse(value, { context: "value" });
|
|
400
|
+
found = true;
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
if (!found && append) {
|
|
404
|
+
node.block.children.prepend(
|
|
405
|
+
list.createItem({
|
|
406
|
+
type: "Declaration",
|
|
407
|
+
property,
|
|
408
|
+
value: csstree.parse(value, { context: "value" })
|
|
409
|
+
})
|
|
410
|
+
);
|
|
411
|
+
}
|
|
285
412
|
}
|
|
413
|
+
}
|
|
286
414
|
}
|
|
287
415
|
}
|
|
288
|
-
})
|
|
416
|
+
});
|
|
417
|
+
return csstree.generate(ast);
|
|
289
418
|
}
|
|
290
|
-
function
|
|
291
|
-
|
|
419
|
+
function extractDeclarations(ruleNode, resultMap) {
|
|
420
|
+
csstree.walk(ruleNode.block, {
|
|
292
421
|
visit: "Declaration",
|
|
293
|
-
enter(
|
|
294
|
-
const
|
|
295
|
-
|
|
422
|
+
enter(declNode) {
|
|
423
|
+
const property = declNode.property;
|
|
424
|
+
const value = csstree.generate(declNode.value);
|
|
425
|
+
resultMap.set(property, value);
|
|
296
426
|
}
|
|
297
427
|
});
|
|
298
428
|
}
|
|
299
|
-
function
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
429
|
+
function buildPseudoSpan(beforeRresults, document) {
|
|
430
|
+
const span = document.createElement("section");
|
|
431
|
+
if (beforeRresults.get("content")) {
|
|
432
|
+
span.textContent = beforeRresults.get("content").replace(/['"]/g, "");
|
|
433
|
+
beforeRresults.delete("content");
|
|
434
|
+
}
|
|
435
|
+
for (const [k, v] of beforeRresults) {
|
|
436
|
+
if (v.includes("url(")) {
|
|
437
|
+
const svgMatch = v.match(/data:image\/svg\+xml;utf8,(.*<\/svg>)/);
|
|
438
|
+
const base64SvgMatch = v.match(/data:image\/svg\+xml;base64,([^"'\)]*)["']?\)/);
|
|
439
|
+
const httpMatch = v.match(/(?:"|')?(https?[^"'\)]*)(?:"|')?\)/);
|
|
440
|
+
if (svgMatch) {
|
|
441
|
+
const svgCode = decodeURIComponent(svgMatch[1]);
|
|
442
|
+
span.innerHTML = svgCode;
|
|
443
|
+
} else if (base64SvgMatch) {
|
|
444
|
+
const decodedString = atob(base64SvgMatch[1]);
|
|
445
|
+
span.innerHTML = decodedString;
|
|
446
|
+
} else if (httpMatch) {
|
|
447
|
+
const img = document.createElement("img");
|
|
448
|
+
img.src = httpMatch[1];
|
|
449
|
+
img.setAttribute("style", "vertical-align: top;");
|
|
450
|
+
span.appendChild(img);
|
|
314
451
|
}
|
|
315
|
-
|
|
452
|
+
beforeRresults.delete(k);
|
|
316
453
|
}
|
|
317
|
-
|
|
318
|
-
|
|
454
|
+
}
|
|
455
|
+
const entries = Array.from(beforeRresults.entries());
|
|
456
|
+
const cssString = entries.map(([key, value]) => `${key}: ${value}`).join("; ");
|
|
457
|
+
span.style.cssText = cssString;
|
|
458
|
+
return span;
|
|
319
459
|
}
|
|
320
|
-
function
|
|
321
|
-
let
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
460
|
+
function addFootnotes(listStyle, element) {
|
|
461
|
+
let footnotes = [];
|
|
462
|
+
let footnoteIndex = 0;
|
|
463
|
+
const links = element.querySelectorAll("a[href]");
|
|
464
|
+
links.forEach((linkElement) => {
|
|
465
|
+
const title = linkElement.textContent || linkElement.innerText;
|
|
466
|
+
const href = linkElement.getAttribute("href");
|
|
467
|
+
footnotes.push([++footnoteIndex, title, href]);
|
|
468
|
+
const footnoteMarker = element.ownerDocument.createElement("sup");
|
|
469
|
+
footnoteMarker.setAttribute("class", "footnote");
|
|
470
|
+
footnoteMarker.innerHTML = `[${footnoteIndex}]`;
|
|
471
|
+
linkElement.after(footnoteMarker);
|
|
472
|
+
});
|
|
473
|
+
if (footnoteIndex > 0) {
|
|
474
|
+
if (!listStyle) {
|
|
475
|
+
let footnoteArray = footnotes.map((x) => {
|
|
476
|
+
if (x[1] === x[2]) {
|
|
477
|
+
return `<p><span class="footnote-num">[${x[0]}]</span><span class="footnote-txt"><i>${x[1]}</i></span></p>`;
|
|
478
|
+
}
|
|
479
|
+
return `<p><span class="footnote-num">[${x[0]}]</span><span class="footnote-txt">${x[1]}: <i>${x[2]}</i></span></p>`;
|
|
480
|
+
});
|
|
481
|
+
const footnotesHtml = `<h3>引用链接</h3><section id="footnotes">${footnoteArray.join("")}</section>`;
|
|
482
|
+
element.innerHTML += footnotesHtml;
|
|
331
483
|
} else {
|
|
332
|
-
|
|
333
|
-
|
|
484
|
+
let footnoteArray = footnotes.map((x) => {
|
|
485
|
+
if (x[1] === x[2]) {
|
|
486
|
+
return `<li id="#footnote-${x[0]}">[${x[0]}]: <i>${x[1]}</i></li>`;
|
|
487
|
+
}
|
|
488
|
+
return `<li id="#footnote-${x[0]}">[${x[0]}] ${x[1]}: <i>${x[2]}</i></li>`;
|
|
489
|
+
});
|
|
490
|
+
const footnotesHtml = `<h3>引用链接</h3><div id="footnotes"><ul>${footnoteArray.join("")}</ul></div>`;
|
|
491
|
+
element.innerHTML += footnotesHtml;
|
|
334
492
|
}
|
|
493
|
+
}
|
|
335
494
|
}
|
|
336
495
|
export {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
496
|
+
addFootnotes,
|
|
497
|
+
buildPseudoSpan,
|
|
498
|
+
configureMarked,
|
|
499
|
+
extractDeclarations,
|
|
500
|
+
getContentForGzhBuiltinTheme,
|
|
501
|
+
getContentForGzhCustomCss,
|
|
502
|
+
handleFrontMatter,
|
|
503
|
+
modifyCss,
|
|
504
|
+
monospace,
|
|
505
|
+
renderMarkdown,
|
|
506
|
+
replaceCSSVariables,
|
|
507
|
+
sansSerif,
|
|
508
|
+
serif
|
|
350
509
|
};
|