Repository navigation
feat: add KaTeX rendering and markdown parsers for AI answers 📝 - #916
Closed
Wisesofthemall wants to merge 2 commits into
Closed
Wisesofthemall wants to merge 2 commits into
Wisesofthemall wants to merge 2 commits into
Conversation
Adds the pure math helpers that decide which `$...$` spans are math and which are prices, plus code masking and LaTeX cleanup. Nothing uses them yet; they are the first piece of the fix for colorstackorg#914. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds `safeKatexRender` (falls back to the escaped source instead of KaTeX's red error output), `preprocessContent` (normalizes `\(...\)` and `\[...\]` to `$`/`$$` and strips stray HTML, never touching code), and the inline and block parsers that turn raw AI output into tokens a renderer can map to elements. Nothing uses them yet. Part of colorstackorg#914. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wisesofthemall
requested review from
levin0710,
queenellie873 and
ramiAbdou
as code owners
November 4, 2026 09:40
This was referenced Nov 4, 2026
Wisesofthemall
marked this pull request as draft
November 4, 2026 09:50
Wisesofthemall
marked this pull request as ready for review
November 4, 2026 10:06
3 of 9 tasks
Contributor
Author
|
Closing in favor of #918, which uses react-markdown instead of our own parsing logic, per the review feedback. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description ✏️
Part 2 of 3 for #914. Depends on #915. The PR comes from a fork, so it can't be stacked on #915's branch, and its diff includes #915's commit. Please review only the last commit,
6111c37, until #915 merges. Nothing uses this code yet; PR 3 switches it on.This PR turns raw AI output into structured tokens a renderer can map to elements. Everything lives in
packages/utils/src/ai-formatting/:katex.ts:safeKatexRender(latex, displayMode), exported as@oyster/utils/katexso that the main entry doesn't pull in KaTeX.katex.renderToStringwiththrowOnError: true.katex(0.19) as a dependency of@oyster/utils.preprocess.ts:preprocessContentruns once on the whole answer:\(...\)to$...$and\[...\]to$$...$$inline.ts:parseInlineproduces tokens for bold, italic, inline code, math,[ref:N]citations andhttp(s)links.`price = $5`is never treated as math.**solve $x^2$**keeps both the bold and the math.$math comes from the scanner added in feat: add math core for formatting AI answers 🧮 #915, never from a regex.5 * 3 * 2are left as text.blocks.ts:parseAiMarkdownis a line-by-line parser for:$$display math (on one line, with trailing text, or across lines)Images are shown as links, so the page never loads arbitrary image URLs.
This uses a small hand-written parser instead of a library like
marked, for two reasons:[ref:N]citations have to render as React components in the middle of a sentence.dangerouslySetInnerHTML; only KaTeX's own escaped output does.Screenshots 📸
These were taken locally on the
/ask-aipage with the full stack (#917) applied. A sample answer was stored in Redis so the page renders it without calling the AI. The sample's markdown source is in the collapsed section below the image.Markdown parsing: headings, every list style (including a numbered list that starts at 4), a quote, a divider, a table, a code block, a link, and an image shown as a link:
Sample answer source (
2-markdown-kitchen-sink.md)Math with KaTeX: inline, inside bold, display, multi-line
cases, in table cells,$7.23$shown as7.23, a repaired missing brace, and an invalid command shown as its source (no red error):On a 390px-wide phone the page doesn't scroll sideways (checked in headless Chrome).
Sample answer source (
3-math.md)Test cases 🧪
Run with
cd packages/utils && bun test(no database needed). 98 pass, 0 fail (60 from #915 plus 38 new).\(x^2\)/\[ \int f \]\frac{(invalid)<span class="math-fallback">\frac{</span>, never the red KaTeX errorw = abba \in \Sigma^*}}dropped)$w \in \Sigma^*$ and $L \subseteq \Sigma^*$*s5 * 3 * 2**solve $x^2$**the mean was $7.23$the mean was 7.23Costs $5, $10, $20 and $x$.xis mathRun `price = $5`x = $x$ * 2```mermaidwithA[Mean ($\mu$)]A{p < 0.05?} --> Bin a fence<b>Note</b>: 2 < 3 and 5 > 4Note: 2 < 3 and 5 > 41. **Apply early** [ref:2]$O(n \log n)$[click](javascript:alert(1))Full test output (new tests)
Type of Change 🐞
Checklist ✅
🤖 Generated with Claude Code