Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-md/API_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Configuration for md4c parser extension flags.
- **`superscript`**: When `true`, parses `^text^` as superscript. Visual appearance can be tuned with the `superscript` style prop — see [Superscript-specific](./STYLES.md#superscript-specific).
- **`subscript`**: When `true`, parses `~text~` as subscript. When disabled, single and double tildes remain strikethrough markers. Visual appearance can be tuned with the `subscript` style prop — see [Subscript-specific](./STYLES.md#subscript-specific).
- **`highlight`**: When `true`, parses `==text==` as highlighted spans. When disabled, double equals signs are treated as plain text. Visual appearance can be tuned with the `highlight` style prop — see [Highlight-specific](./STYLES.md#highlight-specific).
- **`latexMath`**: When `true`, parses `$...$` and `$$...$$` as LaTeX math spans.
- **`latexMath`**: When `true`, parses `$...$` and `\(...\)` as inline math and `$$...$$` and `\[...\]` as display math.
- **`hardSoftBreaks`**: When `true`, treats single newlines (soft breaks) as hard breaks, rendering them as visible line breaks instead of collapsing them to spaces. Useful when displaying content authored in `EnrichedMarkdownTextInput`, where pressing Enter produces a single newline. See [Line Breaks](./ELEMENTS_STRUCTURE.md#line-breaks) for details.
- **`admonitions`**: When `true` (default), GitHub alert blockquotes (`> [!NOTE]`, `> [!TIP]`, `> [!IMPORTANT]`, `> [!WARNING]`, `> [!CAUTION]`) render as themed callouts. Only takes effect with `flavor="github"`; ignored for CommonMark.
- **`preserveBlankLines`**: When `true`, preserves runs of consecutive blank lines from the source instead of collapsing them into a single paragraph break (per CommonMark). Each blank line renders as one empty line, so the output keeps the exact line count that was typed. See [Blank Lines](./ELEMENTS_STRUCTURE.md#blank-lines) for details.
Expand Down
4 changes: 2 additions & 2 deletions docs-md/LATEX_MATH.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

LaTeX math rendering is supported for both block and inline equations:

- **Block math (`$$...$$`)**: Rendered as a standalone display element. Requires `flavor="github"`.
- **Inline math (`$...$`)**: Rendered within the text flow. Works with both `flavor="commonmark"` and `flavor="github"`.
- **Block math (`$$...$$` or `\[...\]`)**: Rendered as a standalone display element. Requires `flavor="github"`.
- **Inline math (`$...$` or `\(...\)`)**: Rendered within the text flow. Works with both `flavor="commonmark"` and `flavor="github"`.

## Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ When `true`, parses `==text==` as highlighted spans. When disabled, double equal

#### `latexMath`

When `true`, parses `$...$` as inline math and `$$...$$` as display (block) math. Rendering on web uses [KaTeX](https://katex.org/) and natively [RaTeX](https://ratex.lites.dev/). Unlike the other flags on this page, `latexMath` is enabled by default - set it to `false` to treat dollar signs as plain text.
When `true`, parses `$...$` and `\(...\)` as inline math and `$$...$$` and `\[...\]` as display (block) math. Rendering on web uses [KaTeX](https://katex.org/) and natively [RaTeX](https://ratex.lites.dev/). Unlike the other flags on this page, `latexMath` is enabled by default - set it to `false` to treat dollar signs as plain text.

<PropInfo type="boolean" default="true" />

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/rich-text-formatting/latex-math.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import MathSrc from '!!raw-loader!@site/src/examples/react-native/rich-text-form

`EnrichedMarkdownText` renders LaTeX math natively, both inline and as block equations:

- **Inline math** (`$...$`) flows within the surrounding text and works in either flavor.
- **Block math** (`$$...$$`) renders as a standalone display equation. In the React Native package on iOS and Android, a display block needs the segmented renderer, so it requires [`flavor="github"`](/react-native/guides/markdown-flavors) - in `commonmark`, a `$$...$$` on its own line falls back to inline typesetting. The web build has no `flavor` prop and always renders `$$` as a block, and the standalone iOS and Android SDKs need no flavor switch either.
- **Inline math** (`$...$` or `\(...\)`) flows within the surrounding text and works in either flavor.
- **Block math** (`$$...$$` or `\[...\]`) renders as a standalone display equation. In the React Native package on iOS and Android, a display block needs the segmented renderer, so it requires [`flavor="github"`](/react-native/guides/markdown-flavors) - in `commonmark`, a `$$...$$` on its own line falls back to inline typesetting. The web build has no `flavor` prop and always renders `$$` as a block, and the standalone iOS and Android SDKs need no flavor switch either.

In the React Native package math parsing is **on by default**. You can turn it off so `$` is treated as plain text, and exclude the native math engine to shrink your binary - see [Reducing app size](#reducing-app-size). The standalone iOS and Android SDKs ship math as an optional add-on instead, off until you install and enable it.

Expand Down
120 changes: 118 additions & 2 deletions packages/core/cpp/enrmrkd/enrmrkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,8 @@ struct MD_MARK_tag {
#define MD_MARK_BRACKET_CANBEIMAGE 0x20 /* For '[', if can be expanded to the left to eat '!'. */
#define MD_MARK_BRACKET_HASNESTED 0x40 /* For '[' to rule out invalid link labels early. */
#define MD_MARK_BRACKET_FOOTNOTEREF 0x80 /* For '[', To distinguish footnotes. */
#define MD_MARK_LATEX_DELIMITED 0x20 /* For '$', a \( \) or \[ \] delimiter. */
#define MD_MARK_LATEX_DISPLAY 0x40 /* For '$', a \[ \] delimiter. */

static MD_MARKSTACK*
md_emph_stack(MD_CTX* ctx, ENRMRKD_CHAR ch, unsigned flags)
Expand Down Expand Up @@ -3154,6 +3156,56 @@ md_is_code_span(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, OFF beg
return true;
}

/* Find the \) or \] closing the \( or \[ at beg, reading the formula as TeX
* tokens so \\] does not close it. Like the web tokenizer, a backtick before
* the closer, or \[...\] holding only digits, commas and spaces (a citation),
* is not math. On failure, *p_fail_end is where the scan stopped: any later
* opener of the same kind before it fails the same way. */
static int
md_is_latex_delimited_math(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, OFF beg,
OFF* p_closer_beg, OFF* p_fail_end)
{
CHAR closer_ch = (CH(beg+1) == _T('(') ? _T(')') : _T(']'));
int substantive = (closer_ch == _T(')'));
OFF off = beg + 2;
ENRMRKD_SIZE line_index = 0;

while(line_index < n_lines) {
OFF line_end = lines[line_index].end;

while(off < line_end) {
CHAR ch = CH(off);

if(ch == _T('`')) {
*p_fail_end = off;
return false;
}
if(ch == _T('\\') && off+1 < line_end) {
if(CH(off+1) == closer_ch) {
if(!substantive) {
*p_fail_end = off;
return false;
}
*p_closer_beg = off;
return true;
}
off += (CH(off+1) == _T('\\') ? 2 : 1);
continue;
}
if(ch != _T('\\') && ch != _T(',') && !ISWHITESPACE_(ch) && !ISDIGIT_(ch))
substantive = true;
off++;
}

line_index++;
if(line_index < n_lines)
off = lines[line_index].beg;
}

*p_fail_end = off;
return false;
}

static int
md_is_autolink_uri(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
{
Expand Down Expand Up @@ -3267,6 +3319,10 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, int ta
MD_MARK* mark;
OFF codespan_last_potential_closers[CODESPAN_MARK_MAXLEN] = { 0 };
int codespan_scanned_till_paragraph_end = false;
OFF latex_paren_fail_end = 0;
OFF latex_bracket_fail_end = 0;
OFF latex_link_end = 0;
unsigned latex_open_brackets = 0;

for(line_index = 0; line_index < n_lines; line_index++) {
const MD_LINE* line = &lines[line_index];
Expand Down Expand Up @@ -3296,6 +3352,31 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, int ta

ch = CH(off);

/* A potential \( ... \) or \[ ... \] equation. Table rows are
* only split into cells here; each cell is collected again. */
if(ch == _T('\\') && (ctx->parser.flags & ENRMRKD_FLAG_LATEXMATHSPANS) && !table_mode &&
off+1 < line->end && ISANYOF2_(CH(off+1), _T('('), _T('['))) {
int is_display = (CH(off+1) == _T('['));
OFF* fail_end = (is_display ? &latex_bracket_fail_end : &latex_paren_fail_end);
OFF closer_beg;

if(off >= *fail_end && off >= latex_link_end &&
md_is_latex_delimited_math(ctx, line, n_lines - line_index, off, &closer_beg, fail_end))
{
unsigned flags = MD_MARK_LATEX_DELIMITED | (is_display ? MD_MARK_LATEX_DISPLAY : 0);

ADD_MARK(_T('$'), off, off+2, flags);
ADD_MARK(_T('$'), closer_beg, closer_beg+2, flags);
md_resolve_range(ctx, ctx->n_marks-2, ctx->n_marks-1);
off = closer_beg + 2;

/* Advance the current line accordingly. */
if(off > line->end)
line = md_lookup_line(off, lines, n_lines, &line_index);
continue;
}
}

/* A backslash escape.
* It can go beyond line->end as it may involve escaped new
* line to form a hard break. */
Expand Down Expand Up @@ -3473,6 +3554,8 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, int ta
ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_OPENER);
off += 1;
}
if(off-1 >= latex_link_end)
latex_open_brackets++;

/* Two dummies to make enough place for data we need if it is
* a link. */
Expand All @@ -3481,7 +3564,33 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines, int ta
continue;
}
if(ch == _T(']')) {
int is_latex_label_closer = (off >= latex_link_end);

ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_CLOSER);

/* As on web, an inline link's destination and title are read
* before any \( or \[ inside them, so math cannot start there. */
if((ctx->parser.flags & ENRMRKD_FLAG_LATEXMATHSPANS) && !table_mode &&
is_latex_label_closer && latex_open_brackets > 0 &&
off+1 < lines[n_lines-1].end && CH(off+1) == _T('('))
{
MD_LINK_ATTR attr;
OFF link_end;
int is_link = md_is_inline_link_spec(ctx, lines, n_lines, off+1, &link_end, &attr);

if(is_link < 0) {
ret = -1;
goto abort;
}
if(is_link) {
latex_link_end = link_end;
if(attr.title_needs_free)
free(attr.title);
}
}
if(is_latex_label_closer && latex_open_brackets > 0)
latex_open_brackets--;

off++;
continue;
}
Expand Down Expand Up @@ -4892,14 +5001,21 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines)
break;

case '$':
{
int is_display = (mark->flags & MD_MARK_LATEX_DELIMITED)
? (mark->flags & MD_MARK_LATEX_DISPLAY) != 0
: (mark->end - off) % 2 == 0;
ENRMRKD_SPANTYPE span = (is_display ? ENRMRKD_SPAN_LATEXMATH_DISPLAY : ENRMRKD_SPAN_LATEXMATH);

if(mark->flags & MD_MARK_OPENER) {
MD_ENTER_SPAN((mark->end - off) % 2 ? ENRMRKD_SPAN_LATEXMATH : ENRMRKD_SPAN_LATEXMATH_DISPLAY, NULL);
MD_ENTER_SPAN(span, NULL);
text_type = ENRMRKD_TEXT_LATEXMATH;
} else {
MD_LEAVE_SPAN((mark->end - off) % 2 ? ENRMRKD_SPAN_LATEXMATH : ENRMRKD_SPAN_LATEXMATH_DISPLAY, NULL);
MD_LEAVE_SPAN(span, NULL);
text_type = ENRMRKD_TEXT_NORMAL;
}
break;
}

case '[': /* Footnote reference, link, wiki link, or image. */
case '!':
Expand Down
4 changes: 2 additions & 2 deletions packages/core/cpp/enrmrkd/enrmrkd.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ typedef enum ENRMRKD_SPANTYPE {
*/
ENRMRKD_SPAN_DEL,

/* For recognizing inline ($) and display ($$) equations
/* For recognizing inline ($, \(...\)) and display ($$, \[...\]) equations
* Note: Recognized only when ENRMRKD_FLAG_LATEXMATHSPANS is enabled.
*/
ENRMRKD_SPAN_LATEXMATH,
Expand Down Expand Up @@ -399,7 +399,7 @@ typedef struct ENRMRKD_BLOCK_BLANK_DETAIL {
#define ENRMRKD_FLAG_STRIKETHROUGH 0x200 /* Enable strikethrough extension. */
#define ENRMRKD_FLAG_PERMISSIVEWWWAUTOLINKS 0x400 /* Enable WWW autolinks (even without any scheme prefix, if they begin with 'www.') */
#define ENRMRKD_FLAG_TASKLISTS 0x800 /* Enable task list extension. */
#define ENRMRKD_FLAG_LATEXMATHSPANS 0x1000 /* Enable $ and $$ containing LaTeX equations. */
#define ENRMRKD_FLAG_LATEXMATHSPANS 0x1000 /* Enable $, $$, \( \) and \[ \] containing LaTeX equations. */
#define ENRMRKD_FLAG_WIKILINKS 0x2000 /* Enable wiki links extension. */
#define ENRMRKD_FLAG_UNDERLINE 0x4000 /* Enable underline extension (and disables '_' for normal emphasis). */
#define ENRMRKD_FLAG_HARD_SOFT_BREAKS 0x8000 /* Force all soft breaks to act as hard breaks. */
Expand Down
47 changes: 47 additions & 0 deletions packages/core/cpp/tests/fixtures/latex-delimiters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
\[A=\begin{pmatrix}1&1\\0&1\end{pmatrix}\]

\[\det(A-\lambda I)=0\]

Inline \(x^2\) and \(\frac{a}{b}\) in prose, with \(\left(x\right)\).

Where
\[
\begin{aligned} a &= b \\ c &= d \end{aligned}
\]
holds.

Dollars still work: $x^2$ and $$x^2$$.

As shown in \[1\] and \[2, 3\].

Escaped \\(x\\) stays, and so does \$x\$.

Path \\\(x^2\) and \\\[y\].

\(a `\)` b

Unclosed \(E = mc

See [docs](https://example.com/\(a\)) here.

See [docs](https://example.com/\(a) then \(x\).

See [docs](https://example.com/\[a) then \[x\].

See [docs](https://example.com "\(a") then \(x\).

[a]([) ](\(x\)) and [a](\]) ](\[x\]).

`\(x\)` in code.

```
\[x\]
```

| a | b |
| --- | --- |
| \(x\) | \[\alpha\] |

> \[
> x^2
> \]
Loading