@8btc/mditor 0.0.33 → 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.
@@ -6,8 +6,8 @@ declare global {
6
6
  /**
7
7
  * 数学公式渲染入口
8
8
  *
9
- * 默认使用 MathJax 渲染(SVG 输出),并在需要时按需加载相关脚本与样式。
10
- * 支持通过 options.math.engine 切换为 KaTeX
9
+ * 默认使用 KaTeX 渲染,并在需要时按需加载相关脚本与样式。
10
+ * 支持通过 options.math.engine 切换为 MathJax
11
11
  *
12
12
  * 参数说明:
13
13
  * - element: 需要进行数学渲染的根容器(默认 document)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@8btc/mditor",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "description": "mditor, Md编辑器",
5
5
  "author": "wujie-vditor",
6
6
  "jsdelivr": "dist/index.min.js",
@@ -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,39 +180,22 @@ 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
- // 循环加载会抛异常
216
199
  addScriptSync(
217
200
  `${options.cdn}/dist/js/mathjax/tex-svg-full.js`,
218
201
  "protyleMathJaxScript"
@@ -234,9 +217,7 @@ export const mathRender = (
234
217
  const pre = mathElement.parentElement as HTMLElement;
235
218
  pre.classList.add("vditor-wysiwyg__preview--math");
236
219
  }
237
- // 绑定自定义右键菜单(编辑区生效,预览区自动跳过)
238
220
  bindMathContextMenu(mathElement as HTMLElement);
239
- // 限定 document 范围为当前容器后再 clear/updateDocument,避免处理整页导致编辑器外的 $...$ 被重新渲染;不调用会导致同一公式被渲染 2 次
240
221
  const startup = window.MathJax.startup;
241
222
  const prevElements = startup.elements;
242
223
  if (element instanceof HTMLElement) {
@@ -253,9 +234,10 @@ export const mathRender = (
253
234
  errorTextElement &&
254
235
  errorTextElement.textContent.trim() !== ""
255
236
  ) {
256
- // 渲染失败时按普通文本展示原始内容
257
- mathElement.textContent = rawText;
258
- mathElement.className = "language-math";
237
+ mathElement.innerHTML =
238
+ errorTextElement.textContent.trim();
239
+ mathElement.className =
240
+ "language-math vditor-reset--error";
259
241
  }
260
242
  if (next) {
261
243
  next();
@@ -264,7 +246,6 @@ export const mathRender = (
264
246
  );
265
247
  };
266
248
  window.MathJax.startup.promise.then(() => {
267
- // 限定仅处理当前容器,避免组件内部或用户配置导致处理整页、影响编辑器以外的公式
268
249
  if (window.MathJax.startup) {
269
250
  window.MathJax.startup.typeset = true;
270
251
  window.MathJax.startup.elements =