ant-design / ant-design/x-markdown-mini

[Bug] LaTeX block formulas fail when $$ delimiters share a line with content or environments

Open
#19 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
28
Forks
5
Avg merge
3m
Merged PRs (30d)
1

Description

## Environment

- Package: `@ant-design/x-markdown-mini`
- Version: `1.1.0`
- Platform: WeChat Mini Program
- WeChat DevTools: `2.02.2609082`
- OS: macOS
- LaTeX plugin: enabled

## Description

The LaTeX plugin only recognizes block formulas when both `$$` delimiters are placed on separate lines.

Common Markdown produced by LLMs or other Markdown tools may use compact block-math syntax such as:

```latex
$$x^2+y^2=z^2$$
```

or:

```latex
$$\begin{cases}
a = 1 \\
b = 2
\end{cases}$$
```

These formulas are not recognized as block formulas correctly.

The second case can also interfere with parsing of the formula that follows it, causing subsequent inline LaTeX to be displayed as raw Markdown.

## Minimal reproduction

```js
import { XMarkdownMini } from '@ant-design/x-markdown-mini'
import Latex from '@ant-design/x-markdown-mini/plugins/Latex'

const md = new XMarkdownMini({
escapeText: false,
extensions: [Latex()],
})

const markdown = String.raw`
在两个三角形中,

$$\begin{cases}
BD = CD \\
BE = CF
\end{cases}$$

$\therefore \triangle BDE \cong \triangle CDF$
`

console.log(md.parse(markdown))
```

The same issue occurs when passing this content to the WeChat component:

```xml

```

## Actual behavior

1. `$$x^2+y^2=z^2$$` is treated as inline math instead of block math.
2. `$$\begin{cases}` and `\end{cases}$$` may be displayed as raw Markdown.
3. A following inline formula may also fail to render because the unmatched dollar delimiters affect subsequent tokenization.

Example of the raw output:

```text
$$\begin{cases}
BD = CD \quad (\text{已知}) \\
BE = CF \quad (\text{已知})
\end{cases}$$
$\therefore \triangle BDE \cong \triangle CDF$
```

## Expected behavior

All of the following commonly used block-math forms should produce a `blockKatex` token and render as display math:

```latex
$$
x^2+y^2=z^2
$$
```

```latex
$$x^2+y^2=z^2$$
```

```latex
$$\begin{cases}
a = 1 \\
b = 2
\end{cases}$$
```

The formula following a block formula should continue to be tokenized independently.

## Current workaround

Before passing Markdown to `x-markdown-mini`, we normalize compact delimiters:

```latex
$$\begin{cases}
...
\end{cases}$$
```

into:

```latex
$$
\begin{cases}
...
\end{cases}
$$
```

We also convert same-line display formulas:

```latex
$$x^2+y^2=z^2$$
```

into:

```latex
$$
x^2+y^2=z^2
$$
```

This workaround fixes the rendering, but every consumer currently needs to preprocess its Markdown.

## Suggested fix

It may be helpful to update the LaTeX block tokenizer so that:

1. `$$...$$` is recognized as block math even when the delimiters and content share a line.
2. Opening and closing delimiters attached to `\begin{...}` and `\end{...}` are accepted.
3. The inline tokenizer does not consume the first `$` of a `$$` block expression.
4. Compact block formulas are covered in both normal and streaming parsing tests.

Thank you!

Contributor guide

Open the contributing guide

Research direction

Start at the LaTeX plugin's block tokenizer and reproduce the issue with XMarkdownMini.parse using compact $$ delimiters. Check the normal and streaming parsing tests; done means compact block formulas produce blockKatex tokens and a following inline formula is tokenized independently.

Written by the indexing model from the issue text.

Assessment

Tech stack
latex, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.