opentui-rust CodeBlock now says whether the closing fence arrived. The LaTeX renderer keeps its last formula only while the fence is open, as TS does, so broken math in a closed fence shows its source before the message completes. Removes F13-latex-closed-fence.
5.2 KiB
P.2 LaTeX plugin (opencode-latex)
opencode.latex draws latex and math Markdown fences (and tex, which resolves to latex) as Unicode math in
terminal cells, as packages/latex/src/plugin.ts does. It registers one renderer for both languages through the
P.1 code-block API.
Library choice
Plan 6.10 asks for a plugin on a Rust library. The run notes add: if no library gives terminal output close to TS, parse with a library and port only the TS layout and symbol tables.
No Rust crate lays out math in terminal cells. I checked the parser candidates:
pulldown-latex0.8.0 is the maintained KaTeX-style parser. It emits MathML-oriented events. A probe over the TS test sources (plprobe, not committed) found these problems:\left(A\rightarrow B\right)and\left(\left[A\right]\rightharpoonup B\right)fail with "expected a delimiter token". The parser reads\rightarrowas\rightplusarrow. Arrows inside\left…\rightare common in model output, and TS renders both.\displaylinesand\mboxare unknown commands.\newcommandcannot define\displaylines, because\\is rejected inside the expansion.\operatorname*{arg\,max}_{x}comes back as the function name*{arg\,max}_{x}, followed by stray events.\cfrac[l]{1}{12345}parses[l]as the numerator and draws the wrong fraction. It does not fall back to the source.- MathML artifacts reach the events:
~becomes the text ,-becomes U+2212, and text-group escapes such as\{stay raw.
latex2mathmlandkatex(JavaScript through an embedded engine) target HTML and MathML and are further from terminal output.
Mapping pulldown-latex events back to the TS structure would take as much code as the TS parser (598 lines),
and it would still need workarounds for the bugs above. So the crate ports the TS parser, layout, and tables,
and uses unicode-segmentation for graphemes (TS Intl.Segmenter). The P-ORACLE test checks every one of the
207 cases against TS. If a library later gives equal output, it can replace parse.rs behind render.
Modules
parse.rs:parser.tsandlimits.tsin strict mode, the mode the plugin uses: unsupported commands, environments, and delimiters fail. The 100 000-character source limit and the 256-level nesting limit are kept. User macros (ParseOptions.macros) are not ported: the plugin never passes any.layout.rs:layout.ts.Layoutholds cells, width, height, and baseline, andDisplaygives TStoString.OptionskeepsdisplayModeandcompactScriptsfor the oracle. The plugin uses the defaults (display math, compact scripts).symbols.rs:symbols.ts.lib.rs:plugin(),render(), andrenderer(), the Markdown adapter (markdown.ts). The formula is aTextwith no wrapping, as wide as the formula, inside aScrollBoxthat scrolls only sideways, with hidden scrollbars. TS adds one row of top margin. The Rust Markdown already puts a blank row between blocks, so the widget adds none (deviationF13-latex-leading-row). Structural glyphs (brackets, the radical, box drawing) usetheme.text.muted. Everything else usestheme.text.base. Relations are bold. A source that does not parse returnsNone, so Markdown shows the source in its code view.
Streaming
TS keeps the last frame that parsed while the fence streams, if the new source extends it. The Rust renderer
returns CodeBlock::previous (the widget it returned last for that block) when the source does not parse, the
document is streaming, and the source extends the source of that widget. It keeps a map from widget ID to source
only for widgets made while streaming. It removes the entry when the block gets a new widget.
When the message completes, Markdown::streaming(false) must run the renderer again, so that a retained preview
over broken math turns back into the source. opentui-rust skipped unchanged blocks there. Its branch
rs-plugin-latex (markdown: run code-block renderers again when streaming ends) reruns the renderers of
custom blocks with their widgets. Mermaid's last-good diagram needs the same change.
TS also checks that the fence is still open. The renderer reads that from CodeBlock::closed, so a closing fence
over broken math shows the source while the message still streams, as in TS.
Proof
- P-ORACLE:
parity/oracle/P.2-latex.tswritesparity/fixtures/P.2/layouts.json: the text, geometry, styled cells, or parse failure of 207 sources. These are every source inpackages/latex/src/*.test.ts, about 60 textbook formulas, malformed input, and the option variants.tests::layouts_match_typescriptrequires all of them to be equal. - P-COMP:
parity/component/P.2-latex.tsxrendersTextPart's<markdown>with the TS plugin's renderer over four documents: structures, the adapter cases ofmarkdown.test.ts, a wide formula at 30 columns, and invalid math next to an ordinary fence.tests::transcript_fences_match_typescriptrenders the same documents withopencode_ui::markdown::viewand this renderer. Every cell is equal, colors and attributes included. - Behavior tests port
markdown.test.ts: fallback to source, streaming retention, discard on stop, and no reuse across fences.