Repository navigation
refactor(editor): introduce shared Markdown code scanners - #21554
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — a couple of boundary cases and one design question, inline below.
Reviewed changes — introduces five new editor modules that scan Markdown code boundaries; no existing call site is rewired in this layer, so runtime behavior is unchanged for now. I read the full diff and checked the scanners against [email protected].
markdown-fence-scanner.ts— line iterator, a stateful fence tracker, andgetMarkdownFenceRangesplus cursor helpers.markdown-code-span-scanner.ts— indexes backtick runs and pairs an opener only with a same-length run inside one marked leaf block.markdown-code-stripping.ts—stripMarkdownCoderemoves fence and inline-code ranges while preserving line breaks.markdown-scan-ranges.ts— extracted fence/code-span range helpers for thedetails-markdown-html/raw-markdown-htmlpath.markdown-scanner-boundaries.test.ts— three boundary tests (info-string backticks, escaped opener, CR-only lines); all pass.
ℹ️ Two fence scanners and two code-span scanners ship together with different answers
This PR's premise is a shared scanner layer, yet markdown-fence-scanner.ts and markdown-scan-ranges.ts each export a MarkdownFenceRanges type and each compute fence ranges — and they disagree on real input. The same is true of the two span scanners: markdownCodeSpanRanges pairs backticks across a blank line while createMarkdownCodeSpanScanner refuses to. If the split is deliberate (the ranges variant is a drop-in for raw-markdown-html.ts, the block-aware scanner is for stripMarkdownCode), a one-line note in each module saying which consumer it is for would stop the next caller from reaching for the wrong one; otherwise one should delegate to the other.
Technical details
# Divergent scanner outputs
## Affected sites
- `markdown-fence-scanner.ts:89` `getMarkdownFenceRanges` vs `markdown-scan-ranges.ts:5` `markdownFenceRanges` — both return `MarkdownFenceRanges` but implement different closing-fence rules.
- `markdown-code-span-scanner.ts:189` `findSpanEnd` vs `markdown-scan-ranges.ts:73` `markdownCodeSpanRanges` — block-aware vs fence-aware-only pairing.
## Evidence
- `'```\n~~~\n```~~~\nafter\n'`: `getMarkdownFenceRanges` → `[[0,15]]` (matches marked); `markdownFenceRanges` → `[[0,21]]`.
- `'`a\n\nb`'`: `markdownCodeSpanRanges` → `[[0,6]]`; `createMarkdownCodeSpanScanner(...).findSpanEnd(0)` → `null`.
## Open questions for the human
- Is the split deliberate? If yes, document the intended consumer per module so the two are not used interchangeably.ℹ️ The block-boundary heuristic is the riskiest part and has no tests
markdown-code-span-scanner.ts lines 95-125 encode roughly thirty lines of marked leaf-block heuristics (blank lines, fences, blockquote depth, table delimiter rows, SINGLE_LINE_BLOCK, LEAF_BLOCK_START) whose entire job is deciding when backticks may pair. None of the three tests exercise a block boundary — they cover fence-info rejection, escaped openers, and CR-only lines. The PR body says integration suites arrive in the next layer, but the next layer will depend on these rules, so pinning them now is cheaper than discovering drift later.
Technical details
# Untested block-boundary heuristic
## Affected sites
- `markdown-code-span-scanner.ts:95-125` — block-id computation.
- `markdown-scanner-boundaries.test.ts` — the only test file; no case crosses a block boundary.
## Required outcome
- Cover at least: a span pairing across a plain line break within one paragraph, a span that must not pair across a blank line, a list-item boundary, a blockquote lazy continuation, and a table header/delimiter boundary.
## Suggested approach
- Assert `findSpanEnd` directly on the opener index for each case; that pins the boundary without routing through `stripMarkdownCode`.DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
| // A top-level opener may be indented by at most three spaces. Closers are matched | ||
| // separately below because marked allows them to be indented within the fence. | ||
| const FENCE_LINE = /^[ ]{0,3}(`{3,}|~{3,})/ | ||
| const INDENTED_FENCE_LINE = /^[ \t]*(`{3,}|~{3,})/ |
There was a problem hiding this comment.
marked 17's closing fence is (?: {0,3}\1[~]* (?=
|$)|$)— a closer is limited to three spaces, same as the opener — so the lines 9-10 comment has it backwards. With[ ]here,getMarkdownFenceRanges('
after
')` closes at the four-space line (`[[0,12],[18,22]]`) where marked keeps one block (`[[0,22]]`); a tab-indented closer diverges the same way. `FENCE_LINE` already implements marked's closer, so `INDENTED_FENCE_LINE` can be dropped.
| closingPattern: new RegExp( | ||
| // CommonMark 4.5: a closing fence may be followed only by spaces or | ||
| // tabs, unlike `\s`, which also matches non-ASCII whitespace. | ||
| `^ {0,3}${openingFenceMatch[1][0]}{${openingFenceMatch[1].length},}[ \\t]*$` |
There was a problem hiding this comment.
marked's closing pattern is {0,3}\1[~]* , so ~~~ ` closes a ` block, but[ ]$here rejects it:markdownFenceRanges('```
```~~~
after
')` stays open (`[[0,21]]`) where marked closes at 15. Inherited from `details-markdown-html.ts`, but this shared copy is the natural place to add `[~\`]*`.
* refactor(editor): introduce shared Markdown code scanners (#21554) * refactor(editor): add shared Markdown scanners * test(editor): verify standalone scanner boundaries * fix(editor): preserve line endings and fenced code boundaries * fix(editor): keep bare dash lines out of table scanning

ELI5
Provides reusable code-boundary scanners before integrating them into editing.
What Changed
Adds fence, code-span, code-stripping, and range scanners. Rejects invalid fence info and escaped openers; supports CR-only lines. Existing callers switch in the next layer.
Why
Reviewing boundary rules separately makes the later integration easier to assess.
Linked Issue
Related to #9858; this stack is a partial compatibility improvement and does not close the full request.
Visual Proof
N/A — no renderer or existing call-site behavior changes in this layer.
Testing
Three standalone boundary tests passed; the next layer includes scanner integration suites.
All tests and Electron validation used
ORCA_BACKGROUND_LAUNCH=1. UI screenshots were captured through CDP without revealing windows. No remote protocol, Git-provider, or mobile schema changes are included.Review
Low direct runtime risk: adds functions without switching existing production callers.
Review order: #21553 → #21554 → #21527 → #21529 → #21530 → #21531 → #21532. Each PR targets the preceding branch; merge from left to right.
Review comments were checked against the owning layer and fixes propagated through the dependent branches. See the inline replies for the individual resolutions. Risk here describes the changed surface, not an assertion that failing cases remain.
Agent skill upstream boundary
Checklist