Repository navigation
fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) - #27
Conversation
… (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>
Coverage Report for CI Build 45136912641Warning No base build found for commit Coverage: 99.34%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsRequires a base build to compare against. How to fix this → Coverage Stats💛 - Coveralls |
Yukaii
left a comment
There was a problem hiding this comment.
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 teston 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 headerinlineat 1 (" ") and the second at 5 ("\n"), withmap[1,2]. This PR putsth_openat 0/size 0,inlineat 0 and 4 withmap[0,1], the innerth_closeat 2, and the lastth_closeat 5/size 0. Body cells: 12, 14/1 (|), 16, close 17/0.| i | jwithout a trailing pipe: the lasttd_closemoves 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 thetd_closeofv(32/size 1). \u00a0| a | b |:th_openmoves 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 toth_*/td_*open/close tokens and theinlinetokens 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:
setCellPositionsruns 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 ofCHANGELOG.md;test/annotation.jsmerges cleanly. With both bullets kept,npm testgives 1630 passing, and #26's table trap test still passes.
Notes:
- The header
inline.mapchange (delimiter line → header line) is visible to plugins. A plugin that usesinline.mapto 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.
| 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] === '' |
There was a problem hiding this comment.
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.
| token_il.size = token_il.content.length | ||
| token_il.map = [nextLine, nextLine + 1] | ||
| columnVIndex += sourceCellLength(columns[i]) | ||
| token_il.map = [startLine, startLine + 1] |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Approving per the review above; nits only.
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)為例:|:每一列的columnVIndex都先+1跳過開頭的|,但只有真的以|開頭時columns.shift()才會拿掉空的第一欄,所以整列每一格都往右偏 1。th_open在 0、size 1(指到a);inline 在 1;第二個th_open在 3;第二個 inline 在 5(指到\n)th_open0/size 0;inline 0;th_close2(|);th_open2/size 1;inline 4;th_close5/size 0|:最後一格的 close token 還是size 1,會蓋到換行(沒有開頭也沒有結尾時甚至指到下一行)。| i | j的th_close從\n/size 1 改成在儲存格結尾、size 0。| s | t | u |的 body| v |:第二、三格原本在 33/34(下一行),現在 open、inline、close 都在行尾(33)、size 0。size 1的 close 仍然由最後一個真的格子認領(| v |的結尾|)。map:從[nextLine, nextLine + 1](分隔線)改成[startLine, startLine + 1](header 那一行)。另外順便修了一個同類的問題(不在 issue 原本的範圍,Charlie 已確認放在這個 PR,2026-11-23):
lineText用trim()去掉,但tShift不算,所以整列每一格都往左偏 1,有開頭和結尾|的表格也一樣。例:| a | b |的th_open原本在 0(指到 U+00A0),現在在 1(|)。偏移量是 U+00A0 的個數(不一定是 1)。這是唯一會改變「有開頭和結尾|的表格」位置的地方(從錯改成對)。fork 在段落已用trimmedContentStart處理同樣的情況,這裡讓表格一致。採用的慣例(和 issue 的建議相同)
|的格子(沒有開頭|的列的第一格):open token 放在儲存格開頭,size 0。|,size 1;close token 在後一個|,size 0。|由最後一個真的格子的 close token 認領,size 1。沒有結尾|時,close token 放在儲存格結尾(trim()後的行尾),size 0。eMarks),size 0。size維持content.length(DEV-3359 的決定)。|不會被認領(和之前一樣)。實作上把位置計算抽成
setCellPositions(),header 和 body 共用,在 push 完一整列的 token 之後才設位置。測試結果
fork
npm test:1624 passing、1 pending(master 1619 passing)。lint 通過。test/annotation.js新增 4 個測試:position、size和 inline 的map|後有空白)|)、空的儲存格、只有|的列、tab 縮排、list 裡(含 blockquote 裡的 list)沒有開頭|的表格、header 沒有開頭|但 body 有、\|(含列尾的\|)、開頭有 U+00A0|、格子數隨機少於 header、行尾空白、U+00A0),檢查每個 inline 的position指到該格 trim 後的第一個字元(自動補的格子為行尾)、map是該列,所有 token 的position + size不超過該列行尾,size 1的 open/close 都指到|should annotate tables裡有一列| Hello | World(沒有結尾|),原本斷言td_close蓋到'\n',這正是問題 2,改成斷言在儲存格結尾、內容為空字串。其他既有測試的結果都沒變。table-3-check.mjs:修正前 77 個 ✗,修正後 0 個。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)extra.js,含 mention、spoiler、alert 等 plugin)和 server parser(getParser()),對 23 種表格(有/沒有開頭結尾|、自動補格子、blockquote/list/alert/:::spoiler裡、儲存格裡有 mention、\|\|spoiler\|\|、強調、code、連結、emoji)比較 npm 14.2.0 和 fork:data-position,所以沒有開頭|的列,data-position都 −1(例如a | b的a從 1 變 0,現在指到正確的字)。@user現在會渲染成a.mention-anchor連結(npm/fork 的 anchor 數:| @user | hi @user |表格 1→3、@user | hi @user0→3、a | @user1→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 元素都能在任何結構裡使用。|的列、自動補的格子、header inline 的map,以及這些格子裡的 children(強調、code、連結、emoji、inline spoiler、mention)隨 parent 平移。有開頭和結尾|的列,位置完全沒變。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」):要改 coreinlinerule,還在等決定。這次沒有動escapedSplit拿掉反斜線的行為,也沒有動 coreinline。\|前後 open/close 的位置(sourceCellLength)維持正確。衝突
#25(DEV-3359)和 #26(DEV-3364)都改了
test/annotation.js和 CHANGELOG 的[14.2.1] - Unreleased。master 上還沒有這個段落,這裡照樣新增,之後 rebase 時再合併成一個### Fixed。🤖 Generated with Claude Code