Skip to content

Commit c159708

Browse files
committed
refactor(markdown): extract trailing code delimiter cleaning to utility function
1 parent e4c7b76 commit c159708

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

webview-ui/src/components/chat/ChatRow.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import deepEqual from "fast-deep-equal"
33
import React, { memo, useEffect, useMemo, useRef, useState } from "react"
44
import { useSize } from "react-use"
55
import { useCopyToClipboard } from "../../utils/clipboard"
6+
import { cleanTrailingCodeDelimiters } from "../../utils/markdownUtils"
67
import {
78
ClineApiReqInfo,
89
ClineAskUseMcpServer,
@@ -986,13 +987,8 @@ const Markdown = memo(({ markdown, partial }: { markdown?: string; partial?: boo
986987
const [isHovering, setIsHovering] = useState(false)
987988
const { copyWithFeedback } = useCopyToClipboard(200) // shorter feedback duration for copy button flash
988989

989-
// Remove trailing code delimiters like "```tool_code" or "```xml"
990-
const cleanedMarkdown = useMemo(() => {
991-
if (!markdown) return markdown
992-
993-
// Regex to match trailing code block delimiters at the very end of the string
994-
return markdown.replace(/```[a-zA-Z0-9_-]*\s*$/g, "")
995-
}, [markdown])
990+
// Use the utility function to clean trailing code delimiters
991+
const cleanedMarkdown = useMemo(() => cleanTrailingCodeDelimiters(markdown), [markdown])
996992

997993
return (
998994
<div
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Utility functions for markdown processing
3+
*/
4+
5+
/**
6+
* Removes trailing code block delimiters from markdown text
7+
*
8+
* This function removes trailing code block delimiters like "```tool_code" or "```xml"
9+
* that might appear at the very end of a markdown string.
10+
*
11+
* @param markdown - The markdown text to clean
12+
* @returns The cleaned markdown text with trailing code delimiters removed
13+
*/
14+
export function cleanTrailingCodeDelimiters(markdown?: string): string | undefined {
15+
if (!markdown) return markdown
16+
17+
// Regex to match trailing code block delimiters at the very end of the string
18+
return markdown.replace(/```[a-zA-Z0-9_-]*\s*$/g, "")
19+
}

0 commit comments

Comments
 (0)