diff --git a/docs-md/API_REFERENCE.md b/docs-md/API_REFERENCE.md index 6f13b42cc..9837e94d6 100644 --- a/docs-md/API_REFERENCE.md +++ b/docs-md/API_REFERENCE.md @@ -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. diff --git a/docs-md/LATEX_MATH.md b/docs-md/LATEX_MATH.md index df5e81830..a9daa532e 100644 --- a/docs-md/LATEX_MATH.md +++ b/docs-md/LATEX_MATH.md @@ -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 diff --git a/docs/docs/react-native/api-reference/enriched-markdown-text.md b/docs/docs/react-native/api-reference/enriched-markdown-text.md index 141d39dea..5a78a6e55 100644 --- a/docs/docs/react-native/api-reference/enriched-markdown-text.md +++ b/docs/docs/react-native/api-reference/enriched-markdown-text.md @@ -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. diff --git a/docs/docs/rich-text-formatting/latex-math.md b/docs/docs/rich-text-formatting/latex-math.md index 865ce7637..2f564c44e 100644 --- a/docs/docs/rich-text-formatting/latex-math.md +++ b/docs/docs/rich-text-formatting/latex-math.md @@ -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. diff --git a/packages/core/cpp/enrmrkd/enrmrkd.c b/packages/core/cpp/enrmrkd/enrmrkd.c index 2e81b534c..1bcc88a89 100644 --- a/packages/core/cpp/enrmrkd/enrmrkd.c +++ b/packages/core/cpp/enrmrkd/enrmrkd.c @@ -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) @@ -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) { @@ -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]; @@ -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. */ @@ -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. */ @@ -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; } @@ -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 '!': diff --git a/packages/core/cpp/enrmrkd/enrmrkd.h b/packages/core/cpp/enrmrkd/enrmrkd.h index 46cded762..debeec7d5 100644 --- a/packages/core/cpp/enrmrkd/enrmrkd.h +++ b/packages/core/cpp/enrmrkd/enrmrkd.h @@ -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, @@ -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. */ diff --git a/packages/core/cpp/tests/fixtures/latex-delimiters.md b/packages/core/cpp/tests/fixtures/latex-delimiters.md new file mode 100644 index 000000000..0ca5ce098 --- /dev/null +++ b/packages/core/cpp/tests/fixtures/latex-delimiters.md @@ -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 +> \] diff --git a/packages/core/cpp/tests/golden/ast.txt b/packages/core/cpp/tests/golden/ast.txt index 7ade55090..997f2ac48 100644 --- a/packages/core/cpp/tests/golden/ast.txt +++ b/packages/core/cpp/tests/golden/ast.txt @@ -48,6 +48,16 @@ {"type":"Document","children":[{"type":"Paragraph","children":[{"type":"Text","content":"Intro paragraph before the video."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Video","attributes":{"url":"https://example.com/clip.mp4"}},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Paragraph between."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"UnorderedList","children":[{"type":"ListItem","children":[{"type":"Video","attributes":{"url":"https://example.com/in-list.mp4"}},{"type":"BlankLine","attributes":{"count":"1"}}]}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Inline html span inside a paragraph."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Video","attributes":{"url":"https://example.com/multiline.mp4"}}]} === html-blocks.md [extensions-on-commonmark] === {"type":"Document","children":[{"type":"Paragraph","children":[{"type":"Text","content":"Intro paragraph before the video."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Video","attributes":{"url":"https://example.com/clip.mp4"}},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Paragraph between."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"UnorderedList","children":[{"type":"ListItem","children":[{"type":"Video","attributes":{"url":"https://example.com/in-list.mp4"}},{"type":"BlankLine","attributes":{"count":"1"}}]}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Inline html span inside a paragraph."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Video","attributes":{"url":"https://example.com/multiline.mp4"}}]} +=== latex-delimiters.md [defaults-gfm] === +{"type":"Document","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"A=\\begin{pmatrix}1&1\\\\0&1\\end{pmatrix}"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\det(A-\\lambda I)=0"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Inline "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\frac{a}{b}"}]},{"type":"Text","content":" in prose, with "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\left(x\\right)"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Where"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":" \\begin{aligned} a &= b \\\\ c &= d \\end{aligned} "}]},{"type":"Paragraph","children":[{"type":"Text","content":"holds."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Dollars still work: "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"As shown in [1] and [2, 3]."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Escaped \\(x\\) stays, and so does $x$."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Path \\"},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and \\"},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"y"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"(a "},{"type":"Code","children":[{"type":"Text","content":"\\)"}]},{"type":"Text","content":" b"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Unclosed (E = mc"}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a)"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" here."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/[a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"title":"(a","url":"https://example.com"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Link","attributes":{"url":"["},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":") and "},{"type":"Link","attributes":{"url":"]"},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":")."}]},{"type":"Paragraph","children":[{"type":"Code","children":[{"type":"Text","content":"\\(x\\)"}]},{"type":"Text","content":" in code."}]},{"type":"CodeBlock","attributes":{"fenceChar":"`"},"children":[{"type":"Text","content":"\\[x\\]\n"}]},{"type":"Table","attributes":{"bodyRowCount":"1","colCount":"2","headRowCount":"1"},"children":[{"type":"TableHead","children":[{"type":"TableRow","children":[{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"a"}]},{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"b"}]}]}]},{"type":"TableBody","children":[{"type":"TableRow","children":[{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]}]},{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\alpha"}]}]}]}]}]},{"type":"Blockquote","children":[{"type":"Paragraph","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":" x^2 "}]}]}]}]} +=== latex-delimiters.md [defaults-commonmark] === +{"type":"Document","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"A=\\begin{pmatrix}1&1\\\\0&1\\end{pmatrix}"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\det(A-\\lambda I)=0"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Inline "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\frac{a}{b}"}]},{"type":"Text","content":" in prose, with "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\left(x\\right)"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Where"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":" \\begin{aligned} a &= b \\\\ c &= d \\end{aligned} "}]},{"type":"Paragraph","children":[{"type":"Text","content":"holds."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Dollars still work: "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"As shown in [1] and [2, 3]."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Escaped \\(x\\) stays, and so does $x$."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Path \\"},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and \\"},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"y"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"(a "},{"type":"Code","children":[{"type":"Text","content":"\\)"}]},{"type":"Text","content":" b"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Unclosed (E = mc"}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a)"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" here."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/[a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"title":"(a","url":"https://example.com"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"Paragraph","children":[{"type":"Link","attributes":{"url":"["},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":") and "},{"type":"Link","attributes":{"url":"]"},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":")."}]},{"type":"Paragraph","children":[{"type":"Code","children":[{"type":"Text","content":"\\(x\\)"}]},{"type":"Text","content":" in code."}]},{"type":"CodeBlock","attributes":{"fenceChar":"`"},"children":[{"type":"Text","content":"\\[x\\]\n"}]},{"type":"Paragraph","children":[{"type":"Text","content":"| a | b |"},{"type":"SoftBreak"},{"type":"Text","content":"| --- | --- |"},{"type":"SoftBreak"},{"type":"Text","content":"| "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":" | "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\alpha"}]},{"type":"Text","content":" |"}]},{"type":"Blockquote","children":[{"type":"Paragraph","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":" x^2 "}]}]}]}]} +=== latex-delimiters.md [extensions-off-gfm] === +{"type":"Document","children":[{"type":"Paragraph","children":[{"type":"Text","content":"[A=\\begin{pmatrix}1&1\\0&1\\end{pmatrix}]"}]},{"type":"Paragraph","children":[{"type":"Text","content":"[\\det(A-\\lambda I)=0]"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Inline (x^2) and (\\frac{a}{b}) in prose, with (\\left(x\\right))."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Where"},{"type":"SoftBreak"},{"type":"Text","content":"["},{"type":"SoftBreak"},{"type":"Text","content":"\\begin{aligned} a &= b \\ c &= d \\end{aligned}"},{"type":"SoftBreak"},{"type":"Text","content":"]"},{"type":"SoftBreak"},{"type":"Text","content":"holds."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Dollars still work: $x^2$ and $$x^2$$."}]},{"type":"Paragraph","children":[{"type":"Text","content":"As shown in [1] and [2, 3]."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Escaped \\(x\\) stays, and so does $x$."}]},{"type":"Paragraph","children":[{"type":"Text","content":"Path \\(x^2) and \\[y]."}]},{"type":"Paragraph","children":[{"type":"Text","content":"(a "},{"type":"Code","children":[{"type":"Text","content":"\\)"}]},{"type":"Text","content":" b"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Unclosed (E = mc"}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a)"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" here."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then (x)."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/[a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then [x]."}]},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"title":"(a","url":"https://example.com"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then (x)."}]},{"type":"Paragraph","children":[{"type":"Link","attributes":{"url":"["},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]((x)) and "},{"type":"Link","attributes":{"url":"]"},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]([x])."}]},{"type":"Paragraph","children":[{"type":"Code","children":[{"type":"Text","content":"\\(x\\)"}]},{"type":"Text","content":" in code."}]},{"type":"CodeBlock","attributes":{"fenceChar":"`"},"children":[{"type":"Text","content":"\\[x\\]\n"}]},{"type":"Table","attributes":{"bodyRowCount":"1","colCount":"2","headRowCount":"1"},"children":[{"type":"TableHead","children":[{"type":"TableRow","children":[{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"a"}]},{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"b"}]}]}]},{"type":"TableBody","children":[{"type":"TableRow","children":[{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"(x)"}]},{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"[\\alpha]"}]}]}]}]},{"type":"Blockquote","children":[{"type":"Paragraph","children":[{"type":"Text","content":"["},{"type":"SoftBreak"},{"type":"Text","content":"x^2"},{"type":"SoftBreak"},{"type":"Text","content":"]"}]}]}]} +=== latex-delimiters.md [extensions-on-gfm] === +{"type":"Document","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"A=\\begin{pmatrix}1&1\\\\0&1\\end{pmatrix}"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\det(A-\\lambda I)=0"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Inline "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\frac{a}{b}"}]},{"type":"Text","content":" in prose, with "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\left(x\\right)"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Where"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":" \\begin{aligned} a &= b \\\\ c &= d \\end{aligned} "}]},{"type":"Paragraph","children":[{"type":"Text","content":"holds."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Dollars still work: "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"As shown in [1] and [2, 3]."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Escaped \\(x\\) stays, and so does $x$."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Path \\"},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and \\"},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"y"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"(a "},{"type":"Code","children":[{"type":"Text","content":"\\)"}]},{"type":"Text","content":" b"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Unclosed (E = mc"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a)"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" here."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/[a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"title":"(a","url":"https://example.com"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Link","attributes":{"url":"["},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":") and "},{"type":"Link","attributes":{"url":"]"},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":")."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Code","children":[{"type":"Text","content":"\\(x\\)"}]},{"type":"Text","content":" in code."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"CodeBlock","attributes":{"fenceChar":"`"},"children":[{"type":"Text","content":"\\[x\\]\n"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Table","attributes":{"bodyRowCount":"1","colCount":"2","headRowCount":"1"},"children":[{"type":"TableHead","children":[{"type":"TableRow","children":[{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"a"}]},{"type":"TableHeaderCell","attributes":{"align":"default"},"children":[{"type":"Text","content":"b"}]}]}]},{"type":"TableBody","children":[{"type":"TableRow","children":[{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]}]},{"type":"TableCell","attributes":{"align":"default"},"children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\alpha"}]}]}]}]}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Blockquote","children":[{"type":"Paragraph","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":" x^2 "}]}]}]}]} +=== latex-delimiters.md [extensions-on-commonmark] === +{"type":"Document","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":"A=\\begin{pmatrix}1&1\\\\0&1\\end{pmatrix}"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\det(A-\\lambda I)=0"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Inline "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\frac{a}{b}"}]},{"type":"Text","content":" in prose, with "},{"type":"LatexMathInline","children":[{"type":"Text","content":"\\left(x\\right)"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Where"}]},{"type":"LatexMathDisplay","children":[{"type":"Text","content":" \\begin{aligned} a &= b \\\\ c &= d \\end{aligned} "}]},{"type":"Paragraph","children":[{"type":"Text","content":"holds."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Dollars still work: "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"As shown in [1] and [2, 3]."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Escaped \\(x\\) stays, and so does $x$."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Path \\"},{"type":"LatexMathInline","children":[{"type":"Text","content":"x^2"}]},{"type":"Text","content":" and \\"},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"y"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"(a "},{"type":"Code","children":[{"type":"Text","content":"\\)"}]},{"type":"Text","content":" b"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"Unclosed (E = mc"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a)"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" here."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/(a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"url":"https://example.com/[a"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"See "},{"type":"Link","attributes":{"title":"(a","url":"https://example.com"},"children":[{"type":"Text","content":"docs"}]},{"type":"Text","content":" then "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":"."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Link","attributes":{"url":"["},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":") and "},{"type":"Link","attributes":{"url":"]"},"children":[{"type":"Text","content":"a"}]},{"type":"Text","content":" ]("},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":")."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Code","children":[{"type":"Text","content":"\\(x\\)"}]},{"type":"Text","content":" in code."}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"CodeBlock","attributes":{"fenceChar":"`"},"children":[{"type":"Text","content":"\\[x\\]\n"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Paragraph","children":[{"type":"Text","content":"| a | b |"},{"type":"LineBreak"},{"type":"Text","content":"| --- | --- |"},{"type":"LineBreak"},{"type":"Text","content":"| "},{"type":"LatexMathInline","children":[{"type":"Text","content":"x"}]},{"type":"Text","content":" | "},{"type":"LatexMathDisplay","children":[{"type":"Text","content":"\\alpha"}]},{"type":"Text","content":" |"}]},{"type":"BlankLine","attributes":{"count":"1"}},{"type":"Blockquote","children":[{"type":"Paragraph","children":[{"type":"LatexMathDisplay","children":[{"type":"Text","content":" x^2 "}]}]}]}]} === windows-paths.md [defaults-gfm] === {"type":"Document","children":[{"type":"Paragraph","children":[{"type":"Text","content":"Saved to C:\\Users\\me.t3_build\\shot.png and C:/work.config and C:\\folder(draft."}]},{"type":"Paragraph","children":[{"type":"Text","content":"A drive alone keeps it too: D:.hidden"}]},{"type":"Paragraph","children":[{"type":"Text","content":"A space ends the path: C:\\tmp *not emphasis*"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Not a drive after a word: abC:\\x.y"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Code stays literal: "},{"type":"Code","children":[{"type":"Text","content":"C:\\a\\.b"}]}]},{"type":"Paragraph","children":[{"type":"Text","content":"Link text "},{"type":"Link","attributes":{"title":"t.u","url":"/p.q"},"children":[{"type":"Text","content":"C:\\x.y"}]},{"type":"Text","content":" and image "},{"type":"Image","attributes":{"url":"i.png"},"children":[{"type":"Text","content":"C:\\a.b"}]}]},{"type":"Paragraph","children":[{"type":"Text","content":"Formatting inside a path: C:/Users/"},{"type":"Strong","children":[{"type":"Text","content":"name"}]},{"type":"Text","content":".config and C:\\a_"},{"type":"Emphasis","children":[{"type":"Text","content":"b"}]},{"type":"Text","content":".c"}]},{"type":"Paragraph","children":[{"type":"Text","content":"Code ends a path: "},{"type":"Code","children":[{"type":"Text","content":"C:\\a"}]},{"type":"Text","content":".b"}]},{"type":"Paragraph","children":[{"type":"Text","content":"A line break ends it too: C:\\tmp"},{"type":"SoftBreak"},{"type":"Text","content":"*not emphasis*"}]}]} === windows-paths.md [defaults-commonmark] === diff --git a/packages/core/cpp/wasm/md4c_wasm.cpp b/packages/core/cpp/wasm/md4c_wasm.cpp index fcbf959dc..5faa880d0 100644 --- a/packages/core/cpp/wasm/md4c_wasm.cpp +++ b/packages/core/cpp/wasm/md4c_wasm.cpp @@ -14,7 +14,7 @@ extern "C" { * * @param markdown Null-terminated UTF-8 markdown input. * @param underline 1 → enable __ underline extension; 0 → __ means emphasis. - * @param latexMath 1 → enable $…$ / $$…$$ LaTeX math spans; 0 → disable. + * @param latexMath 1 → enable $…$ / $$…$$ / \(…\) / \[…\] LaTeX math spans; 0 → disable. * @param superscript 1 → enable ^superscript^ spans; 0 → disable. * @param subscript 1 → enable ~subscript~ spans; 0 → disable. * @param highlight 1 → enable ==highlight== spans; 0 → disable. diff --git a/packages/react-native-enriched-markdown/src/web/wasm/md4c.js b/packages/react-native-enriched-markdown/src/web/wasm/md4c.js index 8797d9c84..7bcc3dc12 100644 Binary files a/packages/react-native-enriched-markdown/src/web/wasm/md4c.js and b/packages/react-native-enriched-markdown/src/web/wasm/md4c.js differ