fix(markdown): read a CRLF document exactly like its LF twin (#363)

The markdown pipeline's line-based scanners split on `\n` and then reason
about each line, so a Windows checkout (`core.autocrlf=true` — the
`ci-windows` lane) broke three of them at once:

- `splitHtmlBlocks`'s blank-line test (`/^[ \t]*$/`) read a CRLF blank line
  (`'\r'`) as content, so an HTML run never ended: on this repo's own README
  the splitter produced 4 segments instead of 36, and the markdown after a
  `<div>` run was rendered as raw text.
- `CLOSE_FENCE_RE` (`[ \t]*$`) never matched a CRLF closing fence, so
  `splitMermaidBlocks` swallowed the rest of the file and the splitter's own
  open-fence state never closed.
- `closesFence` (`/^[ \t]*$/`) left every CRLF fence open, so
  `maskCodeRegions` masked from the first fence to the end of the file and
  the link rewriter stopped claiming every `.md` link after it (first
  divergence on README.md: `./CHANGELOG.md%23v0230`, claimed under LF, left
  as an inert `#v0230` destination under CRLF).

The editor preview rendered the document twice as well: its draft snapshot
publishes CodeMirror's live document — always LF, whatever the file used —
one commit after mount, and that CRLF→LF re-render makes the host rebuild the
markdown elements whose text changed, discarding the heading ids the surface
pass had just written (the MutationObserver only heals them a microtask
later). Line endings are not an edit: the snapshot now keeps `draft` null
while the live document is the loaded content under any line-ending spelling.

Tests: `scanDocumentAnchors` and the README TOC slice split on `/\r?\n/`;
tests/markdown-crlf.spec.ts pins the CRLF≡LF invariant across the splitter,
the mask, both rewriters and the mermaid splitter, and two surface cases
mount a CRLF document through the real renderer — one of them a CRLF copy of
the README, so every lane exercises the spelling only `ci-windows` checks
out. Reverting the `\r` fixes turns nine of them red.
This commit is contained in:
Menghuan1918 committed 2026-10-06 00:54:24 +08:00
1 parent 76cf851a45
commit ee5be9b601
7 files changed
+301 -8

No files matched your search

+6 -2
View File
@@ -47,12 +47,16 @@ function fenceRun(line: string): { marker: string; length: number } | null {
return length >= 3 ? { marker, length } : null
}
/** Whether a line closes a fence opened by `marker` × `length` (CommonMark). */
/** Whether a line closes a fence opened by `marker` × `length` (CommonMark).
* The tail may carry a CRLF terminator: `\r` is the line's ending, not
* content, and reading it as content left every fence in a CRLF document
* unclosed — the masked region then ran to the end of the file and the link /
* image passes skipped everything after the first fence. */
function closesFence(line: string, marker: string, length: number): boolean {
if (line.charAt(0) !== marker) return false
let run = 0
while (line.charAt(run) === marker) run += 1
return run >= length && /^[ \t]*$/.test(line.slice(run))
return run >= length && /^[ \t\r]*$/.test(line.slice(run))
}
/**