From f0a46f212cf10cc48287d4e582685b1e5a1dd189 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 10:56:41 +0000 Subject: [PATCH] fix(plugin-markdown): bold ending in full-width punctuation renders bold before a CJK letter The manual reader renders package docs through MarkdownImpl, whose remark chain ran remark-gfm and remark-github-blockquote-alert only. A closing `**` with full-width punctuation before it and a CJK letter after it is not right-flanking under CommonMark, so the run stayed literal asterisks. Run remark-cjk-friendly/parseOnly first in the chain, the rule the chat surface already runs, and cross-reference the two import sites (neither package depends on the other). Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01WGufzhoBbwh8URjwQHde8v --- .changeset/36946-manual-reader-cjk-bold.md | 15 +++++ pnpm-lock.yaml | 3 + .../plugin-chatbot/src/chatMarkdownPlugins.ts | 5 ++ ui/packages/plugin-markdown/package.json | 1 + .../plugin-markdown/src/MarkdownImpl.tsx | 25 ++++++- .../src/markdown-render.test.tsx | 66 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 .changeset/36946-manual-reader-cjk-bold.md diff --git a/.changeset/36946-manual-reader-cjk-bold.md b/.changeset/36946-manual-reader-cjk-bold.md new file mode 100644 index 00000000000..df745636a67 --- /dev/null +++ b/.changeset/36946-manual-reader-cjk-bold.md @@ -0,0 +1,15 @@ +--- +'@object-ui/plugin-markdown': patch +'@object-ui/plugin-chatbot': patch +--- + +fix(plugin-markdown): bold that ends in full-width punctuation renders bold before a CJK letter + +Clause-②: no +Metadata-protocol: none + +The markdown renderer the console's manual reader uses (`@object-ui/plugin-markdown`) left a `**bold**` run as literal asterisks when the run ended in full-width punctuation and a CJK letter followed it, as in `**解决耗时(小时)**会被清空`, `**每个项目一张报销单。**每个项目` or `**它跳过和拒绝什么:**已经`. CommonMark counts full-width punctuation as punctuation, so such a closer is not right-flanking; a Chinese or Japanese sentence puts no space there. + +The renderer now runs `remark-cjk-friendly` (its `parseOnly` entry, a new MIT dependency at `^2.3.4`) ahead of remark-gfm, the rule the chat surface already runs. It reads a CJK character beside a `**` run the way it reads the whitespace an English sentence would have there. ASCII emphasis, code spans, backslash escapes, GFM tables and GitHub-style alerts render as before, and the output still passes the same sanitize schema. + +`@object-ui/plugin-chatbot` changes a comment only: its CJK rule now names the renderer's copy, since neither package depends on the other and each imports the plugin itself. No props or exports change in either package; nothing to migrate. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3eeb547a2db..a1e46f9c9cc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5840,6 +5840,9 @@ importers: rehype-slug: specifier: ^6.0.0 version: 6.0.0 + remark-cjk-friendly: + specifier: ^2.3.4 + version: 2.3.4(@types/mdast@4.0.4)(micromark-util-types@2.0.2)(micromark@4.0.2)(unified@11.0.5) remark-gfm: specifier: ^4.0.1 version: 4.0.1 diff --git a/ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts b/ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts index a3da6f4f8cd..f82b67d8741 100644 --- a/ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts +++ b/ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts @@ -26,6 +26,11 @@ import type { CjkPlugin, PluginConfig } from "streamdown"; * the emphasis tokenizer; ASCII emphasis, code spans and backslash escapes * keep their CommonMark meaning. The `parseOnly` entry leaves out the * markdown serializer half, which the chat never runs. + * + * The manual reader carries the same rule in `@object-ui/plugin-markdown` + * (`src/MarkdownImpl.tsx`, its react-markdown `remarkPlugins`). Neither + * package depends on the other, so each imports the plugin itself; change the + * two together. */ const CJK_EMPHASIS: CjkPlugin = { name: "cjk", diff --git a/ui/packages/plugin-markdown/package.json b/ui/packages/plugin-markdown/package.json index 1a36dd3879a..1650e7dd172 100644 --- a/ui/packages/plugin-markdown/package.json +++ b/ui/packages/plugin-markdown/package.json @@ -46,6 +46,7 @@ "rehype-highlight": "^7.0.2", "rehype-sanitize": "^6.0.0", "rehype-slug": "^6.0.0", + "remark-cjk-friendly": "^2.3.4", "remark-gfm": "^4.0.1", "remark-github-blockquote-alert": "^2.1.0" }, diff --git a/ui/packages/plugin-markdown/src/MarkdownImpl.tsx b/ui/packages/plugin-markdown/src/MarkdownImpl.tsx index 6cfc99605bf..2e6b55acc36 100644 --- a/ui/packages/plugin-markdown/src/MarkdownImpl.tsx +++ b/ui/packages/plugin-markdown/src/MarkdownImpl.tsx @@ -8,6 +8,7 @@ import * as React from "react" import ReactMarkdown, { type Components } from "react-markdown" +import remarkCjkFriendly from "remark-cjk-friendly/parseOnly" import remarkGfm from "remark-gfm" import { remarkAlert } from "remark-github-blockquote-alert" import rehypeSanitize, { defaultSchema } from "rehype-sanitize" @@ -95,7 +96,29 @@ function rehypeNormalizeClassName() { return (tree: unknown) => visit(tree as { children?: unknown[] }) } -const remarkPlugins = [remarkGfm, remarkAlert] +// ─── Emphasis next to CJK full-width punctuation ──────────────────────── +// +// CommonMark decides whether a `**` run can open or close emphasis from the +// characters on either side, and counts full-width punctuation ()。:,、 +// 《》「」) as punctuation. A Chinese or Japanese sentence puts no space beside +// the run, so `**解决耗时(小时)**会被清空` (punctuation before the closer, a +// CJK letter after it) is not right-flanking and stays literal asterisks. +// +// `remark-cjk-friendly` replaces the attention tokenizer with the +// markdown-cjk-friendly rule, which reads a CJK character beside the run the +// way it reads the whitespace an English sentence would have there. It is +// listed first, ahead of remark-gfm, so it owns the emphasis tokenizer; ASCII +// emphasis, code spans and backslash escapes keep their CommonMark meaning. +// It changes how `*`/`_` runs tokenize and nothing else, so its output is the +// same `strong`/`emphasis` nodes rehype-sanitize already allows. The +// `parseOnly` entry leaves out the markdown serializer half, which this +// renderer never runs. +// +// The chat surface carries the same rule in +// `@object-ui/plugin-chatbot` (`src/chatMarkdownPlugins.ts`, a streamdown +// PluginConfig). Neither package depends on the other, so each imports the +// plugin itself; change the two together. +const remarkPlugins = [remarkCjkFriendly, remarkGfm, remarkAlert] const rehypePlugins = [ rehypeSlug, rehypeNormalizeClassName, diff --git a/ui/packages/plugin-markdown/src/markdown-render.test.tsx b/ui/packages/plugin-markdown/src/markdown-render.test.tsx index 73a0a21cb8b..e1392c7a69e 100644 --- a/ui/packages/plugin-markdown/src/markdown-render.test.tsx +++ b/ui/packages/plugin-markdown/src/markdown-render.test.tsx @@ -96,3 +96,69 @@ describe('mermaid diagrams (ADR-0046)', () => { expect(html).toContain('hljs'); }); }); + +/** + * Bold that ends in full-width punctuation, followed directly by a CJK letter. + * + * CommonMark counts full-width punctuation ()。:) as punctuation, so a + * closing `**` with that punctuation before it and a CJK letter after it is + * not right-flanking, and the run stays literal asterisks. A Chinese or + * Japanese sentence puts no space there. The manual reader renders package + * docs through this pipeline, so the sources below are lines from shipped + * zh-CN manuals. The chat surface carries the same rule + * (`plugin-chatbot/src/chatMarkdownPlugins.ts`). + */ +describe('bold next to CJK full-width punctuation', () => { + /** Every element tag name in the rendered markup, deduplicated. */ + const tagNames = (html: string): string[] => + [...new Set([...html.matchAll(/<([a-z][a-z0-9]*)\b/g)].map((m) => m[1]))].sort(); + + it.each([ + ['a closer after a full-width parenthesis, before a CJK letter', '**关闭日期**和**解决耗时(小时)**会被清空', ['关闭日期', '解决耗时(小时)']], + ['a closer after a full-width full stop, before a CJK letter', '- **每个项目一张报销单。**每个项目的费用', ['每个项目一张报销单。']], + ['a closer after a full-width colon, before a CJK letter', '**它跳过和拒绝什么:**已经在活动中的人', ['它跳过和拒绝什么:']], + ['a closer after a full-width parenthesis holding a percent sign', '**投资回报率(%)**是(收入 − 成本)', ['投资回报率(%)']], + ])('%s renders strong', (_label, md, bold) => { + const html = render(md); + for (const text of bold) expect(html).toContain(`${text}`); + expect(html).not.toContain('**'); + }); + + it('adds no element the sanitizer did not already allow', () => { + const html = render('**解决耗时(小时)**会被清空'); + expect(tagNames(html)).toEqual(['div', 'p', 'strong']); + }); + + it('keeps raw HTML and javascript: links stripped inside a CJK bold run', () => { + const html = render('**(小时)**会被清空,**[链接](javascript:alert(1))。**然后'); + expect(html).not.toContain('onerror'); + expect(html).not.toContain('javascript:'); + expect(html).not.toContain('**'); + }); + + it.each([ + ['plain English bold', 'a **bold** word', 'bold'], + ['intraword ASCII bold', 'foo**bar**baz', 'foobarbaz'], + ])('leaves %s as CommonMark has it', (_label, md, expected) => { + expect(render(md)).toContain(expected); + }); + + it.each([ + ['a closer after whitespace', '**not bold **here', '**not bold **here'], + ['a code span', '`**x**`', '**x**'], + ['backslash escapes', '\\*\\*x\\*\\*', '**x**'], + ])('keeps %s literal, as CommonMark has it', (_label, md, expected) => { + const html = render(md); + expect(html).toContain(expected); + expect(html).not.toContain(''); + }); + + it('still renders a CJK bold run inside an alert and a GFM table', () => { + const alert = render('> [!NOTE]\n> **解决耗时(小时)**会被清空\n'); + expect(alert).toMatch(/class="[^"]*markdown-alert[^"]*markdown-alert-note/); + expect(alert).toContain('解决耗时(小时)'); + const table = render('| 字段 | 说明 |\n|---|---|\n| 耗时 | **解决耗时(小时)**会被清空 |\n'); + expect(table).toContain('解决耗时(小时)'); + }); +});