Skip to content

fix(documents): rescue dollar-delimited math in AI-written HTML - #838

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixdocuments-rescue-dollar-delimited-309ac7
Draft

posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixdocuments-rescue-dollar-delimited-309ac7

Conversation

@posthog

@posthog posthog Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  • Study notes are the product's core output, and the AI wrote math as plain text on the only path that saves them — a math-and-science app getting formulas wrong at the thing users came for.
  • parseDocumentAiHtml rescues <sub>/<sup> and h5/h6, but nothing repairs math. Math the model writes as $…$ or $$…$$ is stored as literal text; only display equations already in their own math block survived.
  • The tool prompt already tells the model to use Tiptap math markup and avoid dollar signs, but that is prompt-only. Wording was tightened once before and did not hold.

Changes

  • Mechanism: a deterministic rescue in rewriteLossyElements, beside the existing sub/sup handling, converts dollar-delimited math into the math nodes the schema renders.
Input in model HTML Result
$E = mc^2$ inline math node (<span data-type="inline-math">)
$$\int_0^1 x^2 dx$$ block math node (<div data-type="block-math">)
$5 and $10, prose left as text (mathLikeInlineBody guards money)
math inside <code>, <pre>, widget source left as text
  • An inline pair converts only when its body reads as math; a $$…$$ pair is display math with no money reading, so a non-empty body is enough.
  • Moved the mathLikeInlineBody heuristic to a neutral module (src/lib/math-like-text.ts) shared by the chat Markdown normalizer and the document parser, so the document model no longer imports from the chat UI folder.
  • Flashcards and quizzes parse through the same parseDocumentAiHtml, so they get the rescue too.

Testing

  • pnpm check (format, lint, types) — pass.
  • New unit tests for inline/block rescue, money left alone, and code/widget text kept literal.
  • pnpm vitest --run over documents, content, flashcards, quizzes — 81 pass.

Closes #837


Created with PostHog Desktop from this inbox report, addressing #837.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Review in cubic

The document write path parses model HTML through parseDocumentAiHtml, which
rescues sub/sup but never math. Math written as `$…$` or `$$…$$` was stored as
literal text, so generated notes showed plain-text formulas.

Add a deterministic rescue beside the sub/sup handling in rewriteLossyElements:
convert `$…$` into inline math nodes and `$$…$$` into block math nodes. Skip
code, pre, widget source, and existing math nodes, and leave plain money like
`$5 and $10` alone by reusing the mathLikeInlineBody heuristic.

Move mathLikeInlineBody to a neutral module (src/lib/math-like-text.ts) so the
document model does not import from the chat UI folder. Flashcards and quizzes
share parseDocumentAiHtml, so they get the same rescue.

Generated-By: PostHog Desktop
Task-Id: 194509e0-b536-46b2-a3e3-39b8ed77620d
@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: f170cd89-dedb-4541-9bf9-dd6897f5f033

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

React Doctor found no new issues. 🎉

Reviewed by React Doctor for commit 49fd2c2.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Math formatting is still sometimes missed

0 participants