Files
apes-sidebar/src/client/markdown-code.ts
T
Menghuan1918 ee5be9b601 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.
2026-10-06 00:54:24 +08:00

137 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The ONE code-region mask the two markdown source rewriters share
* (`markdown-images.ts` for image destinations, `markdown-navigation.ts` for
* link destinations).
*
* Both rewrites are regex passes over raw markdown, so anything that LOOKS like
* a link or an image inside a code region must be left verbatim: a README that
* documents `[x](./a.md#标题)` or `![i](./p.png)` means to show that syntax, not
* to have it rewritten. Masking preserves the region byte for byte — the
* sentinel is a NUL, which cannot appear in markdown source, and the original
* text goes back in after the pass.
*
* What counts as a code region is CommonMark's own rule: an INLINE code span
* (a backtick run closed by a run of the same length), and a FENCED block — an
* opening fence of three or more backticks OR three or more tildes, closed by a
* fence of the same character at least as long as the opener. The fence line
* itself is masked too (its leading run is what a backtick-span scan would
* otherwise mistake for a code span), and an unclosed fence runs to the end of
* the text, exactly as a renderer treats it.
*
* Both fence characters matter: `~~~` is a legal fence in CommonMark and in
* every renderer this plugin draws through, so an image or a link demonstrated
* inside one is as much an example as one inside ``` ``` ```.
*/
/** A backtick or tilde fence: three or more, at up to three spaces of indent. */
const FENCE_RE = /^ {0,3}(`{3,}|~{3,})([^\n]*)$/gm
/** An inline code span: a backtick run, then the next run of the SAME length. */
const CODE_SPAN_RE = /(`+)[\s\S]*?\1/g
/** A masked region's placeholder: NUL + index + NUL. */
// eslint-disable-next-line no-control-regex -- NUL is the deliberate mask sentinel (cannot appear in source markdown)
const MASK_RE = /\u0000(\d+)\u0000/g
/** A half-open `[start, end)` range of the source. */
type Region = [number, number]
/**
* The length of the fence run at the start of `line` — its leading `\`` or `~`
* characters. Zero when the line is not a fence (or not one of ours).
*/
function fenceRun(line: string): { marker: string; length: number } | null {
const marker = line.charAt(0)
if (marker !== '`' && marker !== '~') return null
let length = 0
while (line.charAt(length) === marker) length += 1
return length >= 3 ? { marker, length } : null
}
/** 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\r]*$/.test(line.slice(run))
}
/**
* Every fenced block in `text`, as half-open ranges that INCLUDE the fence
* lines.
*
* The scan is a single forward walk that cannot be fooled by a fence-looking
* line inside a block or before the opener: a closing fence has to be the same
* character at least as long as its opener, and a fence nested inside a longer
* one is content, not a delimiter. An unclosed fence runs to the end of the
* text — the extent a renderer gives it.
*/
function fenceRegions(text: string): Region[] {
const regions: Region[] = []
let searchFrom = 0
for (;;) {
FENCE_RE.lastIndex = searchFrom
const opener = FENCE_RE.exec(text)
if (opener === null) break
const start = opener.index
const run = fenceRun(opener.at(1) ?? '')
const bodyStart = start + opener[0].length
let end = text.length
if (run !== null) {
// Walk the body line by line until a closing fence (or the end of text).
let at = bodyStart
while (at < text.length) {
if (text.charAt(at) === '\n') { at += 1; continue }
const lineEnd = text.indexOf('\n', at)
const stop = lineEnd === -1 ? text.length : lineEnd
if (closesFence(text.slice(at, stop), run.marker, run.length)) {
end = stop
break
}
at = stop + 1
}
}
regions.push([start, end])
searchFrom = end
}
return regions
}
/** The masked text, plus the way back to the original. */
export interface MaskedCodeRegions {
/** `text` with every code region replaced by a placeholder. */
masked: string
/** Put every masked region back, in place. */
restore: (text: string) => string
}
/**
* Mask the fenced blocks and inline code spans of one markdown text.
* @param text - the markdown source.
* @returns the masked text and its restore function.
*/
export function maskCodeRegions(text: string): MaskedCodeRegions {
const masks: string[] = []
const mask = (region: string): string => {
masks.push(region)
return `\u0000${masks.length - 1}\u0000`
}
// Fences first (they are the outer region), and from the END backwards so
// each splice leaves the offsets of the not-yet-masked regions valid.
let fenced = text
for (const [start, end] of fenceRegions(text).reverse()) {
fenced = fenced.slice(0, start) + mask(text.slice(start, end)) + fenced.slice(end)
}
// Then the inline spans the fence pass did not swallow.
const masked = fenced.replace(CODE_SPAN_RE, (span: string) => mask(span))
return {
masked,
restore: (value: string): string => value.replace(MASK_RE, (_m, index: string) => masks[Number(index)] ?? ''),
}
}