Skip to content

feat(render): send LaTeX formulas as native math blocks - #303

Merged
grinev merged 2 commits into
grinev:mainfrom
Ptah9:feat/native-math-blocks
Nov 21, 2026
Merged

grinev merged 2 commits into
grinev:mainfrom
Ptah9:feat/native-math-blocks

Conversation

@Ptah9

@Ptah9 Ptah9 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Description of changes

  • Parse math / latex / ```tex fences as dedicated math blocks instead of code blocks.
  • Normalize $$ ... $$ blocks into those fences before parsing, so a model can emit either form.
  • Map the new block to Telegram's mathematical_expression rich block, keeping a plain-text projection and a MarkdownV2 fallback that emits the LaTeX source verbatim.
  • Inline $...$ is out of scope: only block-level formulas are converted, so ordinary text with a dollar sign is untouched.

Closes issue (optional)

How it was tested

  • Added unit tests for all three layers: fence parsing, $$ normalization, and end-to-end pipeline output.
  • npm run lint, npm run build, npm run typecheck — clean.
  • npm test — 228 files, 3552 passed, 7 skipped.
  • Checked against upstream v0.28.0: it produces a pre block for ```math and a plain paragraph for $$ ... $$, so no existing behavior is duplicated.
  • Confirmed manually in a Telegram client: fractions and roots render as native formulas.

User-visible impact

Formulas written as ```math fences or $$ ... $$ blocks now render as native Telegram formulas instead of raw LaTeX text. Other code fences are unchanged.

Note on #300 (copyable replies)

Formula rendering relies on rich messages: Telegram has no math markup in MarkdownV2 or entities. If the transport moves to a copyable format, formulas degrade to their LaTeX source (already handled in the MarkdownV2 fallback) rather than break.

Checklist

  • PR title follows Conventional Commits: <type>(<scope>)?: <description>
  • This PR contains one logically complete change
  • Branch is rebased on the latest main
  • I ran npm run lint, npm run build, and npm test
  • Not OS-sensitive: no paths, processes, or filesystem access involved

Telegram Bot API 10.1 renders formulas in rich messages, but the renderer
turned every math fence into a pre block and left $$ ... $$ as plain text.

- parse ```math / ```latex / ```tex fences as dedicated math blocks
- normalize $$ ... $$ blocks into those fences before parsing
- map the math block to Telegram's mathematical_expression
- keep a plain-text projection and a MarkdownV2 fallback
@grinev

grinev commented Nov 17, 2026

Copy link
Copy Markdown
Owner

@Ptah9 thanks for adding native formula rendering. Before merging, please fix these two regressions in markdown-normalizer.ts:

  1. major — Formula normalization changes literal code (lines 90–114).
    The $$ conversion also runs inside code structures that the normalizer does not recognize. For example, a ~~~text block containing $$x^2$$ gets extra Markdown fences inserted into its content. A code block indented by four spaces becomes a formula instead. Both examples preserve their original code content on the base branch.
    Please exclude code from formula normalization, including tilde-fenced and indented code blocks, and add regression tests for these cases.

  2. major — Formula normalization breaks list structure (lines 92–114).
    The generated math fences always start without indentation. When a formula belongs to a list item, this moves it outside the item and splits the list. In a reproduced example with a formula and a continuation paragraph inside the first numbered item, the second item is displayed as 1. instead of 2..
    Please preserve the formula’s nesting within the list. Add a regression test covering a numbered item with a formula, a continuation paragraph, and a second item.

The existing tests, lint, and TypeScript checks pass, but additional checks reproduce both problems. These should be fixed before merging because they change code content and the structure of assistant replies.

Skip backtick and tilde fences plus indented code when converting \$\$ blocks. Keep the fence indentation so a formula stays inside the list item, and strip that indent from the expression. Regression tests cover tilde fences, four-space indented code and ordered items with a formula.
@Ptah9

Ptah9 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@grinev thanks for the reproductions, fixed both.

  • Formula normalization now recognizes backtick and tilde fences and skips
    indented code, so $$ is never converted inside literal code.
  • Generated math fences keep the indentation of the original line and the
    expression is de-indented by the same amount, so a formula inside a list item
    stays inside it. The continuation paragraph and the second item are untouched.

Regression tests added: a ~~~ fence containing $$x^2$$, a four-space indented
$$ block, a numbered item with a formula plus continuation paragraph and a
second item (checked through parseTelegramBlocks, so the list keeps two items
and the formula lands in the first), and a multi-line $$ block inside a list
item.

npm run lint, npm run build, npm run typecheck and npm test
(228 files, 3556 passed, 7 skipped) are clean.

@grinev

grinev commented Nov 17, 2026

Copy link
Copy Markdown
Owner

@Ptah9 thanks for addressing the previous feedback. The original examples now work correctly, including formulas inside numbered lists. However, code protection is still incomplete. Please fix these three remaining issues before merging:

  1. major — Code fences are closed too early (markdown-normalizer.ts, lines 107–114).
    The normalizer checks only the fence character, not its length or whether the rest of the closing line is valid. Inside a four-backtick block, a three-backtick line incorrectly ends code protection. A following $$x^2$$ is then rewritten inside literal code. A line such as ~~~not-a-closing-fence causes the same problem.
    Please track the opening fence length and validate closing fences according to Markdown rules. Add regression tests for both cases.

  2. major — Formula conversion runs inside multiline inline code (lines 147–149).
    A valid code span such as begin\n$$x^2$$\nend is split into ordinary text and a native formula instead of remaining code. Here, \n represents actual line breaks.
    Please exclude code spans from formula normalization and add a regression test.

  3. major — The closing $$ search crosses code blocks (lines 154–179).
    If an unmatched $$ appears before a fenced code block, the normalizer can use a $$ inside that code block as the closing delimiter. This changes the code content and can also break the structure of the following text.
    Please make delimiter matching respect Markdown boundaries and exclude delimiters inside code.

Build, lint, typecheck, and all existing tests pass. Additional checks reproduce these issues and confirm that the base commit preserves the original content.

@grinev
grinev merged commit 497b711 into grinev:main Nov 21, 2026
3 checks passed
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.

[Feature]: support formula blocks in rich messages (LaTeX)

2 participants