@8btc/mditor 0.0.32 → 0.0.34

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.
@@ -50,8 +50,8 @@ const normalizeTex = (tex: string): string => {
50
50
  /**
51
51
  * 数学公式渲染入口
52
52
  *
53
- * 默认使用 MathJax 渲染(SVG 输出),并在需要时按需加载相关脚本与样式。
54
- * 支持通过 options.math.engine 切换为 KaTeX
53
+ * 默认使用 KaTeX 渲染,并在需要时按需加载相关脚本与样式。
54
+ * 支持通过 options.math.engine 切换为 MathJax
55
55
  *
56
56
  * 参数说明:
57
57
  * - element: 需要进行数学渲染的根容器(默认 document)
@@ -70,7 +70,7 @@ export const mathRender = (
70
70
  const defaultOptions = {
71
71
  cdn: Constants.CDN,
72
72
  math: {
73
- engine: "MathJax",
73
+ engine: "KaTeX",
74
74
  inlineDigit: true,
75
75
  macros: {},
76
76
  },
@@ -180,82 +180,26 @@ export const mathRender = (
180
180
  next();
181
181
  };
182
182
  if (!window.MathJax) {
183
- // typeset 设为 true 以正常完成公式渲染;elements 限定为传入的容器,外部使用本包时仅处理该容器,不影响编辑器以外的公式。
184
- // 渲染时会将 startup.elements 临时设为传入的 element(当前预览/编辑容器),再 clear/updateDocument。
185
183
  window.MathJax = {
186
184
  loader: {
187
185
  paths: { mathjax: `${options.cdn}/dist/js/mathjax` },
188
186
  },
189
187
  startup: {
190
- typeset: true,
191
- elements: element instanceof HTMLElement ? [element] : [],
188
+ typeset: false,
189
+ elements:
190
+ element instanceof HTMLElement ? [element] : [],
192
191
  },
193
192
  tex: {
194
- inlineMath: [
195
- ["$", "$"],
196
- ["\\(", "\\)"],
197
- ],
198
- displayMath: [
199
- ["$$", "$$"],
200
- ["\\[", "\\]"],
201
- ],
202
- processEscapes: true, // 允许转义字符(可选,确保分隔符不被误解析)
203
193
  macros: options.math.macros,
204
- // 启用 ams 与 unicode,支持 \unicode 宏以渲染 ∯(oiint)
205
- packages: { "[+]": ["ams", "unicode", "noerrors"] },
206
- },
207
- // 关闭 MathJax 自带右键菜单,使用自定义菜单
208
- options: {
209
- enableMenu: false,
210
194
  },
211
195
  };
212
- // https://github.com/Vanessa219/vditor/issues/1453
196
+ // https://github.com/Vanessa219/vditor/issues/1453 额外配置(packages、inlineMath、displayMath 等)由调用方通过 mathJaxOptions 传入
213
197
  Object.assign(window.MathJax, options.math.mathJaxOptions);
214
198
  }
215
- const mathJaxScriptUrl = `${options.cdn}/dist/js/mathjax/tex-svg-full.js`;
216
- const mathJaxScriptId = "protyleMathJaxScript";
217
- // Qt WebView 中同步 XHR 被限制,addScriptSync 无法加载脚本,改为异步 addScript
218
- const isQtWebView =
219
- typeof navigator !== "undefined" &&
220
- /Qt|QtWebEngine/i.test(navigator.userAgent);
221
- const runRenderChain = () => {
222
- window.MathJax.startup.promise.then(() => {
223
- if (window.MathJax.startup) {
224
- window.MathJax.startup.typeset = true;
225
- window.MathJax.startup.elements =
226
- element instanceof HTMLElement ? [element] : [];
227
- }
228
- const chains: any[] = [];
229
- for (let i = 0; i < mathElements.length; i++) {
230
- const mathElement = mathElements[i];
231
- if (
232
- !mathElement.parentElement.classList.contains(
233
- "vditor-wysiwyg__pre"
234
- ) &&
235
- !mathElement.parentElement.classList.contains(
236
- "vditor-ir__marker--pre"
237
- ) &&
238
- !mathElement.getAttribute("data-math") &&
239
- code160to32(mathElement.textContent).trim()
240
- ) {
241
- chains.push((next: () => void) => {
242
- if (i === mathElements.length - 1) {
243
- renderMath(mathElement);
244
- } else {
245
- renderMath(mathElement, next);
246
- }
247
- });
248
- }
249
- }
250
- chainAsync(chains);
251
- });
252
- };
253
- if (isQtWebView) {
254
- addScript(mathJaxScriptUrl, mathJaxScriptId).then(runRenderChain);
255
- } else {
256
- addScriptSync(mathJaxScriptUrl, mathJaxScriptId);
257
- runRenderChain();
258
- }
199
+ addScriptSync(
200
+ `${options.cdn}/dist/js/mathjax/tex-svg-full.js`,
201
+ "protyleMathJaxScript"
202
+ );
259
203
  const renderMath = (mathElement: Element, next?: () => void) => {
260
204
  const rawText = code160to32(mathElement.textContent).trim();
261
205
  const math = normalizeTex(rawText);
@@ -273,29 +217,16 @@ export const mathRender = (
273
217
  const pre = mathElement.parentElement as HTMLElement;
274
218
  pre.classList.add("vditor-wysiwyg__preview--math");
275
219
  }
276
- // 绑定自定义右键菜单(编辑区生效,预览区自动跳过)
277
220
  bindMathContextMenu(mathElement as HTMLElement);
278
- // 限定 document 范围为当前容器后再 clear/updateDocument,避免处理整页导致编辑器外的 $...$ 被重新渲染;不调用会导致同一公式被渲染 2 次
279
- // Qt WebView 中 clear/updateDocument 可能清掉刚插入的节点或抛错,故跳过
280
- if (!isQtWebView) {
281
- try {
282
- const startup = window.MathJax.startup;
283
- const prevElements = startup.elements;
284
- if (element instanceof HTMLElement) {
285
- startup.elements = [element];
286
- }
287
- startup.document.clear();
288
- startup.document.updateDocument();
289
- startup.elements =
290
- prevElements !== undefined ? prevElements : [];
291
- } catch (_) {
292
- if (
293
- window.MathJax?.startup?.elements !== undefined
294
- ) {
295
- window.MathJax.startup.elements = [];
296
- }
297
- }
221
+ const startup = window.MathJax.startup;
222
+ const prevElements = startup.elements;
223
+ if (element instanceof HTMLElement) {
224
+ startup.elements = [element];
298
225
  }
226
+ startup.document.clear();
227
+ startup.document.updateDocument();
228
+ startup.elements =
229
+ prevElements !== undefined ? prevElements : [];
299
230
  const errorTextElement = node.querySelector(
300
231
  '[data-mml-node="merror"]'
301
232
  );
@@ -303,9 +234,10 @@ export const mathRender = (
303
234
  errorTextElement &&
304
235
  errorTextElement.textContent.trim() !== ""
305
236
  ) {
306
- // 渲染失败时按普通文本展示原始内容
307
- mathElement.textContent = rawText;
308
- mathElement.className = "language-math";
237
+ mathElement.innerHTML =
238
+ errorTextElement.textContent.trim();
239
+ mathElement.className =
240
+ "language-math vditor-reset--error";
309
241
  }
310
242
  if (next) {
311
243
  next();
@@ -313,5 +245,35 @@ export const mathRender = (
313
245
  }
314
246
  );
315
247
  };
248
+ window.MathJax.startup.promise.then(() => {
249
+ if (window.MathJax.startup) {
250
+ window.MathJax.startup.typeset = true;
251
+ window.MathJax.startup.elements =
252
+ element instanceof HTMLElement ? [element] : [];
253
+ }
254
+ const chains: any[] = [];
255
+ for (let i = 0; i < mathElements.length; i++) {
256
+ const mathElement = mathElements[i];
257
+ if (
258
+ !mathElement.parentElement.classList.contains(
259
+ "vditor-wysiwyg__pre"
260
+ ) &&
261
+ !mathElement.parentElement.classList.contains(
262
+ "vditor-ir__marker--pre"
263
+ ) &&
264
+ !mathElement.getAttribute("data-math") &&
265
+ code160to32(mathElement.textContent).trim()
266
+ ) {
267
+ chains.push((next: () => void) => {
268
+ if (i === mathElements.length - 1) {
269
+ renderMath(mathElement);
270
+ } else {
271
+ renderMath(mathElement, next);
272
+ }
273
+ });
274
+ }
275
+ }
276
+ chainAsync(chains);
277
+ });
316
278
  }
317
279
  };
@@ -4,10 +4,8 @@ export const addScriptSync = (path: string, id: string) => {
4
4
  }
5
5
  const xhrObj = new XMLHttpRequest();
6
6
  xhrObj.open("GET", path, false);
7
- xhrObj.setRequestHeader(
8
- "Accept",
9
- "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
10
- );
7
+ xhrObj.setRequestHeader("Accept",
8
+ "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");
11
9
  xhrObj.send("");
12
10
  const scriptElement = document.createElement("script");
13
11
  scriptElement.type = "text/javascript";
@@ -30,7 +28,7 @@ export const addScript = (path: string, id: string) => {
30
28
  document.head.appendChild(scriptElement);
31
29
  scriptElement.onerror = (event) => {
32
30
  reject(event);
33
- };
31
+ }
34
32
  scriptElement.onload = () => {
35
33
  if (document.getElementById(id)) {
36
34
  // 循环调用需清除 DOM 中的 script 标签