Skip to content

fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) - #27

Merged
Yukaii merged 2 commits into
masterfrom
fix/dev-3395-table-cell-positions
Nov 26, 2026
Merged

Yukaii merged 2 commits into
masterfrom
fix/dev-3395-table-cell-positions

Conversation

@galaxian85

@galaxian85 galaxian85 commented Nov 23, 2026 •

Copy link
Copy Markdown

DEV-3395

修正表格儲存格的位置:沒有開頭/結尾 | 的列、自動補的格子,以及 header inline 的 map。只改 lib/rules_block/table.mjs 的位置計算(切欄位的邏輯、token 的種類和順序都沒動)。markdown-it 本身輸出的 HTML 不變。

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

修正的問題

a | b\n--|--\nc | d(a 在 0、| 在 2、b 在 4)為例:

  1. 沒有開頭 |:每一列的 columnVIndex 都先 +1 跳過開頭的 |,但只有真的以 | 開頭時 columns.shift() 才會拿掉空的第一欄,所以整列每一格都往右偏 1。
    • 14.2.0:第一個 th_open 在 0、size 1(指到 a);inline 在 1;第二個 th_open 在 3;第二個 inline 在 5(指到 \n)
    • 修正後:th_open 0/size 0;inline 0;th_close 2(|);th_open 2/size 1;inline 4;th_close 5/size 0
  2. 沒有結尾 |:最後一格的 close token 還是 size 1,會蓋到換行(沒有開頭也沒有結尾時甚至指到下一行)。| i | j 的 th_close 從 \n/size 1 改成在儲存格結尾、size 0。
  3. 自動補的格子(body 的格子比 header 少):補出來的格子 open/inline 會超出行尾,甚至超出原始碼長度。| s | t | u | 的 body | v |:第二、三格原本在 33/34(下一行),現在 open、inline、close 都在行尾(33)、size 0。size 1 的 close 仍然由最後一個真的格子認領(| v | 的結尾 |)。
  4. header inline 的 map:從 [nextLine, nextLine + 1](分隔線)改成 [startLine, startLine + 1](header 那一行)。

另外順便修了一個同類的問題(不在 issue 原本的範圍,Charlie 已確認放在這個 PR,2026-11-23):

  1. 列的開頭有 Unicode 空白(例如 U+00A0):lineText 用 trim() 去掉,但 tShift 不算,所以整列每一格都往左偏 1,有開頭和結尾 | 的表格也一樣。例: | a | b | 的 th_open 原本在 0(指到 U+00A0),現在在 1(|)。偏移量是 U+00A0 的個數(不一定是 1)。這是唯一會改變「有開頭和結尾 | 的表格」位置的地方(從錯改成對)。fork 在段落已用 trimmedContentStart 處理同樣的情況,這裡讓表格一致。

採用的慣例(和 issue 的建議相同)

  • 前面沒有 | 的格子(沒有開頭 | 的列的第一格):open token 放在儲存格開頭,size 0。
  • 其他格子的 open token 在前一個 |,size 1;close token 在後一個 |,size 0。
  • 結尾的 | 由最後一個真的格子的 close token 認領,size 1。沒有結尾 | 時,close token 放在儲存格結尾(trim() 後的行尾),size 0。
  • 自動補的格子:open、inline、close 都放在行尾(eMarks),size 0。
  • inline token 的 size 維持 content.length(DEV-3359 的決定)。
  • 比 header 多的格子仍然直接丟掉,結尾的 | 不會被認領(和之前一樣)。

實作上把位置計算抽成 setCellPositions(),header 和 body 共用,在 push 完一整列的 token 之後才設位置。

測試結果

fork

  • npm test:1624 passing、1 pending(master 1619 passing)。lint 通過。
  • test/annotation.js 新增 4 個測試:
    • issue 的 8 個例子,檢查每格 open/inline/close 的 position、size 和 inline 的 map
    • 自動補的格子(含結尾 | 後有空白)
    • 邊界情況:只有一欄(只有開頭或只有結尾 |)、空的儲存格、只有 | 的列、tab 縮排、list 裡(含 blockquote 裡的 list)沒有開頭 | 的表格、header 沒有開頭 | 但 body 有、\|(含列尾的 \|)、開頭有 U+00A0
    • 通用檢查:500 個隨機表格(各種容器,每列隨機有沒有開頭/結尾 |、格子數隨機少於 header、行尾空白、U+00A0),檢查每個 inline 的 position 指到該格 trim 後的第一個字元(自動補的格子為行尾)、map 是該列,所有 token 的 position + size 不超過該列行尾,size 1 的 open/close 都指到 |
    • 這 4 個測試在 master 上都失敗
  • 既有測試改了一處:should annotate tables 裡有一列 | Hello | World(沒有結尾 |),原本斷言 td_close 蓋到 '\n',這正是問題 2,改成斷言在儲存格結尾、內容為空字串。其他既有測試的結果都沒變。
  • 比對腳本 table-3-check.mjs:修正前 77 個 ✗,修正後 0 個。
  • A/B(master vs 這個 branch,default/linkify+typographer/commonmark 三種設定):test/fixtures 的所有範例、benchmark/samples、3000 個隨機表格,共 4095 份文件,HTML 完全相同。token 的差異逐一分類,全部落在:沒有開頭或結尾 | 的列、自動補的格子、header inline 的 map、開頭有 U+00A0 的列;inline 的 children 只隨著 parent inline 平移(相對位置和 size 都沒變)。

和 #26(DEV-3364)的關係

把這次的 table.mjs 套到 #26 的 branch 上,#26 新增的表格測試(documented trap)仍然通過。差別:用 lookAhead 探查時,header 的兩格從 ['|', '|'] 變成 ['h', 'i']。header inline 現在讀的是 header 那一行的偏移,不再是分隔線那一行的;body 的格子還是會讀到探查留下的值,所以那個測試的 notDeepEqual 仍然成立,文件寫的限制也還在。

hackmd-production(#6100 的 branch,14.2.0,node_modules/@hackmd/markdown-it 暫時換成指向 fork 的 symlink)

  • markdown 相關 spec:node 8 個檔案、135 個測試;browser 15 個檔案、163 個測試,全過,和 14.2.0 的基準相同。
  • 臨時 spec(沒有 commit,已刪除):用 production 的 frontend renderer(extra.js,含 mention、spoiler、alert 等 plugin)和 server parser(getParser()),對 23 種表格(有/沒有開頭結尾 |、自動補格子、blockquote/list/alert/:::spoiler 裡、儲存格裡有 mention、\|\|spoiler\|\|、強調、code、連結、emoji)比較 npm 14.2.0 和 fork:
    • server parser 的 HTML:23 份完全相同。
    • frontend renderer 的 HTML 有差異:
      • 正常的差異:frontend 把 token 位置寫進 HTML 的 data-position,所以沒有開頭 | 的列,data-position 都 −1(例如 a | b 的 a 從 1 變 0,現在指到正確的字)。
      • mention 變成會被辨識:5 份樣本裡,原本顯示成純文字的 @user 現在會渲染成 a.mention-anchor 連結(npm/fork 的 anchor 數:| @user | hi @user | 表格 1→3、@user | hi @user 0→3、a | @user 1→2、blockquote 裡 0→2、alert 裡 0→1)。原因:mention.js 用 inline token 的 map 決定要找哪幾行的 anchor,再用 token.position 算偏移,所以 14.2.0 的 header 儲存格(就算有開頭和結尾 |) 和沒有開頭 | 的列裡的 mention 都對不到 anchor。修正後兩者都對得上。這是 production 原本的 bug 被修好,production 不用改 code,但畫面會不一樣(表格 header 裡的 mention 會變成連結)。已確認可以接受(2026-11-23,Charlie):本來就預期任何 inline 元素都能在任何結構裡使用。
    • token 位置的差異只出現在:沒有開頭/結尾 | 的列、自動補的格子、header inline 的 map,以及這些格子裡的 children(強調、code、連結、emoji、inline spoiler、mention)隨 parent 平移。有開頭和結尾 | 的列,位置完全沒變。
    • excerpt(helper.ts):沒有開頭 | 的表格的 excerpt 變準了,例如 a | b 從 0 起 1 個字原本是 '',現在是 'a';從 0 起 2 個字原本是 '\na',現在是 'a'。自動補的格子因為 inline 從行尾之後移到行尾,跨過它的範圍少了多餘的 \n(| c | 那列從 21 起 8 個字:'\nc\n' → '\nc')。test/note/excerpt.test.ts 全過。

不在這次範圍

  • 儲存格裡 \| 之後的文字位置偏移(DEV-3267 的「Deferred by the author」):要改 core inline rule,還在等決定。這次沒有動 escapedSplit 拿掉反斜線的行為,也沒有動 core inline。\| 前後 open/close 的位置(sourceCellLength)維持正確。

衝突

#25(DEV-3359)和 #26(DEV-3364)都改了 test/annotation.js 和 CHANGELOG 的 [14.2.1] - Unreleased。master 上還沒有這個段落,這裡照樣新增,之後 rebase 時再合併成一個 ### Fixed。

🤖 Generated with Claude Code

galaxian85 and others added 2 commits November 23, 2026 16:09
… (DEV-3395)

- Without a leading `|`, every cell of the row was one character too far
  right. The first open token now sits at the start of its cell (size 0).
- Without a trailing `|`, the last close token had size 1 and covered the
  newline. It now sits at the end of the cell (size 0).
- Autocompleted cells ran past the end of the line (even past the source).
  Their open, inline and close tokens now sit at the end of the line (size 0).
- Unicode whitespace before a row (removed by trim(), not counted by tShift)
  moved its cells one character to the left.
- Header inline tokens had `map` set to the delimiter line instead of the
  header line.

Tables with a leading and trailing `|` keep the same positions. Rendered
HTML is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45136912641

Warning

No base build found for commit 3b368fe on master.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 99.34%

Details

  • Patch coverage: 68 of 68 lines across 1 file are fully covered (100%).

Uncovered Changes

No uncovered changes found.

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 6709
Covered Lines: 6676
Line Coverage: 99.51%
Relevant Branches: 1777
Covered Branches: 1754
Branch Coverage: 98.71%
Branches in Coverage %: Yes
Coverage Strength: 388840.06 hits per line

💛 - Coveralls

@Yukaii Yukaii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of b4afef1 (COMMENT only).

Verdict: ship. Risk: low. No P0/P1/P2 found, only nits. HTML is unchanged, and positions change only for the table cell tokens this PR targets.

What I checked on this head:

  • npm test on Node 24.21: lint ok, build ok, mocha 1624 passing, 1 pending, specsplit ok (master: 1620 passing).
  • The examples in the description, compared with master:
    • a | b\n--|--\nc | d: master puts the first header inline at 1 (" ") and the second at 5 ("\n"), with map [1,2]. This PR puts th_open at 0/size 0, inline at 0 and 4 with map [0,1], the inner th_close at 2, and the last th_close at 5/size 0. Body cells: 12, 14/1 (|), 16, close 17/0.
    • | i | j without a trailing pipe: the last td_close moves from 23/size 1 ("\n") to 23/size 0.
    • Autocompleted cells for | v | under a 3-column header: open/inline/close move from 32–34 (past the line) to 33/size 0. The trailing | is still claimed by the td_close of v (32/size 1).
    • \u00a0| a | b |: th_open moves from 0 (the U+00A0) to 1 (|).
  • A/B against master over 4853 documents (fixtures bare, in > , and in - , plus benchmark samples; two option presets): HTML identical. Token differences are limited to th_*/td_* open/close tokens and the inline tokens right after them. No other token changed.
  • Randomized check, 4000 tables (4642 cells): rows with and without leading/trailing pipes; prefixes > , >\t, - , -\t, 1. , > - , - > ; spaces, tabs, U+00A0 and U+3000 around cells; empty cells; \|; CRLF; fewer or more cells than the header. Invariants: the inline starts at the cell's first character, a size-1 open/close sits on a |, and nothing passes the end of the line. Master has 4056 violations; this PR has 0.
  • Performance: setCellPositions runs once per row, and a micro-benchmark on a 3000-row table shows no difference from master beyond noise.
  • Merged with #26 onto master in both orders: the only conflict is the [14.2.1] section of CHANGELOG.md; test/annotation.js merges cleanly. With both bullets kept, npm test gives 1630 passing, and #26's table trap test still passes.

Notes:

  • The header inline.map change (delimiter line → header line) is visible to plugins. A plugin that uses inline.map to locate its source line now finds the header line in header cells. The CHANGELOG mentions it, which is enough. I'd keep that sentence in the 14.2.1 notes even if the entries are reworded during the rebase.
  • nit inline: the leading/trailing-pipe detection is duplicated.

Merge order: I'd merge this PR first. #26 then only needs to resolve the CHANGELOG conflict by keeping both bullets under a single ### Fixed.

Comment thread lib/rules_block/table.mjs
columns = escapedSplit(lineText)
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') columns.pop()
let hasLeadingPipe = columns.length > 0 && columns[0] === ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these four lines are repeated at L265–268 for body rows. A small helper (e.g. splitRow(lineText) → { columns, hasLeadingPipe, hasTrailingPipe }) would keep the header and body rows from drifting apart. Not blocking.

Comment thread lib/rules_block/table.mjs
token_il.size = token_il.content.length
token_il.map = [nextLine, nextLine + 1]
columnVIndex += sourceCellLength(columns[i])
token_il.map = [startLine, startLine + 1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: header inlines now get map [startLine, startLine+1]. The inline state takes startLine from map[0], so header cells now read lineOffsets of the header line instead of the delimiter line. That is the right line, and it matches th_open.map. Plugins that relied on the old value will see the change; the CHANGELOG covers it.

@Yukaii Yukaii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving per the review above; nits only.

@Yukaii
Yukaii merged commit c694146 into master Nov 26, 2026
2 checks passed
@Yukaii
Yukaii deleted the fix/dev-3395-table-cell-positions branch November 26, 2026 08:55
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.

3 participants