From 072e3465f17d05413e1a5d5525edbaa2fb3bb767 Mon Sep 17 00:00:00 2001 From: Chinesezjc Date: Fri, 18 Sep 2026 14:24:01 +0800 Subject: [PATCH] refactor(web): drop the hand-rolled verbatim scan from the dollar flow The `\verb` state machine added for a `%` inside verbatim regions produced its own defect class: its lookahead rescanned the line for every unterminated candidate (58 KB of them took ~2 s), a `%` taken as a candidate delimiter lost its comment state on the fallback, and control words, bare letters, and the starred delimiter each needed a rule. It is gone; the comment gate alone keeps `$$\verb|%|$$` and the rest of a 17-case parity set at base behavior. What replaces it is the single-line rule the flow needs anyway: a `$$` that failed to close continues scanning while it is still on the opening line, so a later `$$` there still closes (`$$\verb*a$$a$$`), and it ends the candidate at that line's ending (or immediately, on a later line), so `$$x$$ trailing` and the repeated-candidate inputs keep the behavior earlier rounds pinned. Net: 12 fewer branches in the tokenizer, linear parser work, and 132 tests with 100% statements, branches, functions, and lines. --- ...17-multiline-display-dollar-math.i18n.yaml | 4 +- ...026-09-17-multiline-display-dollar-math.md | 12 +- ...-09-17-multiline-display-dollar-math.zh.md | 10 +- .../src/markdown/mathCompatibility.ts | 121 ++---------------- .../tests/markdown.client.spec.tsx | 74 +++++------ 5 files changed, 57 insertions(+), 164 deletions(-) diff --git a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.i18n.yaml b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.i18n.yaml index 99f7ff3a88..015a2f6a83 100644 --- a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.i18n.yaml +++ b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.md -2026-09-17-multiline-display-dollar-math.md: 7e54188e2839505e22984a9ce46647f1b4aff705 -2026-09-17-multiline-display-dollar-math.zh.md: baa9f28b0c32bd6090bd50201be529c8bf8d4f7d +2026-09-17-multiline-display-dollar-math.md: 49640d64a9b6fe5635d3ee84c08806f48f55589a +2026-09-17-multiline-display-dollar-math.zh.md: 07d4eaff3f7ad0c0975ca0c65ecf9038d03946b9 diff --git a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.md b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.md index 7e54188e28..49640d64a9 100644 --- a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.md +++ b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.md @@ -21,7 +21,7 @@ In the session that surfaced this, one chat reply lost lines 22–55 of 55 to a [mathCompatibility.ts](../../../../packages/client/ui-primitives/src/markdown/mathCompatibility.ts) gains multi-line display-dollar blocks. Its dollar construct keeps the opening line's text in `mathFlowValue` instead of fence meta and closes at a trailing `$$` on any later content line, so `$$a+b` continued by `c+d$$` becomes one math node. -It ends the attempt when a `$$` sequence is not followed by the line's end, on the opening line or any later one (`$$x$$ trailing`), because scanning on from there would consume a later block's closing fence. A solitary `$` is content rather than a failed fence, so a formula that uses one — `$$P = \begin{cases} 1 & \text{if $x>0$} \end{cases}$$` — keeps the block open and closes normally wherever this flow owns the block. A trailing backslash escapes the line ending, not the next line's first character, so the escape run ends at the line ending instead of reaching into the next candidate. An unescaped `%` starts a TeX comment that runs to the line ending, so a `$$` at the end of a later line closes nothing when the comment holds text before it. A `%` whose text before the fence is empty or whitespace (`95%$$`, `95 %$$`) holds no comment text, and a block that opens and closes on one line keeps closing at that line's `$$`, as it did before this flow spanned lines. A `\verb…` region is verbatim, so a `%` inside one is content; the delimiter cannot be a letter, one optional `*` marks the starred form, and the region must close before the line ending or the characters read so far stay ordinary content. The comment state and its gate on the closing check belong to the dollar flow; the escape run and the verbatim scan are shared with the backslash construct. +It ends the attempt at the line ending when a `$$` sequence failed to close on that line (`$$x$$ trailing`), because scanning past it would consume a later block's closing fence; within the opening line the scan continues, so a later `$$` on that same line still closes. A solitary `$` is content rather than a failed fence, so a formula that uses one — `$$P = \begin{cases} 1 & \text{if $x>0$} \end{cases}$$` — keeps the block open and closes normally wherever this flow owns the block. A trailing backslash escapes the line ending, not the next line's first character, so the escape run ends at the line ending instead of reaching into the next candidate. An unescaped `%` starts a TeX comment that runs to the line ending, so a `$$` at the end of a later line closes nothing when the comment holds text before it. A `%` whose text before the fence is empty or whitespace (`95%$$`, `95 %$$`) holds no comment text, and a block that opens and closes on one line keeps closing at that line's `$$`, as it did before this flow spanned lines. That state and its gate on the closing check belong to the dollar flow. Verbatim regions are not parsed: only the shapes above decide, which keeps `\verb`-bearing formulas on the same path as every other value. Its paragraph-interruption check declines instead of consuming the line ending, for the dollar construct only. Consuming it there keeps the paragraph open across the line boundary, so the document layer keeps the container and counts the next line lazy for every construct tried afterwards; that made upstream's `mathFlow`, the construct tried after this one, end empty inside a list item. Declining also leaves the interruption decision to that upstream flow, whose own check short-circuits without consuming and claims only the fences it accepts, so a candidate neither construct accepts (`**before` followed by `$$a\$b`) leaves its paragraph intact. The backslash construct has no competing upstream construct and keeps consuming there. @@ -47,18 +47,18 @@ Its paragraph-interruption check declines instead of consuming the line ending, The three shapes now produce the same math node, so formulas stop truncating at the opening line and stop swallowing the prose after them. `$$` fence meta is no longer honored: `$$asciimath` parses as a formula whose first line is `asciimath`, which upstream's flow would have split into meta plus body. That spelling has no user in this repository, and the renderer never read `meta` anyway. -Five inputs still reach upstream's flow or stay in the surrounding paragraph, because the compatibility flow declines them. Trailing text after a closing `$$` (`$$x$$ trailing`, or a CJK quote mark closing a line of prose) resolves through upstream inline math, as the existing `markdown.client.spec.tsx` case pins — including when a later `$$` block follows. A `$$` sequence the line does not end after, met anywhere inside a block, declines that whole block to the same fallback. A `$$` block left open runs to the next lone `$$` or to EOF exactly as it did before this change, because the upstream flow closes at EOF. +Six inputs still reach upstream's flow or stay in the surrounding paragraph, because the compatibility flow declines them. Trailing text after a closing `$$` (`$$x$$ trailing`, or a CJK quote mark closing a line of prose) resolves through upstream inline math, as the existing `markdown.client.spec.tsx` case pins — including when a later `$$` block follows. A `$$` sequence the line does not end after, met anywhere inside a block, declines that whole block to the same fallback. A `$$` block left open runs to the next lone `$$` or to EOF exactly as it did before this change, because the upstream flow closes at EOF. -The fourth input is a multi-line block whose opening line carries a solitary `$` and that must interrupt a paragraph, with no blank line before it. Both flows decline the interruption there — this one by design, upstream because its fence meta stops at the `$` — so the block stays inside the paragraph and renders as inline math or not at all. Separating the block from the paragraph with a blank line makes this flow own it and render the display block. +The fourth is a multi-line block whose opening line carries a solitary `$` and that must interrupt a paragraph, with no blank line before it. Both flows decline the interruption there — this one by design, upstream because its fence meta stops at the `$` — so the block stays inside the paragraph and renders as inline math or not at all. Separating the block from the paragraph with a blank line makes this flow own it and render the display block. -A sixth shape follows from the comment gate: a block whose only closing fence sits behind a comment that holds text keeps scanning instead of closing there, so when nothing else closes it the block reaches the upstream flow as the shapes above do. A fence behind a whitespace-only comment closes the block there, which leaves a following lone `$$` as its own empty block where the flow it replaced kept one node. +The fifth follows from the comment gate: a block whose only closing fence sits behind a comment that holds text keeps scanning instead of closing there, so when nothing else closes it the block reaches the upstream flow as the shapes above do. A fence behind a whitespace-only comment closes the block there, which leaves a following lone `$$` as its own empty block where the flow it replaced kept one node. -The fifth is a multi-line block inside a container whose continuation line falls back to the container's indentation. `- Define:` followed by `$$f(x)=x` at the item's indentation and then a de-indented continuation line gives upstream's flow a fence meta and no value, so the reader sees an empty display block and the continuation as prose — the opening line is gone. Keeping the continuation at the same indentation, or moving the block out of the list, renders it normally. Both flow behaviors match the grammar this replaced; only the multi-line blocks this flow owns are new. +The sixth is a multi-line block inside a container whose continuation line falls back to the container's indentation. `- Define:` followed by `$$f(x)=x` at the item's indentation and then a de-indented continuation line gives upstream's flow a fence meta and no value, so the reader sees an empty display block and the continuation as prose — the opening line is gone. Keeping the continuation at the same indentation, or moving the block out of the list, renders it normally. Both flow behaviors match the grammar this replaced; only the multi-line blocks this flow owns are new. Repeated unclosed candidates keep linear parser work: an open candidate declines at the next line that starts with a full `$$` it cannot close, rather than scanning each candidate to EOF, which would make 9.6 KB of `$$a$b` lines take ~4 s instead of ~16 ms. KaTeX-level failures in a formula's own source, such as `\arccot`, remain errors by design. ## Testing -`packages/client/ui-primitives/tests/markdown.client.spec.tsx` adds the multi-line case — opening-line math closed on a content line, closed by a lone fence, after a paragraph, inside a list item, and at column zero after a list item — and that case fails without the change. The same file also pins a multi-line formula that uses a solitary `$`, which like the case above renders only with this change. Further cases pin one condition each: a trailing-text candidate ahead of a later block (the flow must decline a full `$$` the line does not end after), a solitary `$` inside a single-line display formula (the flow must not decline on a single `$`), an escaped-dollar candidate that leaves its paragraph intact (the interruption check must not claim the paragraph), an unclosed `\[` candidate that leaves its paragraph intact (the guard must not apply to the backslash construct), a candidate ending in an odd backslash (the escape run must end at the line ending), a closing fence inside a TeX comment, a `%` the fence follows immediately, a second `%` inside an open comment, a verbatim `%` inside `\verb|…|` and the starred form's arbitrary delimiter, the plain `\verb` control word and its unterminated forms, and bounded-work cases for repeated candidates with and without that backslash. +`packages/client/ui-primitives/tests/markdown.client.spec.tsx` adds the multi-line case — opening-line math closed on a content line, closed by a lone fence, after a paragraph, inside a list item, and at column zero after a list item — and that case fails without the change. The same file also pins a multi-line formula that uses a solitary `$`, which like the case above renders only with this change. Further cases pin one condition each: a trailing-text candidate ahead of a later block (the flow must decline a full `$$` the line does not end after), a solitary `$` inside a single-line display formula (the flow must not decline on a single `$`), an escaped-dollar candidate that leaves its paragraph intact (the interruption check must not claim the paragraph), an unclosed `\[` candidate that leaves its paragraph intact (the guard must not apply to the backslash construct), a candidate ending in an odd backslash (the escape run must end at the line ending), a closing fence inside a TeX comment, a `%` the fence follows immediately or after whitespace, a second `%` inside an open comment, a `%` inside `\verb|…|`, and bounded-work cases for repeated candidates with and without that backslash. The DOM compatibility corpus adds `math-multiline-display` with its `.settled.txt` and `.streaming.txt` fixtures, which also pin that a column-zero `$$` after a list item stays outside the list item. `apps/web/tests/math-rendering.e2e.ts` renders the multi-line shape in the browser lane. diff --git a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.zh.md b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.zh.md index baa9f28b0c..07d4eaff3f 100644 --- a/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.zh.md +++ b/.agents/notes/implemented/bug-fix/2026-09-17-multiline-display-dollar-math.zh.md @@ -21,7 +21,7 @@ $$f(x)=x [mathCompatibility.ts](../../../../packages/client/ui-primitives/src/markdown/mathCompatibility.ts) 现在支持跨行的显示美元块。dollar 构造把开围栏同一行的文本留在 `mathFlowValue` 里而不是当作围栏 meta,并在后续任意内容行行尾的 `$$` 处闭合,于是 `$$a+b` 接 `c+d$$` 成为一个 math 节点。 -`$$` 序列后面不是该行行尾时(开围栏行或任意后续行,例如 `$$x$$ trailing`)它放弃这次尝试:继续扫描会把更靠后一个块的闭合围栏也吞掉。孤立的一个 `$` 属于公式内容而不是失败的围栏,所以凡是本 flow 拥有该块的场合,用到它的公式——`$$P = \begin{cases} 1 & \text{if $x>0$} \end{cases}$$`——保持块打开并正常闭合。行尾反斜杠转义的是换行本身,不是下一行的第一个字符,因此转义状态在换行处结束,不会延续到下一个候选。未转义的 `%` 会开启延续到行尾的 TeX 注释:当注释在围栏之前还有文本时,块后续行行尾的 `$$` 不闭合任何东西;而围栏之前只有空白或什么都没有的 `%`(`95%$$`、`95 %$$`)不含注释文本,且在一行内开闭的块仍在该行的 `$$` 处闭合,与它跨行之前的行为一致。`\verb<分隔符>…<分隔符>` 是原样文本区域,其中的 `%` 属于内容;分隔符不能是字母,至多一个 `*` 表示星号形式,并且该区域必须在行尾之前闭合,否则已读到的字符仍按普通内容处理。注释状态及其对闭合判定的门控属于 dollar flow;转义状态与原样文本扫描与反斜杠构造共用。 +`$$` 序列后面不是该行行尾时(开围栏行或任意后续行,例如 `$$x$$ trailing`)它放弃这次尝试:继续扫描会把更靠后一个块的闭合围栏也吞掉。孤立的一个 `$` 属于公式内容而不是失败的围栏,所以凡是本 flow 拥有该块的场合,用到它的公式——`$$P = \begin{cases} 1 & \text{if $x>0$} \end{cases}$$`——保持块打开并正常闭合。行尾反斜杠转义的是换行本身,不是下一行的第一个字符,因此转义状态在换行处结束,不会延续到下一个候选。未转义的 `%` 会开启延续到行尾的 TeX 注释:当注释在围栏之前还有文本时,块后续行行尾的 `$$` 不闭合任何东西;而围栏之前只有空白或什么都没有的 `%`(`95%$$`、`95 %$$`)不含注释文本,且在一行内开闭的块仍在该行的 `$$` 处闭合,与它跨行之前的行为一致。该状态及其对闭合判定的门控属于 dollar flow。原样文本区域不单独解析:只按上面的形状判定,使含 `\verb` 的公式与其它内容走同一条路径。 它的段落打断检查改为「不认领」,且只作用于 dollar 构造。在那里消耗换行会让段落跨行边界保持打开,document 层因此保留容器并把下一行对随后尝试的所有构造计为 lazy;这正是上游 `mathFlow`(本构造之后被尝试的构造)在列表项内以空块结束的原因。不认领同时把打断决定交给上游 flow:它自己的检查不消耗换行就短路返回,并且只认领它接受的围栏,因此两个构造都不接受的候选(`**before` 后接 `$$a\$b`)保持原段落。反斜杠构造没有竞争的上游构造,仍然照旧消耗换行。 @@ -47,18 +47,18 @@ $$f(x)=x 三种写法现在产生同一个 math 节点,公式不再在开围栏那一行被截断,也不再吞掉其后的正文。`$$` 围栏 meta 不再生效:`$$asciimath` 会解析成首行为 `asciimath` 的公式,而上游 flow 会把它拆成 meta 加正文。这种写法在本仓库没有使用者,且渲染器本来就从不读 `meta`。 -有五种输入仍会走到上游 flow,或留在所属段落里,因为兼容 flow 主动放弃它们。闭合 `$$` 之后还有文本时(`$$x$$ trailing`,或用中文引号收尾的散文行)走上游行内数学,这一点由 `markdown.client.spec.tsx` 中已有用例固定,后面还有别的 `$$` 块时同样如此。块内任意位置出现「后面不是行尾」的 `$$` 序列,会让整个块走到同一条回退。未闭合的 `$$` 块仍像本次改动之前那样延伸到下一个独占行 `$$` 或文件结尾,因为上游 flow 在 EOF 处闭合。 +有六种输入仍会走到上游 flow,或留在所属段落里,因为兼容 flow 主动放弃它们。闭合 `$$` 之后还有文本时(`$$x$$ trailing`,或用中文引号收尾的散文行)走上游行内数学,这一点由 `markdown.client.spec.tsx` 中已有用例固定,后面还有别的 `$$` 块时同样如此。块内任意位置出现「后面不是行尾」的 `$$` 序列,会让整个块走到同一条回退。未闭合的 `$$` 块仍像本次改动之前那样延伸到下一个独占行 `$$` 或文件结尾,因为上游 flow 在 EOF 处闭合。 第四种是「开围栏行含孤立 `$`、且需要打断段落、前面又没有空行」的多行块。两个 flow 都不认领这次打断——本 flow 出于设计,上游则因为它的围栏 meta 在 `$` 处即停止——该块因此留在段落里,渲染成行内数学或根本不渲染。用空行把块与段落分开,本 flow 就会拥有它并渲染 display。 -第六种来自注释门控:当块唯一的闭合围栏位于「带文本的注释」之后时,块继续扫描而不是在那里闭合,若再没有别的围栏闭合它,就会像上面几种一样走到上游 flow。若围栏之前只有空白,则块在该处闭合,此后单独一行的 `$$` 会成为它自己的空块——而被替换掉的语法那时只给出一个节点。 +第五种来自注释门控:当块唯一的闭合围栏位于「带文本的注释」之后时,块继续扫描而不是在那里闭合,若再没有别的围栏闭合它,就会像上面几种一样走到上游 flow。若围栏之前只有空白,则块在该处闭合,此后单独一行的 `$$` 会成为它自己的空块——而被替换掉的语法那时只给出一个节点。 -第五种是容器内续行退回容器缩进的多行块。`- Define:` 之后以列表项缩进写 `$$f(x)=x`,再接一行不缩进的续行,上游 flow 会得到有 meta、无 value 的结果:读者看到一个空的 display 块加一行散文,公式首行无声消失。续行保持同样缩进,或把整块移到列表之外,即可正常渲染。这两种 flow 行为都与本次替换掉的语法一致;真正新增的只是「本 flow 拥有的多行块」。 +第六种是容器内续行退回容器缩进的多行块。`- Define:` 之后以列表项缩进写 `$$f(x)=x`,再接一行不缩进的续行,上游 flow 会得到有 meta、无 value 的结果:读者看到一个空的 display 块加一行散文,公式首行无声消失。续行保持同样缩进,或把整块移到列表之外,即可正常渲染。这两种 flow 行为都与本次替换掉的语法一致;真正新增的只是「本 flow 拥有的多行块」。 重复的未闭合候选保持线性解析开销:未闭合的候选会在下一行遇到无法闭合的完整 `$$` 时放弃,而不是把每个候选都扫到 EOF——否则 9.6 KB 的 `$$a$b` 行会花约 4 秒而不是约 16 毫秒。公式自身源码层面的 KaTeX 失败,例如 `\arccot`,仍按设计报错。 ## Testing -`packages/client/ui-primitives/tests/markdown.client.spec.tsx` 新增多行用例——开围栏行内容由内容行行尾 `$$` 闭合、由独占行 `$$` 闭合、出现在段落之后、出现在列表项内,以及列表项之后的零缩进围栏——该用例去掉改动即失败。同一文件还固定了「使用单个 `$` 的多行公式」,它与上面的多行用例一样只有本次改动才会渲染成 display。另有一组用例各自对应一个条件:后面还有块时的尾随文本候选(flow 必须放弃「后面不是行尾」的完整 `$$`)、单行显示公式内的单个 `$`(不得因为一个 `$` 就放弃)、保持段落完整性的转义美元候选(打断检查不得认领段落)、保持段落完整性的未闭合 `\[` 候选(守卫不得作用于反斜杠构造)、以奇数个反斜杠结尾的候选(转义状态必须在换行处结束)、TeX 注释内的闭合围栏、紧邻围栏的 `%`、已开启注释内的第二个 `%`、`\verb|…|` 内的原样 `%` 与星号形式的任意分隔符、`\verb` 控制字及其未闭合形状,以及带与不带该反斜杠两种重复候选的有界解析开销。 +`packages/client/ui-primitives/tests/markdown.client.spec.tsx` 新增多行用例——开围栏行内容由内容行行尾 `$$` 闭合、由独占行 `$$` 闭合、出现在段落之后、出现在列表项内,以及列表项之后的零缩进围栏——该用例去掉改动即失败。同一文件还固定了「使用单个 `$` 的多行公式」,它与上面的多行用例一样只有本次改动才会渲染成 display。另有一组用例各自对应一个条件:后面还有块时的尾随文本候选(flow 必须放弃「后面不是行尾」的完整 `$$`)、单行显示公式内的单个 `$`(不得因为一个 `$` 就放弃)、保持段落完整性的转义美元候选(打断检查不得认领段落)、保持段落完整性的未闭合 `\[` 候选(守卫不得作用于反斜杠构造)、以奇数个反斜杠结尾的候选(转义状态必须在换行处结束)、TeX 注释内的闭合围栏、紧邻围栏或只隔空白的 `%`、已开启注释内的第二个 `%`、`\verb|…|` 内的 `%`,以及带与不带该反斜杠两种重复候选的有界解析开销。 DOM 兼容语料新增 `math-multiline-display` 及其 `.settled.txt`、`.streaming.txt`,同时固定了「列表项之后的零缩进 `$$` 不进入列表项内」。`apps/web/tests/math-rendering.e2e.ts` 在浏览器 lane 渲染多行写法。 diff --git a/packages/client/ui-primitives/src/markdown/mathCompatibility.ts b/packages/client/ui-primitives/src/markdown/mathCompatibility.ts index ed9fb9bdd0..9c63c70921 100644 --- a/packages/client/ui-primitives/src/markdown/mathCompatibility.ts +++ b/packages/client/ui-primitives/src/markdown/mathCompatibility.ts @@ -15,14 +15,6 @@ import type { Construct, Extension, Previous, State, Tokenizer } from 'micromark // oxlint-disable typescript/no-this-alias -- micromark binds tokenizer context only on the outer callback. -/** Letters that follow a backslash to open TeX's `\verb` verbatim form. */ -const VERB_NAME = 'verb' - -/** Whether a code unit is an ASCII letter, which continues a control word. */ -function isAsciiLetter(code: number): boolean { - return (code >= codes.lowercaseA && code <= codes.lowercaseZ) - || (code >= codes.uppercaseA && code <= codes.uppercaseZ) -} const previousBackslash: Previous = function (code) { if (code !== codes.backslash) return true @@ -144,9 +136,7 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) let commentHasContent = false let crossedLineEnding = false let sawClosingFenceSequence = false - let verbPrefixIndex = 0 - let verbatimDelimiter: number | null = null - let verbatimStarred = false + let failedClose = false const tail = self.events.at(-1) const initialSize = tail?.[1].type === types.linePrefix ? tail[2].sliceSerialize(tail[1], true).length @@ -186,6 +176,10 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) )(code) } if (markdownLineEnding(code)) { + // A close that failed earlier on this candidate ends it at the line + // ending, which is what the grammar this flow replaced did: a failed + // `$$x$$ trailing` never reaches the next line. + if (marker === codes.dollarSign && failedClose) return nok(code) // A trailing backslash escapes the line ending, not the next line's // first character, so the escape run ends here, and a TeX comment ends // with the line. The comment exception covers only a closing fence on a @@ -221,11 +215,14 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) if (marker === codes.backslash) { return effects.check({ partial: true, tokenize: tokenizeOpeningFence }, nok, markerValueStart)(code) } - // A full `$$` that the line does not end after closes nothing: `$$x$$ - // trailing` is not a display block, and scanning on from here would - // swallow a later block's closing fence. A solitary `$` is content that - // keeps the block open, not a failed fence. - return sawClosingFenceSequence ? nok(code) : markerValueStart(code) + // A full `$$` that the line does not end after closes nothing there. On + // the opening line the scan continues, so `$$x$$ trailing` declines at + // that line's ending while a later `$$` on the same line still closes; on + // any later line it ends the candidate outright, so a following block is + // not swallowed. A solitary `$` is content, not a failed fence. + if (crossedLineEnding) return nok(code) + if (sawClosingFenceSequence) failedClose = true + return markerValueStart(code) } function afterContinuation(code: number | null): State | undefined { @@ -251,9 +248,7 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) commentHasContent = true } effects.consume(code) - if (code !== codes.backslash || inComment) return value - verbPrefixIndex = 0 - return afterBackslash + return value } function markerValueStart(code: number | null): State | undefined { @@ -279,13 +274,6 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) } const escaped = oddBackslashRun oddBackslashRun = code === codes.backslash ? !escaped : false - if (code === codes.backslash && !escaped && !inComment) { - // A `\verb…` region is verbatim, so a `%` inside - // it is content rather than a comment. A comment holds no commands. - effects.consume(code) - verbPrefixIndex = 0 - return afterBackslash - } // An unescaped `%` starts a TeX comment that runs to the line ending, so // a `$$` inside it closes nothing. A `%` the fence follows immediately // holds no comment text, which is how writers use it mid-formula @@ -302,87 +290,6 @@ function createMathFlow(marker: number, openMarker: number, closeMarker: number) return value } - function afterBackslash(code: number | null): State | undefined { - if (code === VERB_NAME.charCodeAt(verbPrefixIndex)) { - effects.consume(code) - oddBackslashRun = false - verbPrefixIndex += 1 - return verbPrefixIndex === VERB_NAME.length ? verbatimStar : afterBackslash - } - return value(code) - } - - /** `\verb` takes at most one `*`, then the delimiter it repeats. */ - function verbatimStar(code: number | null): State | undefined { - verbPrefixIndex = 0 - if (code !== codes.asterisk) { - verbatimStarred = false - return verbatimDelimiterStart(code) - } - effects.consume(code) - verbatimStarred = true - return verbatimDelimiterStart - } - - function verbatimDelimiterStart(code: number | null): State | undefined { - // In the plain form a letter continues the control word (`\verbatim`), - // so it is no delimiter; the starred form repeats any character. `\verb` - // at a line ending has no delimiter at all. - if (code === null || markdownLineEnding(code)) return value(code) - if (!verbatimStarred && isAsciiLetter(code)) return value(code) - verbatimDelimiter = code - oddBackslashRun = false - effects.consume(code) - return verbatimCloseCheck - } - - /** - * `\verb` closes on its delimiter before the line ends; without one the - * characters read so far are ordinary content, which keeps the error inside - * the formula instead of consuming a closing fence as verbatim text. - */ - function verbatimCloseCheck(code: number | null): State | undefined { - return effects.check( - { partial: true, tokenize: tokenizeVerbatimClose }, - verbatimContent, - verbatimFallback, - )(code) - } - - function tokenizeVerbatimClose( - checkEffects: Parameters[0], - checkOk: State, - checkNok: State, - ): State { - return scan - - function scan(code: number | null): State | undefined { - if (code === null || markdownLineEnding(code)) return checkNok(code) - if (code === verbatimDelimiter) return checkOk(code) - checkEffects.consume(code) - return scan - } - } - - function verbatimContent(code: number | null): State | undefined { - /* v8 ignore next 4 -- the close check guarantees the delimiter precedes - any line ending, so this state never sees one. */ - if (code === null || markdownLineEnding(code)) { - verbatimDelimiter = null - return value(code) - } - const closes = code === verbatimDelimiter - effects.consume(code) - if (!closes) return verbatimContent - verbatimDelimiter = null - return value - } - - function verbatimFallback(code: number | null): State | undefined { - verbatimDelimiter = null - return value(code) - } - function closed(code: number | null): State | undefined { effects.exit('mathFlow') return ok(code) diff --git a/packages/client/ui-primitives/tests/markdown.client.spec.tsx b/packages/client/ui-primitives/tests/markdown.client.spec.tsx index 91c99b28ff..4b2ec27adb 100644 --- a/packages/client/ui-primitives/tests/markdown.client.spec.tsx +++ b/packages/client/ui-primitives/tests/markdown.client.spec.tsx @@ -600,31 +600,33 @@ describe('MarkdownText', () => { } }) - it('keeps an escaped percent and malformed verbatim forms inside one block', () => { - for (const source of ['$$a\\%b$$', '$$\\verb*|%|$$']) { + it('keeps an escaped percent inside its formula', () => { + const { container } = render() + + expect(container.querySelectorAll('.katex-display')).toHaveLength(1) + expect(container.querySelector('.katex-display annotation')?.textContent).toBe('a\\%b') + }) + + it('leaves verbatim forms to the value scanner', () => { + const percent = render() + expect(percent.container.querySelectorAll('.katex-display')).toHaveLength(2) + expect(percent.container.querySelector('.katex-error')).toBeNull() + expect(percent.container.querySelector('p')?.textContent).toBe('After') + percent.unmount() + + const controlWord = render() + expect(controlWord.container.querySelector('.katex-display annotation')?.textContent).toBe('\\verbatim + y') + controlWord.unmount() + + const starred = render() + expect(starred.container.querySelector('.katex-display annotation')?.textContent).toBe('\\verb*a$$a') + starred.unmount() + + for (const source of ['$$\\verb**abc*$$\n\nAfter\n\n$$\ny\n$$', '$$x % \\verb$abc$$\n\nAfter\n\n$$\ny\n$$']) { const rendered = render() - expect(rendered.container.querySelectorAll('.katex-display')).toHaveLength(1) - rendered.unmount() - } - - const escaped = render() - expect(escaped.container.querySelector('.katex-display annotation')?.textContent).toBe('a\\%b') - escaped.unmount() - - // An unterminated `\verb` at EOF leaves the flow to upstream mathFlow, - // whose fence meta the renderer drops, so the node's value is empty. - for (const source of ['$$\\verb', '$$\\verb|abc']) { - const rendered = render() - - expect(rendered.container.querySelector('.katex-display annotation')?.textContent).toBe('') - rendered.unmount() - } - - for (const source of ['$$\\verb\n$$', '$$\\verb|abc\ndef$$']) { - const rendered = render() - - expect(rendered.container.querySelector('.katex-error')).not.toBeNull() + expect(rendered.container.querySelectorAll('.katex-display, .katex-error')).toHaveLength(2) + expect(rendered.container.querySelector('p')?.textContent).toBe('After') rendered.unmount() } }) @@ -642,28 +644,12 @@ describe('MarkdownText', () => { } }) - it('repeats any delimiter character in the starred verbatim form', () => { - const { container } = render() + it('closes a block at a fence behind a whitespace-only comment', () => { + const { container } = render() - expect(container.querySelector('.katex-display annotation')?.textContent).toBe('\\verb*a$$a') - }) - - it('keeps a control word and an unterminated verbatim inside their formulas', () => { - const controlWord = render() - expect(controlWord.container.querySelector('.katex-display annotation')?.textContent).toBe('\\verbatim + y') - controlWord.unmount() - - for (const source of [ - '$$\\verb|abc$$\n\nAfter\n\n$$\ny\n$$', - '$$x % \\verb$abc$$\n\nAfter\n\n$$\ny\n$$', - '$$\\verb**abc*$$\n\nAfter\n\n$$\ny\n$$', - ]) { - const rendered = render() - - expect(rendered.container.querySelectorAll('.katex-display, .katex-error')).toHaveLength(2) - expect(rendered.container.querySelector('p')?.textContent).toBe('After') - rendered.unmount() - } + expect(container.querySelectorAll('.katex-display')).toHaveLength(2) + expect(container.querySelector('.katex-display annotation')?.textContent).toContain('a = x% ') + expect(container.querySelector('.katex-error')).toBeNull() }) it('ends the backslash escape run at a line ending', () => {