Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(markdown): render CJK punctuation emphasis

Linuxdazhao9fef3ba4da9a
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 49d735575..c63de5e3b 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -70,6 +70,7 @@
"rehype-raw": "^7.0.0",
"rehype-sanitize": "^6.0.0",
"remark-gfm": "^4.0.1",
+ "remark-cjk-friendly": "^2.3.4",
"remark-math": "^6.0.0",
"remark-parse": "^11.0.0",
"shiki": "^4.4.3",
diff --git a/apps/desktop/src/lib/markdown-blocks.ts b/apps/desktop/src/lib/markdown-blocks.ts
index 59493c7cd..f182e92ce 100644
--- a/apps/desktop/src/lib/markdown-blocks.ts
+++ b/apps/desktop/src/lib/markdown-blocks.ts
@@ -1,6 +1,7 @@
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
+import remarkCjkFriendly from "remark-cjk-friendly/parseOnly";
import remarkMath from "remark-math";
import { beginRenderDiagnostic } from "./render-diagnostics.ts";
@@ -30,7 +31,7 @@ import { beginRenderDiagnostic } from "./render-diagnostics.ts";
// renderer's is the defect this module exists to remove. Throttling streaming
// renders, or reading boundaries from micromark's own events, would cut the
// cost without buying it back.
-export const markdownRemarkPlugins = [remarkGfm, remarkMath];
+export const markdownRemarkPlugins = [remarkGfm, remarkCjkFriendly, remarkMath];
const parser = unified().use(remarkParse).use(markdownRemarkPlugins);
export type MarkdownBlockSplit = {
diff --git a/apps/desktop/test/markdown-cjk-emphasis.test.mjs b/apps/desktop/test/markdown-cjk-emphasis.test.mjs
new file mode 100644
index 000000000..b894dfadb
--- /dev/null
+++ b/apps/desktop/test/markdown-cjk-emphasis.test.mjs
@@ -0,0 +1,38 @@
+import { register } from "node:module";
+import assert from "node:assert/strict";
+import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import ReactMarkdown from "react-markdown";
+import { unified } from "unified";
+import remarkParse from "remark-parse";
+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
+const { advanceMarkdownBlocks, emptyMarkdownBlockCache, markdownRemarkPlugins } = await import("../src/lib/markdown-blocks.ts");
+
+const render = source => renderToStaticMarkup(createElement(ReactMarkdown, { remarkPlugins: markdownRemarkPlugins, children: source }));
+
+test("CJK emphasis preserves source offsets and streaming grammar at every prefix", () => {
+ const phrase = '**“重点内容”**';
+ const source = `😀这是${phrase},请查看。\n\n后续**重点。**继续。`;
+ assert.match(render(source), /<strong>“重点内容”<\/strong>/);
+ assert.match(render(source), /<strong>重点。<\/strong>/);
+ const root = unified().use(remarkParse).use(markdownRemarkPlugins).parse(source);
+ const strong = root.children[0].children.find(node => node.type === "strong");
+ assert.equal(source.slice(strong.position.start.offset, strong.position.end.offset), phrase);
+ let cache = emptyMarkdownBlockCache;
+ for (let end = 1; end <= source.length; end++) {
+ const prefix = source.slice(0, end);
+ cache = advanceMarkdownBlocks(cache, prefix);
+ assert.equal(cache.blocks.join(""), prefix);
+ assert.equal(cache.blocks.map(render).filter(Boolean).join("\n"), render(prefix), JSON.stringify(prefix));
+ }
+});
+
+test("code, escapes, link destinations and English emphasis keep their grammar", () => {
+ for (const source of ['这是`**“示例”**`。', '```text\n这是**“示例”**。\n```', '这是\\*\\*“示例”\\*\\*。', 'a**"example"**']) {
+ assert.doesNotMatch(render(source), /<strong[ >]/);
+ }
+ const url = 'https://example.com/**“path”**';
+ assert.ok(render(`[link](${url})`).includes(`href="${encodeURI(url)}"`));
+ assert.match(render('**bold** and *italic*'), /<strong>bold<\/strong> and <em>italic<\/em>/);
+});
diff --git a/apps/desktop/test/markdown-math-rendering.test.mjs b/apps/desktop/test/markdown-math-rendering.test.mjs
index aa5a6e17e..509f50b40 100644
--- a/apps/desktop/test/markdown-math-rendering.test.mjs
+++ b/apps/desktop/test/markdown-math-rendering.test.mjs
@@ -48,6 +48,19 @@ test("TeX math and wrapped HTTP links render through the production Markdown pip
const inline = (html) =>
html.includes('class="katex"') && !html.includes("katex-display");
+ for (const streaming of [false, true]) {
+ for (const [source, body] of [
+ ['这是**“重点内容”**,请查看。', '“重点内容”'],
+ ['这是**重点。**后续内容。', '重点。'],
+ ['这是**(重点)**后续内容。', '(重点)'],
+ ]) {
+ const html = render(source, streaming);
+ assert.match(html, new RegExp(`<strong[^>]*>${body}</strong>`));
+ assert.doesNotMatch(html, /\*\*/);
+ assert.ok(html.includes(`data-source-end="${source.length}"`));
+ }
+ }
+
// A nested linked-host destination must be normalized before the
// production rehype sanitizer removes its invalid wrapper href.
const wrappedLink = render(
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 8ee2fa60d..27d5479bd 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -2299,6 +2299,9 @@ Renderer: `apps/desktop/src/components/Markdown.tsx` + `apps/desktop/src/lib/shi
the reader navigates away. The tool block's Copy action still copies the full
result.
- **Plugins**: `remark-gfm` (tables, task lists, strikethrough, autolinks),
+ `remark-cjk-friendly/parseOnly` (CJK emphasis adjacent to quotes, brackets
+ and punctuation, including `这是**“重点”**。`, without rewriting source or
+ changing literal code/escaped markers; shared by splitting and rendering),
`remark-math` + `rehype-katex` (inline `$…$` or `\(…\)`, display `$$…$$`
or `\[…\]`). Raw HTML is
parsed by `rehype-raw` and immediately constrained by the extended
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index c74157719..e3a18b9e1 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -2463,6 +2463,12 @@ identify the platform validation still needed.
forms render with KaTeX, with `\[…\]` using display layout. The renderer does
not start a second animation-frame typewriter loop, raise React error 185, or
reject Vite-inlined KaTeX fonts under CSP.
+- **CJK emphasis regression**: Stream and reload `这是**“重点内容”**,请查看。`,
+ `这是**重点。**后续内容。` and `这是**(重点)**后续内容。`; each marked
+ phrase is bold without visible markers. Inline/fenced code and escaped
+ asterisks remain literal, English emphasis remains intact, and source-copy
+ bytes/selection anchors remain unchanged. React/Vite SSR and every-prefix
+ grammar tests cover this boundary; native E2E remains unexecuted.
- **Specs linked**: `03-runtime/02-agent-runtime.md`,
`04-ux/08-component-spec.md`, `04-ux/09-interaction-patterns.md`,
`05-security/01-security.md`
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d48e62539..a5b87358e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -331,6 +331,9 @@ importers:
rehype-sanitize:
specifier: ^6.0.0
version: 6.0.0
+ remark-cjk-friendly:
+ specifier: ^2.3.4
remark-gfm:
specifier: ^4.0.1
version: 4.0.1([email protected])
@@ -3514,9 +3517,21 @@ packages:
resolution: {integrity: sha512-cctsq2wp5vTsLIcaymblUriiTcZd0CwWtCbLvrOzYCDZoWyMNV8sZ7krj09FSnsiJi3WVsHLM4k6Dq/yaPyCXA==}
+ resolution: {integrity: sha512-1HCuZwTxQX3WfaNFk8PDb/VW8Ulvzn+nHD+W79Q9k5NXUSkGXH9Zv3eMJ4VI7GjZIz4Ff7WbruEFgMUaPmRJ8g==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ '@types/mdast': '*'
+ peerDependenciesMeta:
+ '@types/mdast':
+ optional: true
+
resolution: {integrity: sha512-xj68wMTvGXVOKonmog6LwyJKrYXZPvlwabaryTjLh9LuvovB/KAH+kvi8Gjj+7rJjsFi23nkUxRQv1KqSroMqA==}
+ resolution: {integrity: sha512-dImdB6VZRMRac1SwAW6o1c0ESk/7pFFAMCMuKM+9v7LpTSthMM5nzXpqKw4Mjo66aXVKhNkyrP4bMB9MurPyWg==}
+
resolution: {integrity: sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg==}
@@ -3526,6 +3541,25 @@ packages:
resolution: {integrity: sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg==}
+ resolution: {integrity: sha512-GcbXqTTHOsiZHyF753oIddP/J2eH8j9zpyQPhkof6B2JNxfEJabnQqxbCgzJNuNes0Y2jTNJ3LiYPSXr6eJA8w==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ micromark-util-types: '*'
+ peerDependenciesMeta:
+ micromark-util-types:
+ optional: true
+
+ resolution: {integrity: sha512-/BqZNCkDWg3Hj9zEEcRitZ5uMF3X78qmBFuP17oAjnVDZ0+H1GHiDKMJZDpJsXasCHEn/kzl6QHozUiWgCHtPQ==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ micromark: ^4.0.0
+ micromark-util-types: ^2.0.0
+ peerDependenciesMeta:
+ micromark-util-types:
+ optional: true
+
resolution: {integrity: sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==}
@@ -3980,6 +4014,16 @@ packages:
resolution: {integrity: sha512-CsnhKNsyI8Tub6L4sm5ZFsme4puGfc6pYylvXo1AeqaGbjOYyzNv3qZPwvs0oMJ39eryyeOdmxwUIo94IpEhqg==}
+ resolution: {integrity: sha512-xhBzjXkumeN5tKXXg6k/THSCZCP6AYV6+wSxnCsPTkxHt60bb9Yk0IWhdD87CFaDCiTqd86LKHtVObcJTmgcow==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ '@types/mdast': ^4.0.0
+ unified: ^11.0.0
+ peerDependenciesMeta:
+ '@types/mdast':
+ optional: true
+
resolution: {integrity: sha512-1quofZ2RQ9EWdeN34S79+KExV1764+wCUGop5CPL1WGdD0ocPpu91lzPGbwWMECpEpd42kJGQwzRfyov9j4yNg==}
@@ -7919,6 +7963,18 @@ snapshots:
unist-util-visit: 5.1.0
vfile: 6.0.3
+ dependencies:
+ mdast-util-to-markdown: 2.2.3
+ micromark-extension-cjk-friendly-util: 3.0.1([email protected])
+ micromark-util-character: 2.1.1
+ micromark-util-classify-character: 2.0.1
+ micromark-util-html-tag-name: 2.0.1
+ optionalDependencies:
+ '@types/mdast': 4.0.4
+ transitivePeerDependencies:
+ - micromark-util-types
+
dependencies:
'@types/mdast': 4.0.4
@@ -7931,6 +7987,20 @@ snapshots:
unist-util-visit: 5.1.0
zwitch: 2.0.4
+ dependencies:
+ '@types/mdast': 4.0.4
+ '@types/unist': 3.0.3
+ longest-streak: 3.1.0
+ mdast-util-phrasing: 4.1.0
+ mdast-util-to-string: 4.0.0
+ micromark-util-character: 2.1.1
+ micromark-util-classify-character: 2.0.1
+ micromark-util-decode-string: 2.0.1
+ micromark-util-html-tag-name: 2.0.1
+ unist-util-visit: 5.1.0
+ zwitch: 2.0.4
+
dependencies:
'@types/mdast': 4.0.4
@@ -7978,6 +8048,25 @@ snapshots:
micromark-util-symbol: 2.0.1
micromark-util-types: 2.0.2
+ dependencies:
+ get-east-asian-width: 1.6.0
+ micromark-util-character: 2.1.1
+ micromark-util-symbol: 2.0.1
+ optionalDependencies:
+ micromark-util-types: 2.0.2
+
+ dependencies:
+ devlop: 1.1.0
+ micromark: 4.0.2([email protected])
+ micromark-extension-cjk-friendly-util: 3.0.1([email protected])
+ micromark-util-chunked: 2.0.1
+ micromark-util-resolve-all: 2.0.1
+ micromark-util-symbol: 2.0.1
+ optionalDependencies:
+ micromark-util-types: 2.0.2
+
dependencies:
micromark-util-character: 2.1.1
@@ -8531,6 +8620,17 @@ snapshots:
'@types/hast': 3.0.5
hast-util-sanitize: 5.0.2
+ dependencies:
+ mdast-util-to-markdown-cjk-friendly: 1.0.2(@types/[email protected])([email protected])
+ micromark-extension-cjk-friendly: 2.0.2([email protected])([email protected]([email protected]))
+ unified: 11.0.5
+ optionalDependencies:
+ '@types/mdast': 4.0.4
+ transitivePeerDependencies:
+ - micromark
+ - micromark-util-types
+
dependencies:
'@types/mdast': 4.0.4