Skip to content

Render math in docstring descriptions as KaTeX HTML - #905

Open
mishig25 wants to merge 1 commit into
mainfrom
claude/hungry-noether-72f49e
Open

mishig25 wants to merge 1 commit into
mainfrom
claude/hungry-noether-72f49e

Conversation

@mishig25

@mishig25 mishig25 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Problem

Math in docstring parameter and return descriptions renders as literal text instead of math. For example, on AnyFlowTransformer3DModel, the gate_value parameter shows the text {@html "<span class="\"katex\"> followed by raw KaTeX markup with broken attributes like class="\&quot;mord" mathnormal\". The sample parameter on FlowMapEulerDiscreteScheduler is broken the same way. Both come from :math: in diffusers docstrings, which doc-builder converts to \\(…\\); $…$ breaks the same way.

Cause

renderKatex in kit/preprocessors/mdsvex/index.js wraps every rendered formula in a Svelte {@html "…"} tag. That's right for the doc body of a page, which is Svelte markup. But docstring.js renders each docstring section as a separate mdsvex fragment, and the result is an HTML string that Docstring.svelte displays with {@html description}. The nested {@html tag is never evaluated: the browser shows it as text and parses the JSON-escaped quotes inside it as attribute values.

Fix

  • When the content starts with DOCSTRING_FRAGMENT_MARKER, renderKatex returns the plain KaTeX HTML. Page bodies still get {@html …}.
  • docstring.js rewrites }} as }&zwnj;} in parameter descriptions. That made KaTeX fail on any }} in parameter math, e.g. $\frac{1}{\sqrt{r}}$, and the error stops the build. renderKatex now restores }} before rendering.

Verification

  • Snapshot: a new mix_embeddings sample covers $…$ and :math: in parameter descriptions, the }} case, and math in a return description. No existing line of page.html changes, including the body math of legacy_function. With main, the sample renders the same broken markup as the diffusers page; on this branch it's KaTeX.
  • Unit tests: two new tests in mdsvex/index.test.js, one for page-body math and one for fragment math. The fragment test fails on main.
  • Page bodies: with main and with this branch, I ran the mdsvex preprocessor on transformers' perplexity.md, on a synthetic page with every math form ($…$, \\(…\\), $$…$$, braces, math in a table), and on the full snapshot page. The output is byte-identical.
  • Round trip: parseRenderedParamsList re-serializes the KaTeX HTML. For \frac{1}{\sqrt{r}}, the result is identical to KaTeX's own output except that the zero-width space comes back as &#x200B;, which the browser decodes to the same character. The SVG of \sqrt is kept.
  • Checks: npm test passes (12/12), as do pytest tests/test_docstring_snapshots.py, ruff and prettier. The 3 ESLint errors in mdsvex/index.js are already on main.

Once diffusers' docs rebuild, these blocks become translatable: #903 keeps them in English until then.

🤖 Generated with Claude Code

Docstring fragments (parameter, return and raise descriptions) are HTML
strings that the Docstring component renders with {@html}, so the
{@html "…"} tag that renderKatex wrapped math in was shown as text, with
its quotes escaped again. Fragments now get the plain KaTeX HTML.

docstring.js rewrites `}}` to `}&zwnj;}` in parameter descriptions, so any
`}}` in their math (e.g. \frac{1}{\sqrt{r}}) made KaTeX fail the build:
renderKatex restores it before rendering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant