Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion webview-ui/src/components/chat/ChatRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import deepEqual from "fast-deep-equal"
import React, { memo, useEffect, useMemo, useRef, useState } from "react"
import { useSize } from "react-use"
import { useCopyToClipboard } from "../../utils/clipboard"
import { cleanTrailingCodeDelimiters } from "../../utils/markdownUtils"
import {
ClineApiReqInfo,
ClineAskUseMcpServer,
Expand Down Expand Up @@ -986,13 +987,16 @@ const Markdown = memo(({ markdown, partial }: { markdown?: string; partial?: boo
const [isHovering, setIsHovering] = useState(false)
const { copyWithFeedback } = useCopyToClipboard(200) // shorter feedback duration for copy button flash

// Use the utility function to clean trailing code delimiters
const cleanedMarkdown = useMemo(() => cleanTrailingCodeDelimiters(markdown), [markdown])

return (
<div
onMouseEnter={() => setIsHovering(true)}
onMouseLeave={() => setIsHovering(false)}
style={{ position: "relative" }}>
<div style={{ wordBreak: "break-word", overflowWrap: "anywhere", marginBottom: -15, marginTop: -15 }}>
<MarkdownBlock markdown={markdown} />
<MarkdownBlock markdown={cleanedMarkdown} />
</div>
{markdown && !partial && isHovering && (
<div
Expand Down
19 changes: 19 additions & 0 deletions webview-ui/src/utils/markdownUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Utility functions for markdown processing
*/

/**
* Removes trailing code block delimiters from markdown text
*
* This function removes trailing code block delimiters like "```tool_code" or "```xml"
* that might appear at the very end of a markdown string.
*
* @param markdown - The markdown text to clean
* @returns The cleaned markdown text with trailing code delimiters removed
*/
export function cleanTrailingCodeDelimiters(markdown?: string): string | undefined {
if (!markdown) return markdown

// Regex to match trailing code block delimiters at the very end of the string
return markdown.replace(/```[a-zA-Z0-9_-]*\s*$/g, "")
}