` 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('')
expect(html).not.toContain('安装')
expect(html).toContain('
')
})
it('splitHtmlBlocks: a minimal HTML block + blank line + markdown document', () => {
const lf = '# a\n\n
x
\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 `
` 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 = [
'',
'',
'![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('')
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' },
])
})
})