Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/36946-manual-reader-cjk-bold.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions ui/packages/plugin-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
25 changes: 24 additions & 1 deletion ui/packages/plugin-markdown/src/MarkdownImpl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
66 changes: 66 additions & 0 deletions ui/packages/plugin-markdown/src/markdown-render.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<strong>${text}</strong>`);
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('**<img src=x onerror="alert(1)">(小时)**会被清空,**[链接](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', '<strong>bold</strong>'],
['intraword ASCII bold', 'foo**bar**baz', 'foo<strong>bar</strong>baz'],
])('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**`', '<code>**x**</code>'],
['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('<strong>');
});

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('<strong>解决耗时(小时)</strong>');
const table = render('| 字段 | 说明 |\n|---|---|\n| 耗时 | **解决耗时(小时)**会被清空 |\n');
expect(table).toContain('<table');
expect(table).toContain('<strong>解决耗时(小时)</strong>');
});
});
Loading