Repository navigation
Shift srcLineStarts with positionOffset; document core block rule and ZWSP caveats (DEV-3425) - #29
Open
galaxian85 wants to merge 3 commits into
Open
galaxian85 wants to merge 3 commits into
galaxian85 wants to merge 3 commits into
Conversation
…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>
Coverage Report for CI Build 45766996991Coverage decreased (-0.07%) to 99.269%Details
Uncovered Changes
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-3425
DEV-3267 剩下的三個和 offset 有關的項目(#5、#6、#7)。一個項目一個 commit,可以逐個 commit review。 只有 #5 改行為,而且只影響有傳
positionOffset的人;#6、#7 只改註解和 CHANGELOG。輸出的 HTML 都不變。#5
positionOffset沒有平移srcLineStarts(修 code)—ba54548問題:core
inlinerule 的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
blockrule 會讓行偏移歸零(只改文件)—5491ec9問題:
getLineOffset/getLinePad透過sourceBlockState()讀 core state 的blockState,這是 fork 的 coreblockrule 設的。用 upstream 的寫法換掉 coreblockrule 的話,偏移會全部當成 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 寫清楚:自訂的 coreblockrule 要把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。- s也有 6 個 U+200B,第一個對到-。- - s有 8 個)。blockquote 裡的 list 只算 list marker(> - s是-);list 裡的 blockquote 只算>後面的縮排(- > s)。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
positionOffset、ruler.at('inline'/ruler.at('block'、rules_core/inline|block、__rules__/__find__,都沒有。也沒有換掉 coreblockrule(production 的blockState只是存env.state_block的區域變數名稱,和 core state 的blockState無關)。所以 production 不用改 code。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)。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