fix(webui): fit wide equations and refine math spacing (#6075)

* fix(webui): fit wide equations and refine math spacing

* refactor(webui): colocate display math styles with its component

* style(webui): trim math stylesheet trailing whitespace

* fix(webui): tighten spacing after display equations

* fix(webui): tighten spacing for equations within lists

* fix(webui): center responsive display equations
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-06 02:16:04 +08:00
1 parent d9ceae15be
commit 07c4e3f0d1
4 files changed
+181

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
.math-fit {
position: relative;
display: block;
width: 100%;
min-width: 0;
margin-block: 0.45em 0.5em;
}
.math-fit-content {
position: absolute;
top: 0;
left: 50%;
width: max-content;
padding-block: 0.1em;
transform-origin: top center;
}
.math-fit .katex-display {
margin: 0;
}
.math-fit .katex-display > .katex {
font-size: 1.1em;
}
/* Math-heavy prose uses a tighter reading rhythm than ordinary chat. */
.markdown-content:has(.math-fit) {
--cjk-line-height: 1.65;
}
/* Keep list formulas close to their explanations. */
.markdown-content li .math-fit {
margin-block: 0.3em 0.35em;
}
.markdown-content li > p:has(.math-fit) {
margin-block: 0;
}
.markdown-content li:has(.math-fit) {
margin-block: 0.35em;
}
.markdown-content li:has(.math-fit) > p:first-child:not(:has(.math-fit)) {
margin-bottom: 0.3em;
font-weight: 500;
}
/* Unindented Markdown equations may follow a list as sibling blocks. */
.markdown-content :is(ol, ul):has(+ .math-fit) {
list-style-position: outside;
padding-left: 1.5em;
margin-top: 0.85em;
margin-bottom: 0.3em;
}
.markdown-content :is(ol, ul):has(+ .math-fit) > li {
font-weight: 500;
}
.markdown-content :is(ol, ul) + .math-fit:not([hidden]) {
width: calc(100% - 1.5em);
margin-left: 1.5em;
margin-top: 0.3em;
margin-bottom: 0.35em;
}
.markdown-content .math-fit + :is(ol, ul):has(+ .math-fit) {
margin-top: 0.35em;
}
/* Typography paragraph margins otherwise add a full line after equations. */
.markdown-content .math-fit:not([hidden]) + p,
.markdown-content :is(ol, ul):has(> li:last-child > .math-fit:last-child) + p {
margin-top: 0.5em;
}
+31
View File
@@ -0,0 +1,31 @@
import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
import "./DisplayMath.css";
export function DisplayMath({ children }: { children: ReactNode }) {
const frame = useRef<HTMLSpanElement>(null);
const content = useRef<HTMLSpanElement>(null);
const [layout, setLayout] = useState({ scale: 1, height: 0 });
useLayoutEffect(() => {
const update = () => {
if (!frame.current || !content.current) return;
const width = content.current.offsetWidth;
const scale = width ? Math.min(1, frame.current.clientWidth / width) : 1;
const height = content.current.offsetHeight * scale;
setLayout(previous => previous.scale === scale && previous.height === height ? previous : { scale, height });
};
update();
const observer = new ResizeObserver(update);
observer.observe(frame.current!);
observer.observe(content.current!);
let cancelled = false;
void document.fonts?.ready.then(() => { if (!cancelled) update(); });
return () => { cancelled = true; observer.disconnect(); };
}, [children]);
return <span ref={frame} className="math-fit" style={{ height: layout.height || undefined }}
data-math-scale={layout.scale}>
<span ref={content} className="math-fit-content" style={{ transform: `translateX(-50%) scale(${layout.scale})` }}>
<span className="katex-display">{children}</span>
</span>
</span>;
}
@@ -16,6 +16,7 @@ import remend from "remend";
import { parseMathAwareMarkdownBlocks } from "@/lib/markdown-streaming-blocks";
import { DisplayMath } from "@/components/DisplayMath";
import { AttachmentTile } from "@/components/AttachmentTile";
import { CodeBlock } from "@/components/CodeBlock";
import { WebLink } from "@/components/WebLink";
@@ -689,6 +690,13 @@ export default function MarkdownTextRenderer({
</WebLink>
);
},
span({ children: spanChildren, className: spanClassName, node: _node, ...props }) {
void _node;
if (spanClassName?.split(" ").includes("katex-display")) {
return <DisplayMath>{spanChildren}</DisplayMath>;
}
return <span className={spanClassName} {...props}>{spanChildren}</span>;
},
// Streamdown decorates emphasis with spans by default. Preserve native
// semantics for accessibility and predictable typography.
strong({ children: markdownChildren, node: _node, ...props }) {
+66
View File
@@ -0,0 +1,66 @@
import { act, render } from "@testing-library/react";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { DisplayMath } from "@/components/DisplayMath";
let availableWidth: number;
let formulaWidth: number;
let formulaHeight: number;
let resize: () => void;
beforeEach(() => {
availableWidth = 1000;
formulaWidth = 800;
formulaHeight = 80;
vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockImplementation(function (this: HTMLElement) {
return this.classList.contains("math-fit") ? availableWidth : 0;
});
vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(function (this: HTMLElement) {
return this.classList.contains("math-fit-content") ? formulaWidth : 0;
});
vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockImplementation(function (this: HTMLElement) {
return this.classList.contains("math-fit-content") ? formulaHeight : 0;
});
vi.stubGlobal("ResizeObserver", class {
constructor(callback: () => void) { resize = callback; }
observe() {}
disconnect() {}
});
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it("fits a wide formula on resize and restores its size without enlarging it", () => {
const { container } = render(<DisplayMath><span>long equation</span></DisplayMath>);
const frame = container.querySelector<HTMLElement>(".math-fit")!;
const formula = container.querySelector<HTMLElement>(".math-fit-content")!;
expect(frame.style.height).toBe("80px");
expect(formula.style.transform).toContain("scale(1)");
act(() => { availableWidth = 400; resize(); });
expect(frame.style.height).toBe("40px");
expect(formula.style.transform).toContain("scale(0.5)");
expect(frame.textContent).toBe("long equation");
act(() => { availableWidth = 1200; resize(); });
expect(frame.style.height).toBe("80px");
expect(formula.style.transform).toContain("scale(1)");
});
it("recalculates width and occupied height when a streaming formula grows", () => {
availableWidth = 400;
formulaWidth = 200;
formulaHeight = 40;
const { container, rerender } = render(<DisplayMath><span>initial equation</span></DisplayMath>);
const frame = container.querySelector<HTMLElement>(".math-fit")!;
expect(frame.style.height).toBe("40px");
formulaWidth = 800;
formulaHeight = 120;
rerender(<DisplayMath><span>completed equation</span></DisplayMath>);
expect(frame.style.height).toBe("60px");
expect(container.querySelector<HTMLElement>(".math-fit-content")!.style.transform).toContain("scale(0.5)");
expect(frame.textContent).toBe("completed equation");
});