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
12 changes: 12 additions & 0 deletions .changeset/33723-chat-cjk-bold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/plugin-chatbot': patch
---

fix(plugin-chatbot): bold next to CJK full-width punctuation renders bold in chat answers

Clause-②: no
Metadata-protocol: none

A `**` run with a CJK letter on one side and full-width punctuation on the other (book-title marks, corner brackets, the full-width comma, full stop, exclamation and question marks, the enumeration comma) showed as literal asterisks in chat answers and the reasoning panel: CommonMark's flanking rule reads full-width punctuation as punctuation, and a CJK sentence puts no space beside the run. Both chat markdown surfaces now render with `remark-cjk-friendly` in streamdown's `cjk` plugin slot, so such a run opens and closes. ASCII emphasis, inline code spans and backslash-escaped asterisks keep their CommonMark meaning. A caller that passes its own `plugins` to `MessageResponse` keeps the CJK rule.

`@object-ui/plugin-chatbot` gains the dependency `remark-cjk-friendly` (MIT).
100 changes: 100 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions ui/packages/plugin-chatbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lucide-react": "^1.43.0",
"motion": "^13.2.0",
"nanoid": "^6.0.1",
"remark-cjk-friendly": "^2.3.4",
"shiki": "^4.4.2",
"streamdown": "^2.5.0",
"use-stick-to-bottom": "^1.1.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ConversationScrollButton,
} from '../elements/conversation';
import { ChatbotEnhanced } from '../ChatbotEnhanced';
import { Reasoning, ReasoningContent } from '../elements/reasoning';
import type { ChatbotEnhancedMessage } from '../ChatbotEnhanced';

/** Geometry the component reads, set per test. */
Expand Down Expand Up @@ -256,3 +257,80 @@ describe('an assistant answer in the side panel (objectui#13032)', () => {
expect(list).toHaveClass('min-h-0', 'flex-1');
});
});

/**
* objectstack#33723 — `**` beside CJK full-width punctuation. CommonMark's
* flanking rule reads full-width punctuation as punctuation, so a run with a
* CJK letter on one side and full-width punctuation on the other never opens
* or closes, and the asterisks show. Every chat markdown surface renders with
* `CHAT_MARKDOWN_PLUGINS` (remark-cjk-friendly in streamdown's `cjk` slot).
*/
describe('bold next to CJK full-width punctuation (objectstack#33723)', () => {
/** The text of every strong run in the rendered answer. */
async function strongRuns(markdown: string): Promise<string[]> {
const { container } = render(<MessageResponse>{markdown}</MessageResponse>);
await waitFor(() => expect(container.querySelector('p, pre')).not.toBeNull());
return Array.from(container.querySelectorAll('[data-streamdown="strong"]')).map((el) => el.textContent ?? '');
}

it.each([
['an opener after a CJK letter, before a book-title mark', '借书人是**《书名》**的作者', '《书名》'],
['an opener after a CJK letter, before a corner bracket', '他说**「引文」**之后', '「引文」'],
['a closer after a full-width comma, before a CJK letter', '他说**你好,**我们走', '你好,'],
['a closer after a full-width full stop, before a CJK letter', '看**这个。**然后', '这个。'],
['a closer after a full-width exclamation mark, before a CJK letter', '真**好!**再来', '好!'],
['a closer after a full-width question mark, before a CJK letter', '是**谁?**不知道', '谁?'],
['a closer after an enumeration comma, before a CJK letter', '有**甲、**乙两种', '甲、'],
['a closer after a closing corner bracket, before a CJK letter', '他说**「好」**然后走', '「好」'],
])('renders %s bold', async (_name, markdown, bold) => {
expect(await strongRuns(markdown)).toEqual([bold]);
});

it.each([
['the card example, opener after a corner bracket', '「**《书名》,借书人**」', '《书名》,借书人'],
['bold ending before a full-width comma', '**借书人**,下一位', '借书人'],
['bold ending before a full-width full stop', '是**借书人**。', '借书人'],
['bold ending before a full-width exclamation mark', '是**借书人**!', '借书人'],
['bold ending before a full-width question mark', '是**借书人**?', '借书人'],
['bold ending before an enumeration comma', '**甲**、乙', '甲'],
['bold ending before a closing corner bracket', '「**开头**」再', '开头'],
['bold starting after an opening corner bracket', '说「**开头**」', '开头'],
])('control: %s stays bold', async (_name, markdown, bold) => {
expect(await strongRuns(markdown)).toEqual([bold]);
});

it.each([
['ASCII emphasis', 'This is **bold** text and foo**bar**baz', ['bold', 'bar'], 'This is bold text and foobarbaz'],
['ASCII punctuation inside the run beside letters (CommonMark: literal)', 'a**"b"**c', [], 'a**"b"**c'],
['escaped asterisks', '\\*\\*《书名》\\*\\*', [], '**《书名》**'],
])('control: %s keeps its CommonMark meaning', async (_name, markdown, bold, text) => {
const { container } = render(<MessageResponse>{markdown}</MessageResponse>);
await waitFor(() => expect(container.querySelector('p')).not.toBeNull());
const runs = Array.from(container.querySelectorAll('[data-streamdown="strong"]')).map((el) => el.textContent);
expect(runs).toEqual(bold);
expect(container.querySelector('p')!.textContent).toBe(text);
});

it('control: `**` inside an inline code span stays literal', async () => {
const { container } = render(<MessageResponse>{'书名是`**《书名》**`的写法'}</MessageResponse>);
await waitFor(() => expect(container.querySelector('code')).not.toBeNull());
expect(container.querySelector('code')!.textContent).toBe('**《书名》**');
expect(container.querySelector('[data-streamdown="strong"]')).toBeNull();
});

it('a caller\'s own plugins keep the CJK rule', async () => {
const { container } = render(<MessageResponse plugins={{}}>{'借书人是**《书名》**的作者'}</MessageResponse>);
await waitFor(() => expect(container.querySelector('p')).not.toBeNull());
expect(container.querySelector('[data-streamdown="strong"]')?.textContent).toBe('《书名》');
});

it('the reasoning panel renders with the same rule', async () => {
const { container } = render(
<Reasoning defaultOpen>
<ReasoningContent>{'借书人是**《书名》**的作者'}</ReasoningContent>
</Reasoning>,
);
await waitFor(() => expect(container.querySelector('p')).not.toBeNull());
expect(container.querySelector('[data-streamdown="strong"]')?.textContent).toBe('《书名》');
});
});
39 changes: 39 additions & 0 deletions ui/packages/plugin-chatbot/src/chatMarkdownPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import remarkCjkFriendly from "remark-cjk-friendly/parseOnly";
import type { CjkPlugin, PluginConfig } from "streamdown";

/**
* 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 (book-title
* marks, corner brackets, the full-width comma and full stop) as punctuation.
* A CJK sentence puts no space beside the run, so a run with a CJK letter on
* one side and full-width punctuation on the other is neither left- nor
* right-flanking the way the writer meant it, 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 runs
* before remark-gfm (streamdown's `cjk.remarkPluginsBefore` slot) so it owns
* 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.
*/
const CJK_EMPHASIS: CjkPlugin = {
name: "cjk",
type: "cjk",
remarkPlugins: [remarkCjkFriendly],
remarkPluginsBefore: [remarkCjkFriendly],
remarkPluginsAfter: [],
};

/** The streamdown plugins every chat markdown surface renders with. */
export const CHAT_MARKDOWN_PLUGINS: PluginConfig = { cjk: CJK_EMPHASIS };
3 changes: 3 additions & 0 deletions ui/packages/plugin-chatbot/src/elements/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import { useSafeTranslate } from "@object-ui/i18n";
import { Streamdown } from "streamdown";
import { ListLinkContext, RecordLinkContext, withRecordLinks } from "../recordLink";
import { CHAT_MARKDOWN_PLUGINS } from "../chatMarkdownPlugins";

export type MessageProps = HTMLAttributes<HTMLDivElement> & {
from: UIMessage["role"];
Expand Down Expand Up @@ -407,6 +408,7 @@ const MessageResponseImpl = ({
translations,
isAnimating,
linkSafety,
plugins,
...props
}: MessageResponseProps) => {
const tt = useSafeTranslate();
Expand Down Expand Up @@ -514,6 +516,7 @@ const MessageResponseImpl = ({
components={{ ...ANSWER_COMPONENTS, ...components }}
isAnimating={isAnimating}
linkSafety={answerLinkSafety}
plugins={{ ...CHAT_MARKDOWN_PLUGINS, ...plugins }}
translations={{
...translations,
viewFullscreen: expandTableLabel,
Expand Down
3 changes: 2 additions & 1 deletion ui/packages/plugin-chatbot/src/elements/reasoning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BrainIcon, ChevronDownIcon } from "lucide-react";
import type { ComponentProps, ReactNode } from "react";
import { createContext, memo, useContext, useEffect, useState } from "react";
import { Streamdown } from "streamdown";
import { CHAT_MARKDOWN_PLUGINS } from "../chatMarkdownPlugins";
import { Shimmer } from "./shimmer";

type ReasoningContextValue = {
Expand Down Expand Up @@ -176,7 +177,7 @@ export const ReasoningContent = memo(
)}
{...props}
>
<Streamdown>{children}</Streamdown>
<Streamdown plugins={CHAT_MARKDOWN_PLUGINS}>{children}</Streamdown>
</CollapsibleContent>
)
);
Expand Down
Loading