Repository navigation
fix: map inline positions after escaped pipes in table cells (DEV-3396) - #30
Open
galaxian85 wants to merge 1 commit into
Open
galaxian85 wants to merge 1 commit into
galaxian85 wants to merge 1 commit into
Conversation
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>
Coverage Report for CI Build 46560106566Coverage remained the same at 98.943%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DEV-3396
問題
表格的儲存格裡有
\|時,escapedSplit會先拿掉反斜線,再把儲存格的文字交給 inline 解析。children 的位置是用 content 座標算的,coreinline只加上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 從反斜線前的空白開始、size4。| x | `a\|b` y |:code_inline的size是 3(應為 4),後面的 text 往前偏 1。修正後size4,後面的 text 正確。| a \| x |:texta | x是position2、size5,slice(2, 7)是a \|,蓋不到x。修正後size6。> | h \| [l\|k](u) @me |(blockquote 裡的表格):link 文字和後面的 text 一樣往前偏。修正後正確。做法
|的儲存格,把「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 是\|)。inline再從 content 算):只有內建 table rule 做出來的格子會被標記,第三方表格 plugin 的格子即使有|也不會被換算。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' | '從反斜線前的空白開始、size4);剛好在\|前面的 0 長度 token 放在反斜線前面。position和size都是 0 的 children(link_open/link_close等)不動。null檢查)。text_join之前執行,sourceGap的連續性檢查會通過。跨過\|的 text 是content.length !== size,text_join不合併,會留成獨立的 token(預期)。getLineOffset/getLinePad的語意、escapedSplit、inline token 本身的size(維持content.length,DEV-3359 的決定)。沒有新增公開欄位。已知限制(14.3.0 再處理)
跨過
\|的 token 內部,用position + index換算時,|之後的字還是差 1(每個\|差 1)。例如| a \| x |的 texta | x外框是a \| x(size6),但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之後、tableCellshelper 之前(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。\|、smartypants、linkify、HTML、image、strikethrough、U+00A0、各種容器),預設/commonmark/typographer 三種設定:|的格子裡的 children 的position/size,沒有其他欄位改變。fixtures 只有 3 個 case 的 token 改變(都是格子裡有\|的表格),benchmark samples 0 差異。lookAhead先蓋掉lineOffsets):body 列的位移量和 master 一樣(都是 +4),換算只在上面多加反斜線。benchmark/samples28 份,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 驗證(
develop7213b0bff4,什麼都沒有 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),只接受那幾種形狀。texta | b現在的data-size是 6(含反斜線),長度 5,所以被判失敗。14.2.0 是size5,走另一個分支,再用\|→|的正規化比對通過。執行期不受影響:
frontend/comment/selection.js對文字節點用position + offset(不變,即上面的已知限制),用position + size算結尾的地方現在會蓋到格子文字的結尾(變準)。只有 smartypants 和 tab padding 被當成不可分割的節點。改法(只改 spec,在 worktree 裡試過,11 個測試全過,沒有 commit;Charlie 已確認可以接受,hackmd-production 升級 14.2.1 的 PR 要一起改):
臨時腳本(vitest browser spec,用 production 的 server parser
getParser()和 frontend rendererloadFrontendRenderer();18 份「格子裡有\|」的樣本:6 個表格 × 頂層/blockquote/list,含 smartypants(c)、(r)、--、inline spoiler\|\|secret\|\|、ruby{漢字\|かんじ}、mention(每個@me都有 anchor)、沒有開頭|的列;excerpt 用helper.ts的extractExcerpt,表格範圍內每個位置、長度 1–6 的視窗)。跑完已刪除。data-position/data-size後 0 差異(data-position有變,都是\|之後的 smartypants/spoiler/ruby/text,變準)。data-position,18 份都一樣)。secret從錯的 16 變成 18),594 個是 smartypants(--→–、(r)→®)讓我的自動判斷無法分辨,抽看都是變準或一樣;被判變差的 9 個中,6 個其實是(r) f的視窗現在拿得到®(判斷誤報),3 個是一個字的視窗剛好落在 spoiler 標記\|\|的|上:master 回傳|,這個 branch 回傳空字串(那個|是 spoiler 的標記,不是文字)。map,已確認可以接受)。|的列裡、\|後面的 mention(q \| @me | …)在 14.2.0 是連結,fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) #27 之後變成純文字。14.2.0 剛好是「沒有開頭|的 +1」和「\|的 −1」互相抵消才對上;fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) #27 修掉 +1 之後,就和其他\|後面的 mention 一樣(mention 不在這次範圍,handoff「剩下的項目」fix: use first subtract lineStart to get more accuracy indented length #11)。和其他 PR 的關係
lib/rules_core/inline.mjs(positionOffset平移srcLineStarts),兩邊會衝突,後 merge 的那個要解。CHANGELOG.md和test/annotation.js衝突(新測試放在should annotate table cells after an escaped pipe後面,避開了 fix: end paragraph_close after the last content character in containers #25 的檔案結尾、fix: cover the last line in the size of an unclosed fence (DEV-3424) #28 的約第 78 行、Shift srcLineStarts with positionOffset; document core block rule and ZWSP caveats (DEV-3425) #29 的約第 622、826 行)。這是 14.2.1 批次的一部分,merge 後請先不要發版。
🤖 Generated with Claude Code