markdown-code.ts 先用 NUL 占位替换围栏块,再在掩码后的文本上扫行内代码段; 一个行内段可以把围栏占位符包进自己的替换文本里,而 restore 只做一趟 String#replace —— 内层占位符原样留在输出里。NUL 一路穿过 rewriteLocalImageUrls / rewriteLocalMarkdownLinks 进 MarkdownText,被 markdown 解析器当成 U+FFFD,于是整段代码块渲染成 `�21�` 这样的字面段落(实测一份 中文契约文档:§2.3 的三段键清单只剩 `�17�` `�16�` `�15�`,整篇 22 个代码块 只剩 1 个)。 两处修: - restore 循环到不再变化(嵌套深度 ≤ 掩码数),往返恢复字节级不变; - 行内代码段不再跨空行配对(CommonMark 的行内段不能跨段落)。此前一个 落单反引号会与半篇之后的反引号配对,把中间整段内容一起掩掉,图片/链接 重写也会漏掉那一片。 tests/markdown-code.spec.ts 钉住往返与两个判别性用例(回退任一处即红), 并在 0.28.1 基线上跑通 markdown 相关 12 个套件(141 例)与 tsc。
96 lines
4.1 KiB
TypeScript
96 lines
4.1 KiB
TypeScript
/**
|
|
* The code-region mask must round-trip BYTE FOR BYTE, and it must stay
|
|
* paragraph-scoped.
|
|
*
|
|
* WHY THIS FILE EXISTS (2026-11-07, real document): `markdown-code.ts` masks
|
|
* fenced blocks first, then the inline spans still visible in the masked
|
|
* text. Masked placeholders are `NUL + index + NUL`, and `restore` was a
|
|
* SINGLE `String#replace` pass. An inline span can wrap a fence placeholder
|
|
* (a fence line directly inside the surrounding paragraph, or — before the
|
|
* paragraph bound — one stray backtick pairing with a run half a document
|
|
* later), so its replacement text contained another placeholder that the one
|
|
* pass never rescanned. Those NULs then rode through `rewriteLocalImageUrls` /
|
|
* `rewriteLocalMarkdownLinks` into `MarkdownText`, whose markdown parser turns
|
|
* a NUL into U+FFFD: every swallowed code block rendered as a literal
|
|
* `�21�` paragraph. That is exactly what happened to
|
|
* `dsh-perf-hud/docs/pack-schema.md` — §2.3 lost all three key lists to
|
|
* `�17�` `�16�` `�15�`.
|
|
*
|
|
* The two cases below are DISCRIMINATING by construction: reverting either fix
|
|
* turns them red.
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
import { maskCodeRegions } from '../src/client/markdown-code.ts'
|
|
import { rewriteLocalImageUrls } from '../src/client/markdown-images.ts'
|
|
import { rewriteLocalMarkdownLinks } from '../src/client/markdown-navigation.ts'
|
|
import type { SessionScope } from '../src/client/api.ts'
|
|
|
|
const scope: SessionScope = { sessionId: 'abc', cwd: '/repo' }
|
|
const ORIGIN = 'http://gui.origin'
|
|
|
|
/** The one invariant every consumer of the mask relies on. */
|
|
function expectRoundTrip(text: string): void {
|
|
const { masked, restore } = maskCodeRegions(text)
|
|
expect(restore(masked)).toBe(text)
|
|
}
|
|
|
|
describe('maskCodeRegions', () => {
|
|
it('round-trips fences and inline spans', () => {
|
|
expectRoundTrip([
|
|
'# Title',
|
|
'',
|
|
'Use `` in prose.',
|
|
'',
|
|
'```js',
|
|
'const a = `[x](./a.md)`',
|
|
'```',
|
|
'',
|
|
'~~~',
|
|
'`fenced ` inside a tilde block',
|
|
'~~~',
|
|
].join('\n'))
|
|
})
|
|
|
|
it('restores a fence whose placeholder an inline span swallowed', () => {
|
|
// The fence sits directly after a paragraph (a fence interrupts a
|
|
// paragraph in CommonMark), so the two stray backticks around it form ONE
|
|
// inline span once the fence has been replaced by its placeholder. The
|
|
// span's replacement text therefore contains the fence placeholder — the
|
|
// inner one must still come back.
|
|
const text = [
|
|
'para with a stray ` tick',
|
|
'```js',
|
|
'const src = ``',
|
|
'```',
|
|
'and a closing ` tick',
|
|
].join('\n')
|
|
const { masked, restore } = maskCodeRegions(text)
|
|
expect(masked, 'the fence must be masked').toContain('\u0000')
|
|
const restored = restore(masked)
|
|
expect(restored).not.toContain('\u0000')
|
|
expect(restored).toBe(text)
|
|
// No escaped NUL may reach a markdown renderer through either rewriter.
|
|
expect(rewriteLocalImageUrls(text, scope, '/repo/docs/README.md', ORIGIN)).toBe(text)
|
|
expect(rewriteLocalMarkdownLinks(text, '/repo/docs/README.md', '/repo')).toBe(text)
|
|
})
|
|
|
|
it('does not pair backticks across a blank line', () => {
|
|
// A code span is inline: a blank line ends the paragraph, so these two
|
|
// lone backticks are NOT one span. Before the paragraph bound the pair
|
|
// masked the whole region and the image between them was never rewritten.
|
|
const text = 'stray ` tick\n\n\n\nclosing ` tick'
|
|
const out = rewriteLocalImageUrls(text, scope, '/repo/docs/README.md', ORIGIN)
|
|
expect(out).toContain(`${ORIGIN}/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fp.png&cwd=%2Frepo`)
|
|
expectRoundTrip(text)
|
|
})
|
|
|
|
it('still masks a legitimate multi-line code span', () => {
|
|
// A soft line break inside a span is legal CommonMark; documentation that
|
|
// demonstrates image syntax across it must stay verbatim.
|
|
const text = '`\nstill a span`\n'
|
|
const out = rewriteLocalImageUrls(text, scope, '/repo/docs/README.md', ORIGIN)
|
|
expect(out).toBe(text)
|
|
expectRoundTrip(text)
|
|
})
|
|
})
|