Skip to content

Shift srcLineStarts with positionOffset; document core block rule and ZWSP caveats (DEV-3425) - #29

Open
galaxian85 wants to merge 3 commits into
masterfrom
fix/dev-3425-offset-followups
Open

galaxian85 wants to merge 3 commits into
masterfrom
fix/dev-3425-offset-followups

Conversation

@galaxian85

Copy link
Copy Markdown

DEV-3425

DEV-3267 剩下的三個和 offset 有關的項目(#5、#6、#7)。一個項目一個 commit,可以逐個 commit review。 只有 #5 改行為,而且只影響有傳 positionOffset 的人;#6、#7 只改註解和 CHANGELOG。輸出的 HTML 都不變。

#5 positionOffset 沒有平移 srcLineStarts(修 code)— ba54548

問題:core inline rule 的 positionOffset 只加到每個 token 的 position。fence/code_block 的 meta.srcLineStarts 也是絕對位置,卻沒有跟著加。

例:前面有 18 個字元的 front matter,md.core.ruler.at('inline', s => inline(s, 18)),內容 > ```\n> code\n> ```:fence.position 是 20(正確),srcLineStarts 是 [8],應為 [26]。

做法(lib/rules_core/inline.mjs):

  • positionOffset 不是 0/undefined 時,tok.meta.srcLineStarts 的每個值也加上它(meta 是 null 或沒有 srcLineStarts 時跳過)。沒有傳 offset 時走的路徑和原本一樣。
  • 查過其他 meta 欄位:indentZwsp、srcLinePads 是數量,code_inline 的 srcLineOffsets/contentLineStarts 是相對於 token.position 的位置,都不用改。srcLineStarts 是唯一存絕對位置的 meta 欄位。
  • 函式開頭加了註解說明 positionOffset 的語意。

測試(should shift srcLineStarts with the positionOffset of the core inline rule):DEV-3267 的例子、沒有容器的 fence、blockquote 裡的 code_block、有 tab padding 的 list 裡的 code_block,各自用 undefined、0、18 三種 offset 檢查 position 和 srcLineStarts,並確認平移後的位置對到「把 front matter 加回去的原始碼」。修正前會失敗。

#6 換掉 core block rule 會讓行偏移歸零(只改文件)— 5491ec9

問題:getLineOffset/getLinePad 透過 sourceBlockState() 讀 core state 的 blockState,這是 fork 的 core block rule 設的。用 upstream 的寫法換掉 core block rule 的話,偏移會全部當成 0,不會報錯。例:> a\n> b *c* 的 em_open 從 8 變成 6。內建的 inline rule(emphasis、escape、backticks、entity 等)都是透過這兩個 util 讀偏移,所以全部受影響。

做法:在 lib/rules_core/block.mjs、getLineOffset、sourceBlockState(lib/common/utils.mjs)的註解和 CHANGELOG 寫清楚:自訂的 core block rule 要把 md.block.parse 的回傳值存到 state.blockState,並附上寫法。行為不變,沒有加 warning 或 fallback。

#7 ZWSP 的說明和實際不符(只改文件)— 57a734e

問題:zwspIndent 的說明寫 U+200B 不代表容器 marker(例如 > ),但 -\t s 的 code_block 有 4 個 U+200B、srcLineStarts 是 [0],第一個 U+200B 對到 list marker -。

發生條件(查過的結果):

  • getLines 對 bMarks[line] 到行內文字之間的每個字元都加一個 U+200B。blockquote 會把 bMarks 移到 > 後面,所以 > 不會有 U+200B;list item 在第一行不移 bMarks,而是用 tShift 把 marker 遮成空白,所以這一行的 list marker 和後面的空白都會有 U+200B。
  • 所以條件是:list item 的第一行就是 indented code block(marker 後面有 5 欄以上的空白)。和 tab 無關:- s 也有 6 個 U+200B,第一個對到 -。
  • 同一行開了好幾層 list 時,每層的 marker 都算(- - s 有 8 個)。blockquote 裡的 list 只算 list marker(> - s 是 - );list 裡的 blockquote 只算 > 後面的縮排(- > s)。
  • fence 不會發生:fence 的內容從 marker 的下一行開始。
  • srcLineStarts/indentZwsp/srcLinePads 的換算規則在這些情況下都是對的(每個 U+200B 對到它代表的字元,行內文字逐字對到原始碼);zwspIndent: false 時 srcLineStarts 也正確(例如 -\t s 是 [4])。所以只有說明不準,沒有改換算規則。

做法:更正 lib/index.mjs 的 zwspIndent 說明、lib/presets/{default,commonmark,zero}.mjs 的註解、lib/rules_block/state_block.mjs 裡 getLines 的註解(replaceIndentSpaceWithZWSP、U+200B 的換算、迴圈內的註解)。CHANGELOG 的更正寫在 14.2.1(已發佈的 14.1.3/14.2.0 段落沒改)。U+200B 的數量不變。

測試(should map U+200B on the first line of a list item to its list marker):記錄現況,上面 6 個例子的 indentZwsp、srcLineStarts(zwspIndent 開和關),以及每個 U+200B 對到的原始碼字元。

新測試的位置

兩個新測試放在 test/annotation.js 既有的 srcLineStarts 測試旁邊(should map code lines to source with srcLineStarts 和 should map U+200B on a line with a partially consumed tab to the stripped indent 的後面,約第 623、669 行),避開 #25、#26(檔案最後)、#27(約第 132、269 行之後)、#28(should annotate code blocks 後面)。

hackmd-production

  • 有沒有用到:grep 過 positionOffset、ruler.at('inline'/ruler.at('block'、rules_core/inline|block、__rules__/__find__,都沒有。也沒有換掉 core block rule(production 的 blockState 只是存 env.state_block 的區域變數名稱,和 core state 的 blockState 無關)。所以 production 不用改 code。
  • spec(develop,已是 14.2.0,node_modules/@hackmd/markdown-it 換成指向 fork 的 symlink):node 8 個檔案、135 個測試;browser 15 個檔案、163 個測試,全過,和 14.2.0 的基準一樣。已還原 node_modules。

測試結果

  • npm test:1622 passing、1 pending(master 1620)。三個 commit 各自都通過 npm test(1621 → 1621 → 1622)。
  • A/B(master vs 這個 branch):fixtures(每個 case 分開)和 benchmark samples 共 1108 份 × 3 種設定(default、commonmark、zwspIndent: false + html/linkify/typographer),HTML 和 token(整份 JSON)完全相同。

發版

這是 14.2.1 批次的一部分,merge 後請先不要發版。CHANGELOG 新增 [14.2.1] - Unreleased,會和 #25–#28 衝突,後 merge 的那個再合併 ### Fixed/### Documentation。

🤖 Generated with Claude Code

galaxian85 and others added 3 commits November 26, 2026 14:12
…ule (DEV-3425)

`positionOffset` was added to `token.position` only; `meta.srcLineStarts`
of fence and code_block tokens are absolute offsets too and stayed
unshifted (`> ```\n> code\n> ``` ` with an offset of 18 gave [8], not [26]).
Relative offsets (`srcLineOffsets` of code_inline) need no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
getLineOffset / getLinePad read the line offsets from the block state the
core block rule keeps as state.blockState. A replacement rule that does not
set it (as upstream's) makes them return 0 silently, shifting inline
positions in containers. Document the requirement; behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The U+200B prepended to code lines stand for every character from
bMarks[line] to the line text. Blockquotes move bMarks past `>`, but list
items keep it at the start of their first line and mask the marker with
tShift, so an item starting with an indented code block (`-\t  s`,
`-     s`) gets U+200B for its marker too. The docs said container markers
were never included. Correct them and add a test recording the current
counts and srcLineStarts; behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@galaxian85
galaxian85 requested a review from Yukaii November 26, 2026 06:16
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45766996991

Coverage decreased (-0.07%) to 99.269%

Details

  • Coverage decreased (-0.07%) from the base build.
  • Patch coverage: 5 uncovered changes across 1 file (53 of 58 lines covered, 91.38%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
lib/rules_core/inline.mjs 11 6 54.55%
Total (8 files) 58 53 91.38%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6718
Covered Lines: 6680
Line Coverage: 99.43%
Relevant Branches: 1768
Covered Branches: 1744
Branch Coverage: 98.64%
Branches in Coverage %: Yes
Coverage Strength: 388321.03 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