Files
apes-sidebar/tests/markdown-crlf.spec.ts
T
Peng Li 33f35c79f0 fix(editor): resolve markdown preview images on the page base so the desktop shell renders them
The desktop shell serves the GUI from the dsh-app://app/ origin while the
Host listens on another site (__DSH_TRANSPORT__.streamBaseUrl). Markdown
preview images were rewritten to an absolute /sidebar/file URL on that
TRANSPORT origin, so from the shell page the request was cross-site: the
Connection fence rejected it (403, Sec-Fetch-Site + a SameSite=Strict
session cookie the shell never forwards for plain http subresources) and
MarkdownText fell back to the image's alt text. Disabling the plugin
worked because the built-in document preview resolves images against the
page base.

Resolve local markdown images through the renderer's pathImages vocabulary
on the PAGE base instead of pre-rewriting destinations: in a browser the
plugin media route stays byte-for-byte what it was, and in the desktop
shell the vocabulary maps to the Host's authenticated api/file route
(dsh-app://app/api/file?...), the only local-file shape the renderer
accepts from that page and the same shape the built-in document preview
uses. Images stay plain <img> elements, so the preview keeps the size the
browser/built-in previews have (no chat thumbnail cap or lightbox chrome).

The host file route requires an ABSOLUTE path (a relative one answers
400 "absolute path required"), while an editor tab opened from a chat
address can carry a workspace-relative path (the plugin's own media route
resolved that via its cwd parameter). The desktop branch therefore joins
the session workspace root before building the URL, and falls back to alt
text when no root is known instead of requesting a guaranteed 400.

Raw HTML <img src> leaves keep the direct rewrite, now also page-based.
Image references stay as authored, so the link pass leaves an
image-referenced definition alone (a link-only definition is still pinned
inert).

Regression coverage renders the real MarkdownText in jsdom with the
desktop page origin (markdown images must render as
dsh-app://app/api/file?..., raw-HTML images as dsh-app://app/sidebar/file...,
not alt text) and pins the relative-tab-path → absolute-target rule.
The http-page assertions are unchanged.

Design notes: docs/plans/2026-10-23-desktop-markdown-media-base.md
2026-10-23 17:03:58 +08:00

184 lines
8.5 KiB
TypeScript

/**
* Line endings are not content: every line-based scanner in the markdown
* pipeline must read a CRLF document exactly like the same document with LF
* endings.
*
* WHY THIS FILE EXISTS: the pipeline's scanners split a document on `\n` and
* then reason about each line (`isBlank`, a closed-ATX heading, a closing
* fence). A Windows checkout — `core.autocrlf=true`, which is both the
* `ci-windows` lane and every user who clones on Windows — hands them `\r\n`,
* and a `\r` left at the end of a line is invisible in the rendered output but
* not to those predicates. Measured on this repo's own README before the fix:
* the HTML-run splitter produced 4 segments instead of 36 (an HTML run stayed
* open across the blank line that should have ended it and swallowed the
* markdown after it), `splitMermaidBlocks` never closed a fence, and
* `maskCodeRegions` masked everything from the first fence to the end of the
* file, so the link rewriter stopped claiming every `.md` link after it. The
* same document with LF endings was correct in all three.
*
* The invariant pinned here is that whole contract, scanner by scanner: the
* CRLF spelling's output must equal the LF spelling's output once `\r` is
* ignored. These cases are DISCRIMINATING by construction — reverting any of
* the three `\r` fixes in `src/client` turns them red (the CRLF half diverges
* from the LF half), which is exactly how the `ci-windows` failures got here:
* only the CRLF half of each pair was ever exercised there.
*/
import { describe, expect, it } from 'vitest'
import {
analyzeMarkdownHtml,
splitHtmlBlocks,
type AnalyzedMarkdownHtml,
type MdHtmlSegment,
} from '../src/client/markdown-html.ts'
import { splitMermaidBlocks, type MdBlock } from '../src/client/mermaid-blocks.ts'
import { maskCodeRegions } from '../src/client/markdown-code.ts'
import { rewriteLocalMarkdownLinks } from '../src/client/markdown-navigation.ts'
import type { SessionScope } from '../src/client/api.ts'
/** One document, both spellings — every case below compares the two. */
const LF = [
'# 标题',
'',
'<!-- hero -->',
'',
'<div align="center">',
' <img alt="badge" src="https://img.shields.io/badge/x-y-blue" />',
'</div>',
'',
'## 🚀 安装',
'',
'```sh',
'dsh plugin --profile web add dsh-better-sidebar',
'```',
'',
'[other](./other.md#安装)',
'',
'~~~text',
'[demonstration](./demonstration.md#标题)',
'~~~',
'',
'![image](./pic.png)',
'',
].join('\n')
const CRLF = LF.replace(/\n/g, '\r\n')
/** `\r` is a line terminator, so dropping it reduces one spelling to the other. */
const stripCr = (value: string): string => value.replace(/\r/g, '')
/** The CRLF spelling of a split document, reduced to the LF one. */
function segmentsLf(segments: readonly MdHtmlSegment[]): MdHtmlSegment[] {
return segments.map(segment => ({ kind: segment.kind, text: stripCr(segment.text) }))
}
/** The CRLF spelling of a whole-document analysis, reduced to the LF one. */
function analysisLf(info: AnalyzedMarkdownHtml): AnalyzedMarkdownHtml {
return {
...info,
segments: segmentsLf(info.segments),
referenceDefinitions: stripCr(info.referenceDefinitions),
}
}
/** The CRLF spelling of a mermaid split, reduced to the LF one. */
function blocksLf(blocks: readonly MdBlock[]): MdBlock[] {
return blocks.map(block => block.kind === 'mermaid'
? { kind: 'mermaid' as const, code: stripCr(block.code) }
: { kind: 'markdown' as const, text: stripCr(block.text) })
}
const SCOPE: SessionScope = { sessionId: 's1', cwd: '/p' }
const DOC = '/p/docs/README.md'
describe('the markdown pipeline reads CRLF and LF documents identically', () => {
it('splitHtmlBlocks: an HTML run ends at a blank line in both spellings', () => {
// The regression: `isBlank` was `/^[ \t]*$/`, so a CRLF blank line (`'\r'`)
// was content. The `<div>` run stayed open and swallowed `## 🚀 安装` and
// everything after it as raw HTML, i.e. the preview rendered markdown as
// text.
expect(segmentsLf(splitHtmlBlocks(CRLF))).toEqual(splitHtmlBlocks(LF))
// Guard the guard: the LF half really does carry the markdown after the
// HTML run as MARKDOWN (a splitter that returned one HTML segment for the
// whole document would satisfy the equality above for the wrong reason).
const segments = splitHtmlBlocks(LF)
const markdown = segments.filter(segment => segment.kind === 'markdown').map(segment => segment.text).join('\n')
const html = segments.filter(segment => segment.kind === 'html').map(segment => segment.text).join('\n')
expect(markdown).toContain('## 🚀 安装')
expect(markdown).toContain('![image](./pic.png)')
expect(html).not.toContain('安装')
expect(html).toContain('<div align="center">')
})
it('splitHtmlBlocks: a minimal HTML block + blank line + markdown document', () => {
const lf = '# a\n\n<div>x</div>\n\n# b\n'
expect(segmentsLf(splitHtmlBlocks(lf.replace(/\n/g, '\r\n')))).toEqual(splitHtmlBlocks(lf))
// `# b` is a markdown heading, not part of the `<div>` run.
expect(splitHtmlBlocks(lf).map(segment => segment.kind)).toEqual(['markdown', 'html', 'markdown'])
})
it('analyzeMarkdownHtml: same segmentation, gates and reference definitions', () => {
const lf = `${LF}\n[ref]: ./target.md#锚点\n`
expect(analysisLf(analyzeMarkdownHtml(lf.replace(/\n/g, '\r\n')))).toEqual(analyzeMarkdownHtml(lf))
})
it('maskCodeRegions: a CRLF fence closes where its LF twin does', () => {
// The regression: `closesFence` tested the fence tail with `/^[ \t]*$/`,
// which a `\r` fails — no fence ever closed, so the masked region ran to
// the end of the file and every later pass skipped a masked document.
expect(stripCr(maskCodeRegions(CRLF).masked)).toEqual(maskCodeRegions(LF).masked)
// The mask really does stop at the closing fence: the text right after it
// survives unmasked in both spellings.
expect(maskCodeRegions(LF).masked).toContain('[other](./other.md#安装)')
expect(stripCr(maskCodeRegions(CRLF).masked)).toContain('[other](./other.md#安装)')
})
it('rewriteLocalMarkdownLinks: a `.md` link after a fence is still claimed', () => {
// The regression's user-visible half: on CRLF the destination kept its own
// `#`, the host parser refused it, and the link rendered as inert text.
const rewrittenLf = rewriteLocalMarkdownLinks(LF, DOC, SCOPE.cwd)
const rewrittenCrlf = rewriteLocalMarkdownLinks(CRLF, DOC, SCOPE.cwd)
expect(stripCr(rewrittenCrlf)).toEqual(rewrittenLf)
// Claimed: the heading fragment is carried past the host parser as %23.
expect(rewrittenLf).toContain('[other](./other.md%23安装)')
expect(rewrittenCrlf).toContain('[other](./other.md%23安装)')
// Not claimed: the link demonstrated inside the `~~~` fence is untouched
// (the fence is masked, in both spellings), and a non-markdown local
// destination stays inert.
expect(rewrittenLf).toContain('[demonstration](./demonstration.md#标题)')
expect(rewrittenCrlf).toContain('[demonstration](./demonstration.md#标题)')
})
it('leaves image destinations (and image-referenced definitions) to the renderer, CRLF included', () => {
// Images are resolved by the owner's `pathImages` vocabulary at render
// time, so the source rewrite must hand them over verbatim — including the
// reference definition an image uses — while a definition only links use
// is still pinned inert.
const text = [
'![inline](./pic.png)',
'',
'![logo][logo]',
'',
'see [docs][d]',
'',
'[logo]: ./logo.png',
'',
'[d]: ./notes.txt',
].join('\n')
const rewrite = (source: string): string => rewriteLocalMarkdownLinks(source, DOC, SCOPE.cwd)
expect(stripCr(rewrite(text.replace(/\n/g, '\r\n')))).toEqual(rewrite(text))
expect(rewrite(text)).toContain('![inline](./pic.png)')
expect(rewrite(text)).toContain('[logo]: ./logo.png')
expect(rewrite(text)).toMatch(/\[d\]: \.\/notes\.txt\?/)
})
it('splitMermaidBlocks: a CRLF mermaid fence closes instead of swallowing the file', () => {
const lf = '# t\n\n```mermaid\ngraph TD;\n A-->B\n```\n\nafter\n'
expect(blocksLf(splitMermaidBlocks(lf.replace(/\n/g, '\r\n')))).toEqual(splitMermaidBlocks(lf))
// The diagram is exactly the fence's body — not the rest of the document.
expect(splitMermaidBlocks(lf.replace(/\n/g, '\r\n'))).toEqual([
{ kind: 'markdown', text: '# t\r\n\r' },
{ kind: 'mermaid', code: 'graph TD;\r\n A-->B\r' },
{ kind: 'markdown', text: '\r\nafter\r\n' },
])
})
})