Skip to content

fix: map inline positions after escaped pipes in table cells (DEV-3396) - #30

Open
galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3396-escaped-pipe-positions
Open

galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3396-escaped-pipe-positions

Conversation

@galaxian85

@galaxian85 galaxian85 commented Dec 1, 2026 •

Copy link
Copy Markdown

DEV-3396

hackmd-production 升級到 14.2.1 時,要一起改一個 spec(兩行)。 執行期的 code 不用改,但 test/browser/render/position-invariant.test.ts 的 table escaped pipe 案例會失敗。原因和改法見下方「hackmd-production 驗證」。Charlie 已確認可以接受(2026-12-01)。

問題

表格的儲存格裡有 \| 時,escapedSplit 會先拿掉反斜線,再把儲存格的文字交給 inline 解析。children 的位置是用 content 座標算的,core inline 只加上 tok.position,所以:

  • 在 \| 之後開始的 child,position 每經過一個 \| 就少 1。
  • 跨過 \| 的 child,size 每跨過一個就少 1。

儲存格的 open/close 已經是對的(sourceCellLength)。master(7d1efa2)上的例子:

  • | a | b *c* \| *d* |:*d* 的 em_open 在 14(應為 15),text ' | ' 的 size 是 3(應為 4)。修正後是 15,text 從反斜線前的空白開始、size 4。
  • | x | `a\|b` y |:code_inline 的 size 是 3(應為 4),後面的 text 往前偏 1。修正後 size 4,後面的 text 正確。
  • | a \| x |:text a | x 是 position 2、size 5,slice(2, 7) 是 a \| ,蓋不到 x。修正後 size 6。
  • > | h \| [l\|k](u) @me |(blockquote 裡的表格):link 文字和後面的 text 一樣往前偏。修正後正確。

做法

  1. table(block 階段)記錄:content 裡有 | 的儲存格,把「content 裡每個 | 的 index」記進 block state 的私有 Map _cellEscapes(key 是 inline token,用到才建立;寫法和 fix: keep the line offsets of inline tokens when rules call getLines again (DEV-3364) #26 的 _inlineSaved 一樣,存取用 state_block.mjs 的 markEscapedPipes/escapedPipes)。
    • escapedSplit 只在 \| 時把 | 留在格子裡,所以 content 裡的每個 | 都對應一個被拿掉的反斜線(sourceCellLength 也靠這個前提),escapedSplit 不用改。\\| 也成立(content 是 \|)。
    • index 在 table 裡算好再存(不是到 core inline 再從 content 算):只有內建 table rule 做出來的格子會被標記,第三方表格 plugin 的格子即使有 | 也不會被換算。
  2. core inline 換算:inline 解析完、加上 tok.position 之前,token 在 Map 裡就換算每個 child:
    • 先扣掉 lineOffset(blockState.lineOffsets[tok.map[0]],和解析時 getLineOffset 讀的是同一個值;格子都是單行),在 content 座標裡算:
      • start' = start + (index < start 的 | 數)
      • end' = end + (index < end 的 | 數),size = end' - start'
    • 慣例:反斜線算在包含那個 | 的 token 裡(text ' | ' 從反斜線前的空白開始、size 4);剛好在 \| 前面的 0 長度 token 放在反斜線前面。
    • position 和 size 都是 0 的 children(link_open/link_close 等)不動。
    • 沒有 Map、或 token 不在 Map 裡時完全不跑(快速路徑:一般文件只多一次 null 檢查)。
    • 在 core text_join 之前執行,sourceGap 的連續性檢查會通過。跨過 \| 的 text 是 content.length !== size,text_join 不合併,會留成獨立的 token(預期)。
  3. 沒有改的:getLineOffset/getLinePad 的語意、escapedSplit、inline token 本身的 size(維持 content.length,DEV-3359 的決定)。沒有新增公開欄位。

已知限制(14.3.0 再處理)

跨過 \| 的 token 內部,用 position + index 換算時,| 之後的字還是差 1(每個 \| 差 1)。例如 | a \| x | 的 text a | x 外框是 a \| x(size 6),但 x 用 position + 4 換算會指到前一個空白。單行的 code_inline 也一樣。和 14.2.0 比不會變差(14.2.0 同一個位置一樣差 1,而且外框還蓋不到 x)。已寫進註解、CHANGELOG 和測試(should cover the backslash of an escaped pipe inside a table cell token)。

不把 text 在 | 的地方切開:切開發生在 typographer 的 replacements 之前,\|-- x 這類邊界的 HTML 會改變;code_inline 也切不開。要讓 token 內部也對得上需要新增 meta 欄位,列為 14.3.0(handoff「剩下的項目」#10)。

不在範圍內:mention

hackmd-production 的 mention.js 在 inline 解析過程中用 state.pos + token.position + getLineOffset(state) 算絕對位置,事後換算救不到。同一格裡 \| 後面的 mention 仍然是純文字,和修正前一樣(驗證結果見下方)。

測試

  • npm test(Node 24):1634 passing、1 pending(master 1630),lint 通過。
  • 新測試在 test/annotation.js,緊接在 should annotate table cells after an escaped pipe 之後、tableCells helper 之前(4 個,修正前都失敗;既有測試含 fix: keep the line offsets of inline tokens when rules call getLines again (DEV-3364) #26 的 documented trap 都不變):
    • should map the children of table cells after an escaped pipe:上面的例子、code span 裡的 `a\|b`(HTML 維持 <code>a|b</code>)、一格多個 \|、\| 在格子開頭和結尾、\\|、header 和 body、沒有開頭 | 的列、開頭有 U+00A0 的列、blockquote、list、link 文字裡有 \|。
    • should keep 0-length table cell children before an escaped pipe:用測試 plugin 在 | 前插入 0 長度 token,確認它在反斜線前面;以及 *c*\|。
    • should cover the backslash of an escaped pipe inside a table cell token:記錄上面的已知限制。
    • should map random table cells with escaped pipes back to the source:600 份隨機表格 × 3 種設定(預設、typographer + linkify、commonmark + table),容器含 blockquote、list、blockquote 裡的 list;每個 size > 0 的 child,src.slice(position, position + size) 把 \| 換成 | 後等於它在 content 裡對應的文字,邊界不落在反斜線和 | 之間;text 和 code_inline 等於 content。
  • A/B(master vs 這個 branch):fixtures 1068 個 case + benchmark samples 28 份 + 隨機表格 3000 份(含 \|、smartypants、linkify、HTML、image、strikethrough、U+00A0、各種容器),預設/commonmark/typographer 三種設定:
    • HTML 完全相同(三種設定都是 0 差異)。
    • block 層級的 token 0 差異;所有 token 差異都是 content 有 | 的格子裡的 children 的 position/size,沒有其他欄位改變。fixtures 只有 3 個 case 的 token 改變(都是格子裡有 \| 的表格),benchmark samples 0 差異。
    • 另外確認 fix: keep the line offsets of inline tokens when rules call getLines again (DEV-3364) #26 的探查情境(lookAhead 先蓋掉 lineOffsets):body 列的位移量和 master 一樣(都是 +4),換算只在上面多加反斜線。
  • 效能(Benchmark.js,benchmark/samples 28 份,html/linkify/typographer 開啟,和 master 比,geomean):master 先跑 +0.84%、branch 先跑 −1.73%,平均 −0.45%,在雜訊範圍內(之前量到 master 對 master 約 ±1.7–1.9%)。「40 個表格、每格都有 \|」的樣本:+0.70%/−1.64%。

hackmd-production 驗證(develop 7213b0bff4,什麼都沒有 commit)

develop 是在另一個 git worktree 裡用 pnpm install --frozen-lockfile 測的(本機的 hackmd-production 在別的 branch,node_modules 和 develop 不同),@hackmd/markdown-it 換成指向 fork 的 symlink。跑完已刪掉 worktree、它的 test DB 和 slot。

  • spec(handoff「驗證」列的那組):

    • npm 14.2.0:node 8 個檔案/135 個測試、browser 15/163,全過(和基準一樣)。

    • 這個 branch:node 8/135 全過;browser 1 個檔案失敗、10 個測試失敗(153/163 過):都是 test/browser/render/position-invariant.test.ts 的 table escaped pipe 案例(5 種容器 × 有/沒有 front matter)。

    • fork master(fix: keep the line offsets of inline tokens when rules call getLines again (DEV-3364) #26、fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) #27)跑同一個 spec 全過,所以是這個 PR 造成的。

    • 原因:spec 的 matchesSource 把 data-size !== text.length 當成「渲染後和原始碼不同」(escape、entity、smartypants),只接受那幾種形狀。text a | b 現在的 data-size 是 6(含反斜線),長度 5,所以被判失敗。14.2.0 是 size 5,走另一個分支,再用 \| → | 的正規化比對通過。

    • 執行期不受影響:frontend/comment/selection.js 對文字節點用 position + offset(不變,即上面的已知限制),用 position + size 算結尾的地方現在會蓋到格子文字的結尾(變準)。只有 smartypants 和 tab padding 被當成不可分割的節點。

    • 改法(只改 spec,在 worktree 裡試過,11 個測試全過,沒有 commit;Charlie 已確認可以接受,hackmd-production 升級 14.2.1 的 PR 要一起改):

      function matchesSource (src: string, position: number, text: string, size: number) {
        // escaped pipes in table cells: the backslash is in the size, not in the text
        const escapedPipes = size >= 0 ? (src.substr(position, size).match(/\\\|/g) || []).length : 0
        if (size >= 0 && size - escapedPipes !== text.length) {
  • 臨時腳本(vitest browser spec,用 production 的 server parser getParser() 和 frontend renderer loadFrontendRenderer();18 份「格子裡有 \|」的樣本:6 個表格 × 頂層/blockquote/list,含 smartypants (c)、(r)、--、inline spoiler \|\|secret\|\|、ruby {漢字\|かんじ}、mention(每個 @me 都有 anchor)、沒有開頭 | 的列;excerpt 用 helper.ts 的 extractExcerpt,表格範圍內每個位置、長度 1–6 的視窗)。跑完已刪除。

和其他 PR 的關係

這是 14.2.1 批次的一部分,merge 後請先不要發版。

🤖 Generated with Claude Code

The table rule drops the backslash of each `\|` before inline parsing, so
children of the cell after a `\|` started one character early, and children
spanning one were one character short. The table rule now records the
indices of the `|` left in such cells in a private map of the block state,
and core `inline` maps the children back to the source before making their
positions absolute. Rendered HTML is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@galaxian85
galaxian85 requested a review from Yukaii December 1, 2026 02:18
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 46560106566

Coverage remained the same at 98.943%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 82 of 82 lines across 3 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6892
Covered Lines: 6829
Line Coverage: 99.09%
Relevant Branches: 1814
Covered Branches: 1785
Branch Coverage: 98.4%
Branches in Coverage %: Yes
Coverage Strength: 378527.89 hits per line

💛 - Coveralls

This branch has not been deployed

No deployments
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.

2 participants