From d02e5a89e5eef4a5459c3e6017516e26ecea3068 Mon Sep 17 00:00:00 2001 From: Wesley Smith Date: Wed, 7 May 2025 22:31:31 -0700 Subject: [PATCH] fix excessive markdown format character escaping (#3355) * fix excessive markdown format character escaping * add changeset * made it a little more robust --------- Co-authored-by: Wesley Smith Co-authored-by: Cline Evaluation --- .changeset/heavy-spoons-ring.md | 5 ++++ webview-ui/src/components/chat/ChatView.tsx | 28 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/heavy-spoons-ring.md diff --git a/.changeset/heavy-spoons-ring.md b/.changeset/heavy-spoons-ring.md new file mode 100644 index 00000000..e23842a0 --- /dev/null +++ b/.changeset/heavy-spoons-ring.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Fix for markdown copy excessive escape characters diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index 3d913c08..e0faecf2 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -41,6 +41,27 @@ interface ChatViewProps { showHistoryView: () => void } +// Function to clean up markdown escape characters +function cleanupMarkdownEscapes(markdown: string): string { + return ( + markdown + // Handle underscores and asterisks (single or multiple) + .replace(/\\([_*]+)/g, "$1") + + // Handle angle brackets (for generics and XML) + .replace(/\\([<>])/g, "$1") + + // Handle backticks (for code) + .replace(/\\(`)/g, "$1") + + // Handle other common markdown special characters + .replace(/\\([[\]()#.!])/g, "$1") + + // Fix multiple consecutive backslashes + .replace(/\\{2,}([_*`<>[\]()#.!])/g, "$1") + ) +} + async function convertHtmlToMarkdown(html: string) { // Process the HTML to Markdown const result = await unified() @@ -55,10 +76,14 @@ async function convertHtmlToMarkdown(html: string) { rule: "-", // Use - for horizontal rules ruleSpaces: false, // No spaces in horizontal rules fences: true, + escape: false, + entities: false, }) .process(html) - return String(result) + const md = String(result) + // Apply comprehensive cleanup of escape characters + return cleanupMarkdownEscapes(md) } export const MAX_IMAGES_PER_MESSAGE = 20 // Anthropic limits to 20 images @@ -120,7 +145,6 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie // Convert HTML to Markdown const markdown = await convertHtmlToMarkdown(selectedHtml) - vscode.postMessage({ type: "copyToClipboard", text: markdown }) e.preventDefault() }