fix(web): close CJK emphasis runs that end in punctuation (#2358)

CommonMark leaves a closing ** unpaired when the delimiter run's inner
neighbor is punctuation and its outer neighbor is a CJK letter, so text
like '**속도 제한(시속 30km)**은' renders the markers literally
(commonmark/commonmark-spec#650, open since 2020). The web renders
AI-generated CJK content as-is, so it cannot rephrase around the rule.

Add remark-cjk-friendly 2.x (parseOnly entry) to every shared web
markdown plugin array. It is a parse-time micromark extension: emphasis
resolution changes only for runs adjacent to CJK text, and non-CJK
output is unchanged (regression tests pin Latin and intraword cases).
CJK handling for GFM ~~strikethrough~~ needs the separate
remark-cjk-friendly-gfm-strikethrough package and is out of scope.
This commit is contained in:
Junmo Kim
2026-11-10 14:55:13 +08:00
committed by GitHub
parent c378eb1d81
commit d667c00a59
4 changed files with 95 additions and 7 deletions
@@ -6,6 +6,8 @@ import { toHtml } from 'hast-util-to-html'
import remarkBreaks from 'remark-breaks'
import remarkNonHttpsAutolink from '@/lib/remark-non-https-autolink'
import remarkStripCjkAutolink from '@/lib/remark-strip-cjk-autolink'
import remarkRepairTables from '@/lib/remark-repair-tables'
import remarkCjkFriendly from 'remark-cjk-friendly/parseOnly'
import {
MARKDOWN_PLUGINS,
MARKDOWN_PLUGINS_STANDALONE,
@@ -31,6 +33,69 @@ describe('MARKDOWN_PLUGINS integration', () => {
expect(MARKDOWN_PLUGINS).not.toContain(remarkBreaks)
expect(MARKDOWN_PLUGINS_WITH_BREAKS).toContain(remarkBreaks)
})
it('places remarkCjkFriendly after remarkRepairTables, keeping remarkRepairTables immediately after remarkGfm', () => {
const idxRepair = MARKDOWN_PLUGINS.indexOf(remarkRepairTables)
const idxCjk = MARKDOWN_PLUGINS.indexOf(remarkCjkFriendly)
expect(idxRepair).toBeGreaterThan(0) // not first (remarkGfm slot is first)
expect(idxCjk).toBeGreaterThan(idxRepair)
})
})
describe('MARKDOWN_PLUGINS — CJK emphasis flanking', () => {
// CommonMark fails to close `**` when the closing delimiter run's inner
// neighbor is punctuation and its outer neighbor is a CJK letter
// (commonmark/commonmark-spec#650). remark-cjk-friendly repairs exactly
// that class at parse time. All sentences below are synthetic.
const pluginVariants = [
['default', MARKDOWN_PLUGINS],
['standalone', MARKDOWN_PLUGINS_STANDALONE],
['with breaks', MARKDOWN_PLUGINS_WITH_BREAKS],
['standalone with breaks', MARKDOWN_PLUGINS_STANDALONE_WITH_BREAKS],
] as const
it.each(pluginVariants)(
'closes emphasis runs before CJK particles that directly follow punctuation (%s)',
(_, plugins) => {
const cases = [
// Korean particle after a closing parenthesis
['**속도 제한(시속 30km)**은 지켜야 합니다', '<strong>속도 제한(시속 30km)</strong>은 지켜야 합니다'],
// Korean particle after a closing double quote
['**"먼저 확인하세요"**를 누르세요', '<strong>"먼저 확인하세요"</strong>를 누르세요'],
// Korean particle after a closing single quote
["**'조용히 진행하세요'**해 주세요", "<strong>'조용히 진행하세요'</strong>해 주세요"],
// Korean particle right after sentence-ending punctuation
['**먼저 저장하세요.**그 다음에 실행하세요', '<strong>먼저 저장하세요.</strong>그 다음에 실행하세요'],
// Chinese particle after a full-width quote
['**“示例”**的说明如下', '<strong>“示例”</strong>的说明如下'],
// Japanese particle after a corner bracket
['**「注意事項」**を読んでください', '<strong>「注意事項」</strong>を読んでください'],
] as const
for (const [markdown, expectedHtml] of cases) {
expect(render(markdown, plugins)).toContain(expectedHtml)
}
}
)
it('keeps already-working space-separated CJK emphasis unchanged', () => {
expect(render('**속도 제한(시속 30km)** 을 지키세요')).toContain(
'<strong>속도 제한(시속 30km)</strong> 을 지키세요'
)
})
})
describe('MARKDOWN_PLUGINS — Latin emphasis regressions', () => {
// remark-cjk-friendly only alters runs adjacent to CJK text; Latin and
// intraword emphasis must render exactly as before the plugin.
it.each([
['plain **english emphasis** text', '<p>plain <strong>english emphasis</strong> text</p>'],
['foo**bar**baz', '<p>foo<strong>bar</strong>baz</p>'],
['**a**b', '<p><strong>a</strong>b</p>'],
['snake_case_name', '<p>snake_case_name</p>'],
['2*3*4', '<p>2<em>3</em>4</p>'],
])('renders %s unchanged', (markdown, expectedHtml) => {
expect(render(markdown)).toBe(expectedHtml)
})
})
function render(markdown: string, plugins: PluggableList = MARKDOWN_PLUGINS): string {
@@ -10,6 +10,7 @@ import {
} from '@assistant-ui/react-markdown'
import remarkGfm from 'remark-gfm'
import remarkBreaks from 'remark-breaks'
import remarkCjkFriendly from 'remark-cjk-friendly/parseOnly'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'
import remarkDisableIndentedCode from '@/lib/remark-disable-indented-code'
@@ -63,9 +64,15 @@ export const MarkdownFileBaseContext = createContext<{
} | null>(null)
// ── Plugin array ────────────────────────────────────────────────────────────
// Order: remarkGfm → remarkRepairTables → remarkLatexBracketMath → remarkNonHttpsAutolink → remarkStripCjkAutolink → remarkMath → remarkDisableIndentedCode → remarkSessionPathLinks → remarkFilePathLinks
// Order: remarkGfm → remarkRepairTables → remarkCjkFriendly → remarkLatexBracketMath → remarkNonHttpsAutolink → remarkStripCjkAutolink → remarkMath → remarkDisableIndentedCode → remarkSessionPathLinks → remarkFilePathLinks
// remarkRepairTables must run immediately after remarkGfm — it reads file.value
// (raw source) to pad short separator rows before remark-gfm parses the table.
// remarkCjkFriendly is a parse-time micromark extension
// (commonmark/commonmark-spec#650):
// it lets `**` close before CJK particles that directly follow punctuation
// (e.g. `**...(...)**은`). It stays after remarkRepairTables so the documented
// remarkGfm → remarkRepairTables adjacency is preserved; position in the array
// does not affect parsing, which happens once before any transformer runs.
// remarkNonHttpsAutolink must run BEFORE remarkStripCjkAutolink so that the
// CJK strip plugin sees the new link nodes and can trim trailing CJK punctuation
// from them. Both must come before remarkMath (to avoid treating TeX as URI).
@@ -112,6 +119,7 @@ const REMARK_GFM_PLUGIN = [
export const MARKDOWN_PLUGINS = [
REMARK_GFM_PLUGIN,
remarkRepairTables,
remarkCjkFriendly,
remarkLatexBracketMath,
...MARKDOWN_PLUGIN_TAIL,
] satisfies NonNullable<MarkdownTextPrimitiveProps['remarkPlugins']>
@@ -119,6 +127,7 @@ export const MARKDOWN_PLUGINS = [
export const MARKDOWN_PLUGINS_STANDALONE = [
REMARK_GFM_PLUGIN,
remarkRepairTables,
remarkCjkFriendly,
remarkLatexBracketMath,
...MARKDOWN_PLUGIN_TAIL_STANDALONE,
] satisfies NonNullable<MarkdownTextPrimitiveProps['remarkPlugins']>
@@ -128,6 +137,7 @@ export const MARKDOWN_PLUGINS_STANDALONE = [
export const MARKDOWN_PLUGINS_WITH_BREAKS = [
REMARK_GFM_PLUGIN,
remarkRepairTables,
remarkCjkFriendly,
remarkLatexBracketMath,
remarkBreaks,
...MARKDOWN_PLUGIN_TAIL,
@@ -136,6 +146,7 @@ export const MARKDOWN_PLUGINS_WITH_BREAKS = [
export const MARKDOWN_PLUGINS_STANDALONE_WITH_BREAKS = [
REMARK_GFM_PLUGIN,
remarkRepairTables,
remarkCjkFriendly,
remarkLatexBracketMath,
remarkBreaks,
...MARKDOWN_PLUGIN_TAIL_STANDALONE,