Skip to content

Commit b076c15

Browse files
committed
fix(math): adjust interval timing and improve math rendering logic
1 parent c3c9bf5 commit b076c15

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

playground/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const content = ref<string>('')
77
// To avoid flashing sequences like ":::" during streaming (which later
88
// become an AdmonitionNode), we look ahead when encountering ":" and
99
// defer appending consecutive colons until a non-colon character is seen.
10-
useInterval(10, {
10+
useInterval(16, {
1111
callback() {
1212
const cur = content.value.length
1313
if (cur >= streamContent.length)

playground/src/const/markdown.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,52 +33,52 @@ export const streamContent = `>>>I'll create a simple Electron + Vue chat applic
3333
## 1. 泰勒公式(Taylor's Formula)
3434
3535
### 一般形式(在点 \(x = a\) 处展开):
36-
\\[
36+
\[
3737
f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)}{2!}(x-a)^2 + \frac{f'''(a)}{3!}(x-a)^3 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n + R_n(x)
38-
\\]
38+
\]
3939
4040
其中:
4141
- \(f^{(k)}(a)\) 是 \(f(x)\) 在 \(x=a\) 处的 \(k\) 阶导数
4242
- \(R_n(x)\) 是余项,常见形式有拉格朗日余项:
43-
\\[
43+
\[
4444
R_n(x) = \frac{f^{(n+1)}(xi)}{(n+1)!}(x-a)^{n+1}, \quad xi \text{ 在 } a \text{ 和 } x \text{ 之间}
45-
\\]
45+
\]
4646
4747
---
4848
4949
## 2. 麦克劳林公式(Maclaurin's Formula,即 \(a=0\) 时的泰勒公式):
50-
\\[
50+
\[
5151
f(x) = f(0) + f'(0)x + \frac{f''(0)}{2!}x^2 + \frac{f'''(0)}{3!}x^3 + \cdots + \frac{f^{(n)}(0)}{n!}x^n + R_n(x)
52-
\\]
52+
\]
5353
5454
---
5555
5656
## 3. 常见函数的麦克劳林展开(前几项)
5757
5858
- **指数函数**:
59-
\\[
59+
\[
6060
e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots + \frac{x^n}{n!} + \cdots, \quad x \in \mathbb{R}
61-
\\]
61+
\]
6262
6363
- **正弦函数**:
64-
\\[
64+
\[
6565
\\sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots + (-1)^n \frac{x^{2n+1}}{(2n+1)!} + \cdots
66-
\\]
66+
\]
6767
6868
- **余弦函数**:
69-
\\[
69+
\[
7070
\cos x = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \cdots + (-1)^n \frac{x^{2n}}{(2n)!} + \cdots
71-
\\]
71+
\]
7272
7373
- **自然对数**(在 \(x=0\) 附近):
74-
\\[
74+
\[
7575
\ln(1+x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \cdots + (-1)^{n-1} \frac{x^n}{n} + \cdots, \quad -1 < x \le 1
76-
\\]
76+
\]
7777
7878
- **二项式展开**(\( (1+x)^m \),\(m\) 为实数):
79-
\\[
79+
\[
8080
(1+x)^m = 1 + mx + \frac{m(m-1)}{2!}x^2 + \frac{m(m-1)(m-2)}{3!}x^3 + \cdots, \quad |x| < 1
81-
\\]
81+
\]
8282
8383
- **公式**
8484

src/components/MathBlockNode/MathBlockNode.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ onMounted(() => {
6969
onBeforeUnmount(() => {
7070
// prevent any pending worker responses from touching the DOM
7171
isUnmounted = true
72-
// increment id so any in-flight render is considered stale
73-
currentRenderId++
72+
if (currentAbortController) {
73+
currentAbortController.abort()
74+
currentAbortController = null
75+
}
7476
})
7577
</script>
7678

src/utils/markdown/plugins/math.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ export function applyMath(md: MarkdownIt, mathOpts?: MathOptions) {
222222
const delimiters: [string, string][] = [
223223
['$$', '$$'],
224224
['\(', '\)'],
225-
['\\(', '\\)'],
226225
]
227226
let searchPos = 0
228227
// use findMatchingClose from util
@@ -258,6 +257,7 @@ export function applyMath(md: MarkdownIt, mathOpts?: MathOptions) {
258257
const index = src.indexOf(open, searchPos)
259258
if (index === -1)
260259
break
260+
261261
// If the delimiter is immediately preceded by a ']' (possibly with
262262
// intervening spaces), it's likely part of a markdown link like
263263
// `[text](...)`, so we should not treat this '(' as the start of
@@ -319,7 +319,7 @@ export function applyMath(md: MarkdownIt, mathOpts?: MathOptions) {
319319
const token = state.push('math_inline', 'math', 0)
320320
token.content = normalizeStandaloneBackslashT(content, mathOpts)
321321
token.markup = open === '$$' ? '$$' : open === '\\(' ? '\\(\\)' : open === '$' ? '$' : '()'
322-
322+
token.loading = false
323323
// we'll handle the trailing text after loop; but to preserve the
324324
// original behaviour when strong prefix was detected, mimic it here
325325
if (isStrongPrefix) {
@@ -369,9 +369,8 @@ export function applyMath(md: MarkdownIt, mathOpts?: MathOptions) {
369369
silent: boolean,
370370
) => {
371371
const delimiters: [string, string][] = [
372-
['\\[', '\\]'],
372+
['\[', '\]'],
373373
['$$', '$$'],
374-
['[', ']'],
375374
]
376375

377376
const startPos = state.bMarks[startLine] + state.tShift[startLine]

0 commit comments

Comments
 (0)