Skip to content

fix: end paragraph_close after the last content character in containers - #25

Open
galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3359-paragraph-close-position
Open

galaxian85 wants to merge 1 commit into
masterfrom
fix/dev-3359-paragraph-close-position

Conversation

@galaxian85

Copy link
Copy Markdown

摘要

修正 Linear DEV-3359:在 blockquote 等容器裡,多行段落的 paragraph_close.position 偏短。第二行開始,每一行都少算被拿掉的 > 前綴長度。這個問題在 #22 之前就存在。

這是 14.2.1 批次的一部分,merge 後先不要發版。 等 14.2.1 的其他項目都 merge 後再一起發版。

原始碼 修正前 修正後
> a\n> b *c* d\n> e 13 17
> > a\n> > b\n> > c 9 17
> a\nb lazy\n> c 12 14
- > a\n > b 7 11
> [!NOTE]\n> b *c* d\n> e 19 23

輸出的 HTML 不變。inline 子 token 的位置本來就正確,這次不動。

修法

lib/rules_block/paragraph.mjs:

token_c.position = start + content.length +
  (content.length > 0 ? state.lineOffsets[contentLine + countNewlines(content)] : 0)
  • contentLine 是 trimmedContentStart 找到的 content 起始行;lineOffsets 已經從這一行重新起算。
  • 起始行加上 trim 後 content 裡的換行數,就是最後一個字元所在的行,再加上那一行的 offset。
  • 頭尾被 trim() 拿掉的空白不會算進去,包括整行的 U+00A0。tab 補的 padding 已經在 lineOffsets 裡扣掉。
  • content 是空字串時維持 start,和以前一樣。

和 Linear 的建議做法一樣,只是改成從 trim 後的 content 往下數行數,不必另外回頭算 trim 前的字串。

沒有改的部分

  • inline token 的 size 維持 content.length(2026-11-18 決定)。hackmd-production 的 githubAlertsPositionPlugin.js 在 content 座標裡用 oldPos + oldSize - newSize 算 alert title 的長度。如果 size 改成原始碼長度,alert 的位置會出錯,兩邊就必須一起升級。這次在 Token#size 的 JSDoc 和 paragraph/lheading 的註解寫清楚這個定義,並加了測試鎖住。
  • lheading.mjs 的 heading_close 本來就正確,只補了註解。

文件

  • lib/token.mjs:Token#position 說明 paragraph_close 的位置;Token#size 說明 inline 的 size 是 content 長度。
  • CHANGELOG.md:新增 [14.2.1] - Unreleased。

測試

test/annotation.js 新增 4 個測試。修正前,其中 3 個會失敗:

  • Linear 列的 5 個例子。
  • 21 個邊界情況,都寫了確切的預期值:巢狀 blockquote、list 裡的 blockquote、blockquote 裡的 list、lazy continuation、行尾空白、開頭或結尾的整行 U+00A0、tab(含部分展開的 tab)。
  • 通用檢查:用 400 組隨機生成的段落,zwspIndent 開和關各跑一次。檢查三件事:src[paragraph_close.position - 1] 是 content 的最後一個字元;inline.position 到 paragraph_close.position 之間的原始碼以這個字元結尾;這個字元在 content 最後一行對應的原始碼行。
  • 第 4 個測試鎖住 paragraph 和 setext heading 的 inline size 等於 content.length,修正前後都通過。

npm test(Node 24):1624 passing、1 pending,lint 通過。

hackmd-production 驗證

在 hackmd-production 用 symlink 換成這個 branch build 出來的 fork,用 bin/agent/test 跑和 markdown 相關的 spec,包括 test/browser/render/position-invariant.test.ts(涵蓋 alert)、code-source-map、excerpt、linkify-position、markdown-it-plugins,以及 render 相關的測試:

14.2.0(npm) 這個 branch
node 8 個檔案、135 個測試全過 8 個檔案、135 個測試全過
browser 15 個檔案、163 個測試全過 15 個檔案、163 個測試全過

用的是 hackmd-production#6100 的 branch(已升到 14.2.0),因為 fork 的 master 已經包含 DEV-3358 的 srcLineStarts 改動,要搭配 #6100 才對。hackmd-production 不需要改 code,14.2.1 發版後升級依賴就可以。

🤖 Generated with Claude Code

…rs (DEV-3359)

paragraph_close.position was start + content.length, which misses the
container markers (`> `) dropped before later lines of a multi-line
paragraph. Add the line offset of the line holding the last content
character: contentLine (where the trimmed content starts, offsets rebased
by trimmedContentStart) plus the newlines in the trimmed content, so
trailing whitespace trimmed away, even whole lines, is not counted.

The inline token size stays content.length (hackmd-production computes
GitHub alert titles in content coordinates from it); document that in
Token#size and the paragraph and lheading rules. Rendered HTML is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 44262088967

Warning

No base build found for commit 3b368fe on master.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 99.326%

Details

  • Patch coverage: 22 of 22 lines across 3 files are fully covered (100%).

Uncovered Changes

No uncovered changes found.

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 6692
Covered Lines: 6659
Line Coverage: 99.51%
Relevant Branches: 1770
Covered Branches: 1746
Branch Coverage: 98.64%
Branches in Coverage %: Yes
Coverage Strength: 393146.86 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