Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(webui): keep CJK prose outside bold URL links (#6115)
chengyongrude56e5de6b0f
diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsxindex 772ae0a7a..8eb1792c5 100644--- a/webui/src/components/MarkdownTextRenderer.tsx+++ b/webui/src/components/MarkdownTextRenderer.tsx@@ -37,6 +37,7 @@ import {import { useLogoFallback } from "@/hooks/useLogoFallback";import { inferMediaKind } from "@/lib/media";import { browserSafeFaviconUrls } from "@/lib/provider-brand";+import { remarkCjkAutolinks } from "@/lib/remark-cjk-autolinks";import { remarkTexMath } from "@/lib/remark-tex-math";import { cn } from "@/lib/utils";@@ -300,6 +301,7 @@ const remarkPlugins: NonNullable<StreamdownProps["remarkPlugins"]> = [remarkCjkStrongBoundaries,remarkBreaks,remarkGfm,+ remarkCjkAutolinks,[remarkMath, { singleDollarTextMath: false }],remarkTexMath,remarkSafeHtmlSubset,diff --git a/webui/src/lib/remark-cjk-autolinks.ts b/webui/src/lib/remark-cjk-autolinks.tsnew file mode 100644index 000000000..0d3c345fd--- /dev/null+++ b/webui/src/lib/remark-cjk-autolinks.ts@@ -0,0 +1,99 @@+import type { Root } from "mdast";+import type { Code, Construct, Extension, State, Tokenizer } from "micromark-util-types";+import type { Plugin } from "unified";+import type {} from "micromark-extension-gfm-autolink-literal";++const CJK_PUNCTUATION = /[。,、;:!?()【】《》「」『』“”‘’]/u;+const TRAILING_MARKERS = /[?!.,:*_~]/u;++function isCjkPunctuation(code: Code): boolean {+ return code !== null && code >= 0 && CJK_PUNCTUATION.test(String.fromCodePoint(code));+}++const cjkTrail: Construct = {+ partial: true,+ tokenize(effects, ok, nok) {+ return start;+ function start(code: Code) {+ if (isCjkPunctuation(code)) return ok(code);+ if (code === null || code < 0 || !TRAILING_MARKERS.test(String.fromCodePoint(code))) {+ return nok(code);+ }+ effects.enter("data");+ effects.consume(code);+ return scan;+ }+ function scan(code: Code): ReturnType<State> {+ if (isCjkPunctuation(code)) {+ effects.exit("data");+ return ok(code);+ }+ if (code !== null && code >= 0 && TRAILING_MARKERS.test(String.fromCodePoint(code))) {+ effects.consume(code);+ return scan;+ }+ return nok(code);+ }+ },+};++function boundCjkAutolink(construct: Construct): Construct {+ if (construct.name !== "protocolAutolink" && construct.name !== "wwwAutolink") {+ return construct;+ }+ const tokenType = construct.name === "wwwAutolink" ? "literalAutolinkWww" : "literalAutolinkHttp";+ const cjkAutolink: Construct = {+ ...construct,+ tokenize(effects, ok, nok) {+ return construct.tokenize.call(this, effects, (code) => {+ // A successful GFM tokenizer has just closed its literalAutolink token.+ // Inspect only that URL, never text beyond GFM's own link boundary.+ const token = this.events[this.events.length - 1][1];+ return CJK_PUNCTUATION.test(this.sliceSerialize(token)) ? ok(code) : nok(code);+ }, nok);+ },+ };+ const tokenize: Tokenizer = function (effects, ok, nok) {+ // Bound only URLs already accepted by GFM. Closing emphasis markers stay+ // available to CommonMark, while ordinary text keeps GFM's fast rejection.+ return effects.check(cjkAutolink, start, (code) =>+ construct.tokenize.call(this, effects, ok, nok)(code));++ function start(code: Code) {+ effects.enter("literalAutolink");+ effects.enter(tokenType);+ return scan(code);+ }+ function scan(code: Code): ReturnType<State> {+ return effects.check(cjkTrail, end, consume)(code);+ }+ function consume(code: Code) {+ effects.consume(code);+ return scan;+ }+ function end(code: Code) {+ effects.exit(tokenType);+ effects.exit("literalAutolink");+ return ok(code);+ }+ };+ return { ...construct, tokenize };+}++/** Bound GFM bare links at CJK prose without changing explicit link targets. */+export const remarkCjkAutolinks: Plugin<[], Root> = function () {+ const data = this.data();+ data.micromarkExtensions = data.micromarkExtensions?.map((extension): Extension => {+ if (!extension.text) return extension;+ const text = { ...extension.text };+ for (const code of [72, 104, 87, 119]) {+ const constructs = text[code];+ if (constructs) {+ text[code] = Array.isArray(constructs)+ ? constructs.map(boundCjkAutolink)+ : boundCjkAutolink(constructs);+ }+ }+ return { ...extension, text };+ });+};diff --git a/webui/src/tests/markdown-text-renderer.test.tsx b/webui/src/tests/markdown-text-renderer.test.tsxindex 913c0504c..279c79e06 100644--- a/webui/src/tests/markdown-text-renderer.test.tsx+++ b/webui/src/tests/markdown-text-renderer.test.tsx@@ -586,6 +586,63 @@ describe("MarkdownTextRenderer", () => {expect(screen.getByRole("link", { name: "links" })).not.toHaveAttribute("node");});+ it("keeps CJK prose outside a bold bare URL through stream completion", () => {+ const url = "https://github.com/HKUDS/nanobot";+ const source = `实际跳转到 **${url}**,不是推文页面。`;+ const { container, rerender } = render(<MarkdownTextRenderer>{source}</MarkdownTextRenderer>);+ const assertLink = () => {+ const link = screen.getByRole("link", { name: url });+ expect(link).toHaveAttribute("href", url);+ expect(link.closest("strong")).not.toBeNull();+ expect(container.textContent).toBe(`实际跳转到 ${url},不是推文页面。`);+ };+ assertLink();++ for (let end = 1; end <= source.length; end += 1) {+ rerender(<MarkdownTextRenderer streaming preserveStreamingLayout>{source.slice(0, end)}</MarkdownTextRenderer>);+ }+ assertLink();+ rerender(<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>);+ assertLink();+ });++ it.each([+ ["https://example.com/中文?q=测试", ",后续说明。"],+ ["https://example.com/a_(b)?x=1&y=2#section", "。后续说明"],+ ["www.example.com/path", ";后续说明"],+ ["https://例子.测试/路径", ",后续说明"],+ ["http://localhost:7891/path", ",后续说明"],+ ["https://example.com/foo*bar*baz", ",后续说明"],+ ["https://example.com/escaped%EF%BC%8Cpath", ",后续说明"],+ ])("bounds bare URL %s before CJK punctuation", (url, following) => {+ const { container } = render(<MarkdownTextRenderer>{url + following}</MarkdownTextRenderer>);+ expect(screen.getByRole("link", { name: url })).toHaveAttribute(+ "href", encodeURI(url.startsWith("www.") ? `http://${url}` : url).replace(/%25/g, "%"),+ );+ expect(container.textContent).toBe(url + following);+ });++ it("preserves explicit URL punctuation and literal URLs in code and escapes", () => {+ const url = "https://example.com/中文,测试";+ const { container } = render(+ <MarkdownTextRenderer highlightCode={false}>+ {`[Explicit](${url}) <${url}>\n\n\`**${url}**\`\n\n\`\`\`text\n**${url}**\n\`\`\`\n\n\\*\\*${url}\\*\\*`}+ </MarkdownTextRenderer>,+ );+ expect(screen.getByRole("link", { name: "Explicit" })).toHaveAttribute("href", encodeURI(url));+ expect(screen.getByRole("link", { name: url })).toHaveAttribute("href", encodeURI(url));+ expect(container.querySelector("strong")).toBeNull();+ expect(container.querySelector("code")?.textContent).toBe(`**${url}**`);+ });++ it("keeps GFM link boundaries before a partial Markdown resource containing CJK text", () => {+ const url = "https://example.com/path";+ const source = `${url}](中文,后续说明。`;+ const { container } = render(<MarkdownTextRenderer>{source}</MarkdownTextRenderer>);+ expect(screen.getByRole("link", { name: url })).toHaveAttribute("href", url);+ expect(container.textContent).toBe(source);+ });+it("renders bold CJK text when more CJK text follows immediately", () => {render(<MarkdownTextRenderer streaming>