fix: Found one low-severity UI localization regression in the markdown (#74600)

Co-authored-by: openclaw-clawsweeper[bot] <280122609+openclaw-clawsweeper[bot]@users.noreply.github.com>
This commit is contained in:
clawsweeper[bot]
2026-04-29 13:58:07 -07:00
committed by GitHub
parent fdf8ffaf3c
commit 585c2bdba3
2 changed files with 23 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import { render } from "lit";
import { describe, expect, it, vi } from "vitest";
import { i18n } from "../i18n/index.ts";
import { md, toSanitizedMarkdownHtml } from "./markdown.ts";
import { renderMarkdownSidebar } from "./views/markdown-sidebar.ts";
@@ -317,6 +318,23 @@ describe("toSanitizedMarkdownHtml", () => {
expect(html).toContain("data-code=");
});
it("keeps localized copy labels fresh after locale changes", async () => {
const markdown = "```ts\nconst localizedCopy = true;\n```";
await i18n.setLocale("en");
const english = toSanitizedMarkdownHtml(markdown);
try {
await i18n.setLocale("zh-CN");
const chinese = toSanitizedMarkdownHtml(markdown);
expect(english).toContain(">Copy<");
expect(chinese).toContain(">复制<");
expect(chinese).not.toContain(">Copy<");
} finally {
await i18n.setLocale("en");
}
});
it("collapses JSON code blocks", () => {
const html = toSanitizedMarkdownHtml('```json\n{"key": "value"}\n```');
expect(html).toContain("<details");

View File

@@ -1,7 +1,7 @@
import DOMPurify from "dompurify";
import MarkdownIt from "markdown-it";
import markdownItTaskLists from "markdown-it-task-lists";
import { t } from "../i18n/index.ts";
import { i18n, t } from "../i18n/index.ts";
import { truncateText } from "./format.ts";
import { normalizeLowercaseStringOrEmpty } from "./string-coerce.ts";
@@ -481,8 +481,9 @@ export function toSanitizedMarkdownHtml(markdown: string): string {
return "";
}
installHooks();
const cacheKey = `${i18n.getLocale()}\0${input}`;
if (input.length <= MARKDOWN_CACHE_MAX_CHARS) {
const cached = getCachedMarkdown(input);
const cached = getCachedMarkdown(cacheKey);
if (cached !== null) {
return cached;
}
@@ -498,7 +499,7 @@ export function toSanitizedMarkdownHtml(markdown: string): string {
const html = renderEscapedPlainTextHtml(`${truncated.text}${suffix}`);
const sanitized = DOMPurify.sanitize(html, sanitizeOptions);
if (input.length <= MARKDOWN_CACHE_MAX_CHARS) {
setCachedMarkdown(input, sanitized);
setCachedMarkdown(cacheKey, sanitized);
}
return sanitized;
}
@@ -513,7 +514,7 @@ export function toSanitizedMarkdownHtml(markdown: string): string {
}
const sanitized = DOMPurify.sanitize(rendered, sanitizeOptions);
if (input.length <= MARKDOWN_CACHE_MAX_CHARS) {
setCachedMarkdown(input, sanitized);
setCachedMarkdown(cacheKey, sanitized);
}
return sanitized;
}