Skip to content

fix: cover the last line in the size of an unclosed fence (DEV-3424) - #28

Open
galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3424-unclosed-fence-size
Open

galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3424-unclosed-fence-size

Conversation

@galaxian85

@galaxian85 galaxian85 commented Nov 26, 2026 •

Copy link
Copy Markdown

DEV-3424

問題

fence 沒有結尾的 marker 時(一直延伸到文件或容器結尾),size = pos - originalPos 裡的 pos 停在搜尋結尾 marker 的迴圈最後檢查的地方:可能在那一行的開頭,也可能在中間。所以 size 可能沒蓋到最後一行,也可能多蓋到換行。這在 #22 之前就有(DEV-3267 的「Pre-existing gaps」)。

原始碼 14.2.0(position、size、涵蓋的範圍) 這個 PR
```\nx\ny 0, 6, ```\nx\n 0, 7, ```\nx\ny
> ```\n> x\n> y 2, 10, ```\n> x\n> 2, 11, ```\n> x\n> y
- ```\n x\n y 2, 10, ```\n x\n 2, 11, ```\n x\n y
> ```\n> x\n\ny(blockquote 結束) 2, 6, ```\n> 2, 7, ```\n> x
- ```\n x\ny(縮排變少) 2, 8, ```\n x\n(多蓋到換行) 2, 7, ```\n x
```\nx\n``` y(最後一行不是合法的結尾 marker) 0, 10 0, 11
```js(只有開頭那一行) 0, 3, ``` 0, 5, ```js

修法和慣例

只改 lib/rules_block/fence.mjs:沒有結尾 marker 時,size = state.eMarks[nextLine - 1] - originalPos。也就是結束在 map 最後一行的行尾,不含換行。有結尾 marker 的 fence 本來就結束在結尾那一行的行尾,規則相同。只有開頭那一行的 fence 結束在那一行的行尾,會包含 info string。

結尾有空行時(```\nx\ny\n\n,map 是 [0, 4]),照 issue 的建議結束在最後一個空行的開頭(size 8,和 14.2.0 一樣)。沒有改成「結束在最後一個非空行」,因為 map 和 content 都包含結尾的空行。結束在 map 最後一行的行尾,不管 fence 有沒有結尾 marker,position + size 都和 map 一致。

有結尾 marker 的 fence 走的是原本的路徑,位置不變。position、content、map、meta.srcLineStarts 和輸出的 HTML 都不變。

hackmd-production 有沒有讀 fence 的 size?

有兩個地方,都不用改 code。

  • universal/markdownService/helper.ts(extractExcerpt,第 17–18、56 行):每個 token 都會算 end = position + size,fence 在 contentTokenNames 裡;anchor 的起點在 end 或之後時,就跳過這個 fence。文字本身由 sourceToCodeContentIndex(position、meta、content)取得。在 14.2.0,comment 錨在沒有結尾 marker 的 fence 最後一行時,fence 的 excerpt 是空的。用臨時 spec(已刪除)比較 npm 14.2.0 和這個 branch:
    • ```\nx\nyz,anchor 在 yz:"" → "yz"(blockquote、list 裡也一樣)
    • 有結尾 marker 的 fence,以及從 fence 前面幾行開始的範圍:不變
    • - ```\n x\ny,anchor 在 x 後面的換行:"\n" → ""。舊的 size 多蓋到那個換行。
  • universal/markdownService/plugins/githubAlertsPositionPlugin.js:53-67:對 alert_open 和第一個 inline 之間的 token,用 position + size 改寫 position。fence 只有在標題段落之前才會出現在這裡。沒有結尾 marker 的 fence 會把 blockquote 剩下的部分都吃掉,所以不會走到這個迴圈;有結尾 marker 的 fence size 不變。

其他地方都沒有讀 fence 的 size:

  • codeSourceMap.ts(universal 和 frontend)用的是 position、markup、info、meta 和 content。
  • frontend 的 fence renderer 會輸出 data-position,但不輸出 data-size;只有 code_block 會輸出 data-size。
  • comment/selection 相關的 code 從 DOM 讀 data-size,fence 的區塊不會有這個屬性。

測試

  • test/annotation.js 新增 3 個測試,放在 should annotate code blocks 後面,避免和 fix: end paragraph_close after the last content character in containers #25–fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) #27 衝突:
    • 沒有結尾 marker 的 fence:上面 6 個例子;只有開頭那一行(```、```\n、```js、```js\n);~~~;結尾 marker 太短;結尾有空行;blockquote、list 裡被容器結束。
    • 有結尾 marker 的 fence 維持原本的 position 和 size。
    • 通用檢查:5 種容器 × 6 種內容 × 4 種結尾,每個 fence 都結束在 map 最後一行的行尾。
    • 拿掉修正時,新測試有 2 個失敗(有結尾 marker 的那個仍然通過)。
  • npm test:1623 passing、1 pending(master 是 1620),lint 通過。
  • A/B(master 和這個 branch):fixtures 和 benchmark samples(1081 份)× 3 種設定(default、commonmark、full)。
    • HTML 完全相同。
    • token 的差異只有 fence 的 size,每種設定 201 個,全部是沒有結尾 marker 的 fence,現在都結束在 map 最後一行的行尾。其他欄位都沒有變。
  • hackmd-production(develop,已包含 #6100、已是 14.2.0),把 @hackmd/markdown-it 換成指向這個 branch 的 symlink:node 8 個檔案、135 個測試,browser 15 個檔案、163 個測試,全過,和同一個 commit 上用 npm 14.2.0 的結果相同。

發版

這是 14.2.1 批次(patch)的一部分。merge 後請先不要發版,等其他項目都 merge 後再一起發 14.2.1。CHANGELOG 新增了 [14.2.1] - Unreleased 段落,會和 #25/#26/#27 衝突,後 merge 的 PR 要把 ### Fixed 合併起來。

🤖 Generated with Claude Code

A fence without a closing marker (closed by the end of the document or of
its container) took its size from wherever the closing marker search
stopped, so it ended short of its last line or past its newline. End it at
the end of the last line in its map, without the newline, like a closed
fence. position, content, map, meta and the rendered HTML are unchanged.

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

Copy link
Copy Markdown

Coverage Report for CI Build 45764242818

Coverage increased (+0.001%) to 99.337%

Details

  • Coverage increased (+0.001%) from the base build.
  • Patch coverage: 5 of 5 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6677
Covered Lines: 6644
Line Coverage: 99.51%
Relevant Branches: 1769
Covered Branches: 1746
Branch Coverage: 98.7%
Branches in Coverage %: Yes
Coverage Strength: 390705.71 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