Repository navigation
fix: keep the line offsets of inline tokens when rules call getLines again (DEV-3364) - #26
Open
galaxian85 wants to merge 2 commits into
Open
galaxian85 wants to merge 2 commits into
galaxian85 wants to merge 2 commits into
Conversation
…again (DEV-3364) StateBlock#getLines overwrites lineOffsets and linePads for its lines, and inline parsing runs after the whole block parse, so a rule that read the lines of an inline token pushed earlier (e.g. a plugin probing earlier lines) changed how that token's children were mapped to the source. Before a getLines call overwrites lines of an inline token pushed earlier, save that token's values on the block state; the core inline rule writes them back before parsing the token. Rules that only read lines after the tokens pushed so far (all built-in rules) take a fast path, so documents without such probes pay almost nothing and their positions are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Coverage Report for CI Build 44482659639Warning No base build found for commit Coverage: 98.937%Details
Uncovered Changes
Coverage RegressionsRequires a base build to compare against. How to fix this → 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-3364
摘要
StateBlock#getLines每次呼叫都會覆寫[begin, end)的lineOffsets/linePads。inline 解析在整份文件的 block 解析結束後才跑,所以如果某個 rule 在 inline token 建立之後,又對同樣的行呼叫getLines(例如往回探查),這個 token 的子 token 位置就會錯。這個 PR 在覆寫前把該 token 的值存起來,coreinlinerule 解析它之前再寫回去。這是 14.2.1 批次的一部分,merge 後先不要發版。
輸出的 HTML 不變。沒有這種 plugin 的文件,所有位置都不變。hackmd-production 不用改 code。
問題和重現
一個 block rule 在每一行往回讀前 3 行,然後放棄:
> a\n> b *c*\n\nnextb、c>)、7()- a\n b *c*\n\nnextb、c)、7> > a\n> > b\n> > *c*\n\nnextb、c>)、9沒有容器(例如
a\n b *c*)的段落不受影響,因為那時每行被拿掉的字元數本來就和探查的結果一樣。調查結果:只有自訂 plugin 會踩到
paragraph、lheading、code、fence、html_block都是決定建立 token 之後,才對自己的行呼叫getLines。用 instrumented 的getLines跑過所有 fixture 和 benchmark 樣本(3284 份),沒有任何一次覆寫到已建立的 inline token 的行。extra.js,透過loadFrontendRenderer)和 server 的getParser()解析 55 份樣本:spoiler、extraTag、mention、smartypants、deflist、svg、alert 放在 blockquote/list/巢狀容器/lazy continuation 的多行段落裡。覆寫次數是 0。@hackmd/markdown-it-deflist2.2.2 是先 push 空的 inline token,再對 term 那一行呼叫getLines,讀的是自己的行。svg.ts是決定建立svg_block之後,才對自己的行呼叫。修法
不是在 push inline token 時就存快照,而是等到真的要被覆寫時才存:
getLines開始時,先看自上次呼叫以來新 push 的 token,更新「已 push 的 inline token 最遠到哪一行」(_inlineLineEnd)。begin在這一行之後就直接返回(內建 rule 都是這種情況)。map和[begin, end)重疊的 inline token,把它map範圍內的lineOffsets/linePads存進 block state 上的_inlineSaved(Map,key 是 token)。inlinerule 解析每個 inline token 前,如果它有存值,就寫回 block state 的陣列。和 Linear 建議的做法(push 時存快照)不同,理由:
lheading是 push inline token 之後才呼叫trimmedContentStart重新起算lineOffsets;deflist 也是先 push、再用getLines設 content。push 時存的快照會是錯的。延到下一次getLines開始時,這些都已經完成了。快照放在 block state 的
_inlineScanned/_inlineLineEnd/_inlineSaved上,不在 token 上,也不會被序列化。lineOffsets/linePads的名稱、索引方式(絕對行號),以及「inline 解析時陣列裡是當下 inline token 的偏移」這個語意都沒變,所以直接讀state.env.state_block.lineOffsets的 production plugin(githubAlertsPositionPlugin.js、extraTagPlugin.js)和透過getLineOffset讀的 plugin 都不用改。沒有涵蓋的部分(已寫進註解和 CHANGELOG)
getLines建立 inline token 的 rule(例如表格)會直接讀陣列裡現有的值。如果在那之前有 rule 探查過這些行,位置還是會錯,例如 blockquote 裡的表格配上往前探查的 plugin。這一點加了一個記錄現況的測試。建議探查改用getLine/bMarks/eMarks。inlinerule 裡。 在 block 和 inline 之間執行的 core rule 讀到的還是被覆寫的值。production 的githubAlertsPositionPlugin.js就是在這個時間點讀陣列,但 production 沒有會覆寫的 plugin,所以不受影響。效能
repo 的
benchmark/benchmark.mjs需要先全域安裝舊版 marked/commonmark(benchmark-deps),所以改用同一套 Benchmark.js 和benchmark/samples,只比較 master 和這個 branch(Node 24,html/linkify/typographer開啟),28 個樣本取幾何平均:差異落在雜訊範圍內。第一版每一行都記錄 owner,小文件會慢 4–6%(
block-fences慢 20%),所以改成上面的快速路徑。測試
test/annotation.js新增 6 個測試(Annotation with rules calling getLines again)。修正前,其中 5 個會失敗:begin和indent讀自己行的 rule(包住paragraph)。getLines設 content),配上前後探查。這個測試會抓到「把還在建立的 token 也納入」的錯誤實作(拿掉那個判斷時,這個測試會失敗)。state.env.state_block.lineOffsets和getLineOffset/getLinePad得到的值,要和沒有探查時一樣(production plugin 的讀法)。既有的測試都沒改。
npm test(Node 24):1626 passing、1 pending,lint 通過。和 #25(DEV-3359)一起測:在臨時 worktree 把 #25 merge 進來(只有 CHANGELOG 衝突),
npm test1630 passing。paragraph_close.position是在 push 時就算好的,有探查的 plugin 時結果也一樣(例如> a\n> b是 7)。hackmd-production 驗證
在 #6100 的 branch(
fix/dev-3358-code-source-map-real-line-starts,14.2.0)用 symlink 換成這個 branch build 出來的 fork,用bin/agent/test跑和 markdown 相關的 spec:另外用臨時 spec(沒有 commit)比較 npm 14.2.0 和 fork 的 token:用 frontend renderer 和 server parser 解析 55 份樣本(spoiler、extraTag、mention、smartypants、deflist、svg、alert,放在各種容器裡的多行段落),所有 token 和子 token 的
position/size/map、mention 的data-position,以及輸出的 HTML 完全相同。production 沒有會觸發這個問題的 plugin,所以沒有「原本錯、現在修正」的差異。🤖 Generated with Claude Code