Skip to content

Commit e634d73

Browse files
committed
fix: revert mathjax changes
1 parent bf321c5 commit e634d73

File tree

7 files changed

+58
-166
lines changed

7 files changed

+58
-166
lines changed

apps/web/index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,12 @@
9696
window.MathJax = {
9797
tex: { tags: 'ams' },
9898
svg: { fontCache: 'none' },
99-
loader: {
100-
paths: {
101-
fonts: '[mathjax]/../',
102-
},
103-
},
10499
}
105100
</script>
106-
<script id="MathJax-script" src="static/mathjax/tex-svg.js"></script>
101+
<script
102+
id="MathJax-script"
103+
src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/mathjax@3/es5/tex-svg.js"
104+
></script>
107105
<script type="module" src="/src/main.ts"></script>
108106
<script type="module" src="/src/sidepanel.ts"></script>
109107
</body>

apps/web/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"juice": "11.0.3",
5050
"lucide-vue-next": "^1.0.0",
5151
"marked": "^17.0.5",
52-
"mathjax": "^4.1.1",
5352
"pinia": "^3.0.4",
5453
"qiniu-js": "^3.4.4",
5554
"radix-vue": "^1.9.17",

apps/web/plugins/vite-plugin-mathjax-local.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Plugin } from 'vite'
2+
import process from 'node:process'
3+
4+
/**
5+
* Vite 插件:在 uTools 构建时将远程资源替换为本地资源
6+
*/
7+
export function utoolsLocalAssetsPlugin(): Plugin {
8+
const isUTools = process.env.SERVER_ENV === `UTOOLS`
9+
10+
return {
11+
name: `vite-plugin-utools-local-assets`,
12+
apply: `build`,
13+
transformIndexHtml: {
14+
order: `post`,
15+
handler(html) {
16+
if (!isUTools)
17+
return html
18+
19+
// 替换 MathJax
20+
html = html.replace(
21+
/https:\/\/cdn-doocs\.oss-cn-shenzhen\.aliyuncs\.com\/npm\/mathjax@3\/es5\/tex-svg\.js/g,
22+
`./static/libs/mathjax/tex-svg.js`,
23+
)
24+
25+
return html
26+
},
27+
},
28+
}
29+
}

apps/web/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { defineConfig, loadEnv } from 'vite'
1111
import { VitePluginRadar } from 'vite-plugin-radar'
1212
import vueDevTools from 'vite-plugin-vue-devtools'
1313

14-
import { mathjaxLocalPlugin } from './plugins/vite-plugin-mathjax-local'
14+
import { utoolsLocalAssetsPlugin } from './plugins/vite-plugin-utools-local-assets'
1515

1616
const isNetlify = process.env.SERVER_ENV === `NETLIFY`
1717
const isUTools = process.env.SERVER_ENV === `UTOOLS`
@@ -47,7 +47,7 @@ export default defineConfig(({ mode }) => {
4747
Components({
4848
resolvers: [],
4949
}),
50-
mathjaxLocalPlugin(),
50+
isUTools && utoolsLocalAssetsPlugin(),
5151
],
5252
resolve: {
5353
alias: { '@': path.resolve(__dirname, `./src`) },

packages/core/src/extensions/katex.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,31 @@ function createRenderer(defaultDisplay: boolean, withStyle: boolean = true) {
1717
return (token: any) => {
1818
const display = token.displayMode ?? defaultDisplay
1919

20-
try {
21-
// @ts-expect-error MathJax is a global variable
22-
window.MathJax.texReset()
23-
// @ts-expect-error MathJax is a global variable
24-
const mjxContainer = window.MathJax.tex2svg(token.text, { display })
25-
const svg = mjxContainer.firstChild
26-
const width = svg.style[`min-width`] || svg.getAttribute(`width`)
27-
svg.removeAttribute(`width`)
28-
29-
if (withStyle) {
30-
svg.style.display = `initial`
31-
svg.style.setProperty(`max-width`, `300vw`, `important`)
32-
svg.style.flexShrink = `0`
33-
svg.style.width = width
34-
}
35-
36-
if (!display) {
37-
return `<span class="katex-inline">${svg.outerHTML}</span>`
38-
}
39-
40-
return `<section class="katex-block">${svg.outerHTML}</section>`
20+
// @ts-expect-error MathJax is a global variable
21+
window.MathJax.texReset()
22+
// @ts-expect-error MathJax is a global variable
23+
const mjxContainer = window.MathJax.tex2svg(token.text, { display })
24+
const svg = mjxContainer.firstChild
25+
const width = svg.style[`min-width`] || svg.getAttribute(`width`)
26+
svg.removeAttribute(`width`)
27+
28+
// 行内公式对齐 https://groups.google.com/g/mathjax-users/c/zThKffrrCvE?pli=1
29+
// 直接覆盖 style 会覆盖 MathJax 的样式,需要手动设置
30+
// svg.style = `max-width: 300vw !important; display: initial; flex-shrink: 0;`
31+
32+
if (withStyle) {
33+
svg.style.display = `initial`
34+
svg.style.setProperty(`max-width`, `300vw`, `important`)
35+
svg.style.flexShrink = `0`
36+
svg.style.width = width
4137
}
42-
catch {
43-
// MathJax v4 may throw retry errors when font data not yet loaded
44-
const escaped = token.text.replace(/</g, `&lt;`).replace(/>/g, `&gt;`)
45-
if (!display) {
46-
return `<span class="katex-inline"><code>${escaped}</code></span>`
47-
}
48-
return `<section class="katex-block"><code>${escaped}</code></section>`
38+
39+
if (!display) {
40+
// 新主题系统:使用 class 而非内联样式
41+
return `<span class="katex-inline">${svg.outerHTML}</span>`
4942
}
43+
44+
return `<section class="katex-block">${svg.outerHTML}</section>`
5045
}
5146
}
5247

pnpm-lock.yaml

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)