@jx3box/jx3box-vue3-ui 1.1.0 → 1.1.1
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/assets/js/katex.js +174 -10
- package/package.json +1 -1
package/assets/js/katex.js
CHANGED
|
@@ -1,17 +1,181 @@
|
|
|
1
|
-
import $ from
|
|
2
|
-
import katex from
|
|
3
|
-
import
|
|
4
|
-
// katex.render("x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}", document.getElementById("math"));
|
|
1
|
+
import $ from 'jquery';
|
|
2
|
+
import katex from 'katex';
|
|
3
|
+
import 'katex/dist/katex.min.css';
|
|
5
4
|
|
|
6
|
-
function
|
|
5
|
+
function renderKatexBlock(selector = ".w-latex") {
|
|
7
6
|
try {
|
|
8
|
-
$(selector).each(function
|
|
7
|
+
$(selector).each(function() {
|
|
9
8
|
let $katex = $(this);
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
|
|
10
|
+
// 获取原始HTML内容
|
|
11
|
+
let raw = $katex.html();
|
|
12
|
+
|
|
13
|
+
// 处理各种换行组合:
|
|
14
|
+
// 1. \\<br /> 或 \\<br> -> \\
|
|
15
|
+
raw = raw.replace(/\\\\\s*<br\s*\/?>/gi, '\\\\');
|
|
16
|
+
// 2. \<br /> 或 \<br> -> \\
|
|
17
|
+
raw = raw.replace(/\\\s*<br\s*\/?>/gi, '\\\\');
|
|
18
|
+
// 3. 单独的 <br /> -> \\
|
|
19
|
+
raw = raw.replace(/<br\s*\/?>/gi, '\\\\');
|
|
20
|
+
|
|
21
|
+
// 移除其他HTML标签但保留内容
|
|
22
|
+
raw = raw.replace(/<[^>]+>/g, '');
|
|
23
|
+
|
|
24
|
+
// 解码HTML实体(如 -> 空格)
|
|
25
|
+
raw = $('<div>').html(raw).text();
|
|
26
|
+
|
|
27
|
+
// 清理多余的空白字符
|
|
28
|
+
raw = raw.trim();
|
|
29
|
+
|
|
30
|
+
katex.render(raw, $katex.get(0), { displayMode: true });
|
|
12
31
|
});
|
|
13
32
|
} catch (e) {
|
|
14
|
-
console.error(e);
|
|
33
|
+
console.error('KaTeX render error:', e);
|
|
34
|
+
console.error('Failed content:', $(this).html());
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function renderKatexInline(container = document.body) {
|
|
39
|
+
// 支持 \(...\) 和 $...$ 两种行内语法
|
|
40
|
+
// 改进正则表达式,更好地匹配单美元符号
|
|
41
|
+
const inlineRegex = /(\\\((.+?)\\\)|\$([^$]+?)\$)/g;
|
|
42
|
+
|
|
43
|
+
const walker = document.createTreeWalker(
|
|
44
|
+
container,
|
|
45
|
+
NodeFilter.SHOW_TEXT,
|
|
46
|
+
{
|
|
47
|
+
acceptNode: function (node) {
|
|
48
|
+
if (
|
|
49
|
+
node.parentNode &&
|
|
50
|
+
!node.parentNode.closest("pre") &&
|
|
51
|
+
(node.nodeValue.includes("\\(") || node.nodeValue.includes("$"))
|
|
52
|
+
) {
|
|
53
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
54
|
+
}
|
|
55
|
+
return NodeFilter.FILTER_REJECT;
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const nodesToReplace = [];
|
|
61
|
+
while (walker.nextNode()) {
|
|
62
|
+
nodesToReplace.push(walker.currentNode);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
nodesToReplace.forEach((node) => {
|
|
66
|
+
const text = node.nodeValue;
|
|
67
|
+
const frag = document.createDocumentFragment();
|
|
68
|
+
let lastIndex = 0;
|
|
69
|
+
let match;
|
|
70
|
+
|
|
71
|
+
while ((match = inlineRegex.exec(text))) {
|
|
72
|
+
const fullMatch = match[0];
|
|
73
|
+
const parenContent = match[2]; // \(...\)的内容
|
|
74
|
+
const dollarContent = match[3]; // $...$的内容
|
|
75
|
+
const raw = parenContent || dollarContent;
|
|
76
|
+
const matchStart = match.index;
|
|
77
|
+
|
|
78
|
+
// 添加匹配前文本
|
|
79
|
+
frag.appendChild(document.createTextNode(text.slice(lastIndex, matchStart)));
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const span = document.createElement("span");
|
|
83
|
+
// 处理JavaScript字符串中的双反斜杠转义
|
|
84
|
+
const processedRaw = raw.replace(/\\\\/g, '\\');
|
|
85
|
+
// 添加更多选项确保正确渲染
|
|
86
|
+
span.innerHTML = katex.renderToString(processedRaw, {
|
|
87
|
+
displayMode: false,
|
|
88
|
+
throwOnError: false,
|
|
89
|
+
strict: false,
|
|
90
|
+
trust: true
|
|
91
|
+
});
|
|
92
|
+
frag.appendChild(span);
|
|
93
|
+
} catch (e) {
|
|
94
|
+
frag.appendChild(document.createTextNode(fullMatch));
|
|
95
|
+
console.error("Inline render error:", raw, e);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lastIndex = inlineRegex.lastIndex;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 剩余文本
|
|
102
|
+
if (lastIndex < text.length) {
|
|
103
|
+
frag.appendChild(document.createTextNode(text.slice(lastIndex)));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (frag.hasChildNodes()) {
|
|
107
|
+
node.parentNode.replaceChild(frag, node);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function renderKatexDisplayBlock(container = document.body) {
|
|
113
|
+
// 支持 $$...$$ 和 \[...\] 两种块语法
|
|
114
|
+
const blockRegex = /(\$\$\s*([\s\S]+?)\s*\$\$|\\\[\s*([\s\S]+?)\s*\\\])/g;
|
|
115
|
+
|
|
116
|
+
const walker = document.createTreeWalker(
|
|
117
|
+
container,
|
|
118
|
+
NodeFilter.SHOW_TEXT,
|
|
119
|
+
{
|
|
120
|
+
acceptNode: function (node) {
|
|
121
|
+
if (
|
|
122
|
+
node.parentNode &&
|
|
123
|
+
!node.parentNode.closest("pre") &&
|
|
124
|
+
(node.nodeValue.includes("$$") || node.nodeValue.includes("\\["))
|
|
125
|
+
) {
|
|
126
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
127
|
+
}
|
|
128
|
+
return NodeFilter.FILTER_REJECT;
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const nodesToReplace = [];
|
|
134
|
+
while (walker.nextNode()) {
|
|
135
|
+
nodesToReplace.push(walker.currentNode);
|
|
15
136
|
}
|
|
137
|
+
|
|
138
|
+
nodesToReplace.forEach((node) => {
|
|
139
|
+
const text = node.nodeValue;
|
|
140
|
+
const frag = document.createDocumentFragment();
|
|
141
|
+
let lastIndex = 0;
|
|
142
|
+
let match;
|
|
143
|
+
|
|
144
|
+
while ((match = blockRegex.exec(text))) {
|
|
145
|
+
const fullMatch = match[0];
|
|
146
|
+
const dollarContent = match[2]; // $$...$$的内容
|
|
147
|
+
const bracketContent = match[3]; // \[...\]的内容
|
|
148
|
+
const raw = dollarContent || bracketContent;
|
|
149
|
+
const matchStart = match.index;
|
|
150
|
+
|
|
151
|
+
// 添加匹配前文本
|
|
152
|
+
frag.appendChild(document.createTextNode(text.slice(lastIndex, matchStart)));
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
const div = document.createElement("div");
|
|
156
|
+
// 处理JavaScript字符串中的双反斜杠转义
|
|
157
|
+
const processedRaw = raw.replace(/\\\\/g, '\\');
|
|
158
|
+
div.innerHTML = katex.renderToString(processedRaw, { displayMode: true });
|
|
159
|
+
frag.appendChild(div);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
frag.appendChild(document.createTextNode(fullMatch));
|
|
162
|
+
console.error("Block render error:", raw, e);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
lastIndex = blockRegex.lastIndex;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 添加剩余文本
|
|
169
|
+
if (lastIndex < text.length) {
|
|
170
|
+
frag.appendChild(document.createTextNode(text.slice(lastIndex)));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
node.parentNode.replaceChild(frag, node);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function renderKatexAll(container = document.body) {
|
|
178
|
+
renderKatexBlock(".w-latex");
|
|
179
|
+
renderKatexDisplayBlock(container);
|
|
180
|
+
renderKatexInline(container);
|
|
16
181
|
}
|
|
17
|
-
export default renderKatex;
|