fix(webui): avoid empty inline TeX tokens

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-02 20:44:50 +08:00
1 parent 810802e016
commit 870dc2466f
4 files changed
+333 -9

No files matched your search

+24 -9
View File
@@ -204,21 +204,36 @@ function tokenizeTexMathText(effects: Effects, ok: State, nok: State): State {
effects.consume(code);
effects.exit("mathTextSequence");
return between;
}
function between(code: Code): State | undefined {
if (code === null) return nok(code);
if (code === BACKSLASH) {
closeSequence = effects.enter("mathTextSequence");
effects.consume(code);
return close;
}
if (isLineEnding(code)) {
effects.enter("lineEnding");
effects.consume(code);
effects.exit("lineEnding");
return between;
}
// Commands can immediately follow the opening delimiter. Only enter a data
// token when there is a character to consume; micromark rejects empty tokens.
effects.enter("mathTextData");
effects.consume(code);
return data;
}
function data(code: Code): State | undefined {
if (code === null) {
if (code === null || code === BACKSLASH || isLineEnding(code)) {
effects.exit("mathTextData");
return nok(code);
}
if (code === BACKSLASH) {
effects.exit("mathTextData");
closeSequence = effects.enter("mathTextSequence");
effects.consume(code);
return close;
return between(code);
}
effects.consume(code);
+247
View File
@@ -0,0 +1,247 @@
# 行内公式
这是行内公式:\(E = mc^2\)。
这是另一种行内公式:$a^2+b^2=c^2$。
---
# 独立公式
\[
\int_{-\infty}^{\infty} e^{-x^2}\,dx=\sqrt{\pi}
\]
$$
\sum_{i=1}^{n} i=\frac{n(n+1)}{2}
$$
---
# 分行公式
\[
\begin{aligned}
f(x)
&= x^2+2x+1 \\
&= (x+1)^2
\end{aligned}
\]
---
# 多行等式
\[
\begin{aligned}
a &= b+c \\
&= d+e \\
&= f
\end{aligned}
\]
---
# 分情况公式
\[
f(x)=
\begin{cases}
x^2, & x\ge 0,\\
-x, & x<0.
\end{cases}
\]
---
# 矩阵
\[
A=
\begin{pmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{pmatrix}
\]
\[
\begin{bmatrix}
a & b\\
c & d
\end{bmatrix}
\]
---
# 行列式
\[
\det(A)=
\begin{vmatrix}
a & b\\
c & d
\end{vmatrix}
=ad-bc
\]
---
# 分数与根式
\[
x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}
\]
---
# 极限、导数与积分
\[
\lim_{x\to 0}\frac{\sin x}{x}=1
\]
\[
\frac{d}{dx}\left(\ln x\right)=\frac{1}{x}
\]
\[
\int_a^b f(x)\,dx=F(b)-F(a)
\]
---
# 向量与范数
\[
\vec{v}=
\begin{bmatrix}
v_1\\
v_2\\
v_3
\end{bmatrix},
\qquad
\|\vec{v}\|_2=
\sqrt{v_1^2+v_2^2+v_3^2}
\]
---
# 概率
\[
P(A\mid B)=\frac{P(B\mid A)P(A)}{P(B)}
\]
\[
X\sim\mathcal{N}(\mu,\sigma^2)
\]
---
# 逻辑与集合
\[
\forall x\in\mathbb{R},\quad x^2\ge 0
\]
\[
A\subseteq B,\qquad
A\cap B=\varnothing,\qquad
A\cup B
\]
---
# 颜色与字体
\[
\color{red}{\text{红色文字}}
\qquad
\mathbf{bold}
\qquad
\mathit{italic}
\qquad
\mathrm{Roman}
\]
---
# 上标与下标
\[
x_1,x_2,\ldots,x_n
\qquad
e^{i\pi}+1=0
\]
---
# 大型运算符
\[
\left(\sum_{i=1}^{n}x_i\right)^2
\qquad
\prod_{k=1}^{m} k
\qquad
\bigcup_{i=1}^{n} A_i
\]
---
# 物理公式
\[
\mathrm{HFOV}=2\arctan\left(\frac{W}{2f_x}\right)
\]
\[
F=ma,\qquad
p=mv,\qquad
E=h\nu
\]
---
# 化学与单位
\[
\mathrm{H_2O},\qquad
\mathrm{CO_2},\qquad
\mathrm{C_6H_{12}O_6}
\]
\[
9.8\,\mathrm{m\,s^{-2}}
\qquad
3.0\times10^8\,\mathrm{m/s}
\]
---
# Markdown 列表中的公式
1. $x=1$
2. $y=2$
- \(\alpha+\beta=\gamma\)
- \(\Delta x\to 0\)
- \(\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}\)
---
# 引用中的公式
> 公式:\[
> \hat{y}=\sigma(Wx+b)
> \]
---
# 表格中的公式
| 名称 | 公式 |
|---|---|
| 欧拉公式 | \(e^{i\pi}+1=0\) |
| 勾股定理 | \(a^2+b^2=c^2\) |
| 透视关系 | \(\displaystyle y=f\frac{Y}{Z}\) |
@@ -1,11 +1,13 @@
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import remend from "remend";
import { parseMarkdownIntoBlocks } from "streamdown";
import { unified } from "unified";
import { describe, expect, it } from "vitest";
import { parseMathAwareMarkdownBlocks } from "@/lib/markdown-streaming-blocks";
import { remarkTexMath } from "@/lib/remark-tex-math";
import mixedMath from "@/tests/fixtures/markdown-math-mixed.md?raw";
const formula = String.raw`\[
\tan\left(\frac{\mathrm{HFOV}}{2}\right)
@@ -22,6 +24,30 @@ function expectIntact(source: string, protectedSource: string) {
}
describe("math-aware streaming blocks", () => {
it.each([
[String.raw`\(\alpha+\beta=\gamma\)`, String.raw`\alpha+\beta=\gamma`],
[String.raw`\(\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}\)`, String.raw`\nabla\cdot\vec{E}=\frac{\rho}{\varepsilon_0}`],
[String.raw`\(a\\b\)`, String.raw`a\\b`],
["\\(a\n\\alpha\\)", "a\n\\alpha"],
])("retains inline TeX data beginning with commands: %s", (source, value) => {
const parser = unified().use(remarkParse).use(remarkMath).use(remarkTexMath);
const paragraph = parser.parse(source).children[0];
expect(paragraph.type).toBe("paragraph");
if (paragraph.type !== "paragraph") throw new Error("Expected a paragraph");
expect(paragraph.children).toMatchObject([{ type: "inlineMath", value }]);
});
it("parses mixed math containers without creating empty inline-data tokens", () => {
expect(parseMathAwareMarkdownBlocks(mixedMath).join("")).toBe(mixedMath);
});
it("accepts every repaired prefix of a mixed math response", () => {
for (let end = 1; end <= mixedMath.length; end++) {
const source = remend(mixedMath.slice(0, end), { htmlTags: false });
expect(parseMathAwareMarkdownBlocks(source).join("")).toBe(source);
}
});
it("merges a TeX formula split at a Setext equals line and blank lines", () => {
const source = "Before\n\n" + formula + "\n\nAfter";
expect(parseMarkdownIntoBlocks(source).some((block) => block.includes(formula))).toBe(false);
@@ -2,6 +2,7 @@ import { render, waitFor } from "@testing-library/react";
import { expect, it } from "vitest";
import { MarkdownText } from "@/components/MarkdownText";
import mixedMath from "@/tests/fixtures/markdown-math-mixed.md?raw";
it("retains a rendered TeX formula when an assistant response completes in streaming layout", async () => {
const source = String.raw`Before
@@ -31,3 +32,38 @@ After`;
expect(container).toHaveTextContent("Before");
expect(container).toHaveTextContent("After");
});
it("renders a mixed math response through streaming prefixes and completion", async () => {
const { container, rerender } = render(
<MarkdownText streaming preserveStreamingLayout>{""}</MarkdownText>,
);
// Load the lazy renderer before exercising updates through its error boundary.
rerender(<MarkdownText streaming preserveStreamingLayout>{"# Ready"}</MarkdownText>);
await waitFor(() => expect(container.querySelector("h1")).toHaveTextContent("Ready"), {
timeout: 10_000,
});
const commandInList = String.raw`\[
x=1
\]
- \(\alpha+\beta=\gamma\)`;
for (let end = 1; end <= commandInList.length; end++) {
rerender(
<MarkdownText streaming preserveStreamingLayout>{commandInList.slice(0, end)}</MarkdownText>,
);
}
expect(container.querySelectorAll(".katex")).toHaveLength(2);
rerender(<MarkdownText streaming preserveStreamingLayout>{mixedMath}</MarkdownText>);
expect(container.querySelectorAll("h1")).toHaveLength(20);
expect(container.querySelectorAll(".katex")).toHaveLength(34);
expect(container.querySelector(".katex-error")).toBeNull();
rerender(<MarkdownText preserveStreamingLayout>{mixedMath}</MarkdownText>);
expect(container.querySelectorAll("h1")).toHaveLength(20);
expect(container.querySelectorAll(".katex")).toHaveLength(34);
expect(container.querySelector(".katex-error")).toBeNull();
expect(container.querySelectorAll("table")).toHaveLength(1);
expect(container.querySelector("blockquote")).toBeInTheDocument();
}, 30_000);