diff --git a/frontend/src/utils/chatMarkdownRenderer.test.ts b/frontend/src/utils/chatMarkdownRenderer.test.ts index 28ea9ce1..2356f514 100644 --- a/frontend/src/utils/chatMarkdownRenderer.test.ts +++ b/frontend/src/utils/chatMarkdownRenderer.test.ts @@ -128,6 +128,15 @@ test('repairFlankingEmphasis bolds punctuation-adjacent emphasis CommonMark woul assert.equal(repairFlankingEmphasis('~~删除)~~文字'), '删除)文字') assert.equal(repairFlankingEmphasis('*斜体)*文字'), '斜体)文字') + // Opening delimiter sits between a letter/number and punctuation -> CommonMark + // refuses to open emphasis even though the closer is valid; we bold it too. + assert.equal( + repairFlankingEmphasis('知识库中**《xxx》学程手册**整理'), + '知识库中《xxx》学程手册整理', + ) + assert.equal(repairFlankingEmphasis('在**《书名》**'), '在《书名》') + assert.equal(repairFlankingEmphasis('看到~~《删》文字~~后面'), '看到《删》文字后面') + // Cases marked already handles, literal markers, and code must be untouched. assert.equal(repairFlankingEmphasis('**中文**后面'), '**中文**后面') assert.equal(repairFlankingEmphasis('**中文)** 后面'), '**中文)** 后面') @@ -135,6 +144,10 @@ test('repairFlankingEmphasis bolds punctuation-adjacent emphasis CommonMark woul assert.equal(repairFlankingEmphasis('`**a)**b`'), '`**a)**b`') assert.equal(repairFlankingEmphasis('2 ** 3 ** 4'), '2 ** 3 ** 4') assert.equal(repairFlankingEmphasis('价格 3 * 5 * 7 元'), '价格 3 * 5 * 7 元') + // Exponent / glob markers stay literal: the opener is followed by a number or + // path content, not an emphasis-opening punctuation run. + assert.equal(repairFlankingEmphasis('x**2 + y**2'), 'x**2 + y**2') + assert.equal(repairFlankingEmphasis('2**3**4'), '2**3**4') }) test('renderChatMarkdown bolds punctuation-adjacent emphasis both mid-stream and when complete', () => { diff --git a/frontend/src/utils/chatMarkdownRenderer.ts b/frontend/src/utils/chatMarkdownRenderer.ts index 8df2d859..eadb1a17 100644 --- a/frontend/src/utils/chatMarkdownRenderer.ts +++ b/frontend/src/utils/chatMarkdownRenderer.ts @@ -181,10 +181,21 @@ const FLANKING_BOLD = /(?$1') + .replace(FLANKING_BOLD_OPEN, '$1') .replace(FLANKING_STRIKE, '$1') + .replace(FLANKING_STRIKE_OPEN, '$1') .replace(FLANKING_ITALIC, '$1') } @@ -194,10 +205,11 @@ function repairFlankingEmphasisSegment(segment: string): string { * CommonMark's flanking rule rejects a closing `**`/`*`/`~~` that is preceded by * punctuation and immediately followed by a letter/number, so a very common * model pattern like `**XBRL(…语言)**是一种` (and even ASCII `**a)**b`) renders - * as literal asterisks — both mid-stream and when complete. We convert just that - * blocked pattern (closing delimiter after punctuation, before an alphanumeric) - * into explicit HTML so it bolds reliably. Code spans/fences are skipped so - * their literal markers are untouched. + * as literal asterisks — both mid-stream and when complete. The mirror case is + * an *opening* `**`/`~~` preceded by a letter/number and immediately followed by + * punctuation (`知识库中**《手册》**整理`), which CommonMark also refuses to open. + * We convert just those blocked patterns into explicit HTML so they bold + * reliably. Code spans/fences are skipped so their literal markers are untouched. */ export function repairFlankingEmphasis(text: string): string { if (!text || !/[*~]/.test(text)) return text