Markdown copy (#3060)

* add markdown copy

* add changeset

* fmt

---------

Co-authored-by: Wesley Smith <wes@neofactory.ai>
This commit is contained in:
Wesley SmithandWesley Smith authored and GitHub committed 2025-04-27 15:24:08 -07:00
1 parent df37f29746
commit 459adf0450
6 files changed
+1021 -56

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Add markdown copy to chat
+9
View File
@@ -801,6 +801,15 @@ export class Controller {
}
break
}
case "copyToClipboard": {
try {
await vscode.env.clipboard.writeText(message.text || "")
} catch (error) {
console.error("Error copying to clipboard:", error)
}
break
}
// Add more switch case statements here as more webview message commands
// are created within the webview context (i.e. inside media/main.js)
}
+1
View File
@@ -72,6 +72,7 @@ export interface WebviewMessage {
| "grpc_request"
| "toggleClineRule"
| "deleteClineRule"
| "copyToClipboard"
// | "relaunchChromeDebugMode"
text?: string
+951 -56
View File
File diff suppressed because it is too large. Load diff
+4
View File
@@ -33,7 +33,11 @@
"react-use": "^17.6.0",
"react-virtuoso": "^4.12.3",
"rehype-highlight": "^7.0.1",
"rehype-parse": "^9.0.1",
"rehype-remark": "^10.0.1",
"remark-stringify": "^11.0.0",
"styled-components": "^6.1.15",
"unified": "^11.0.5",
"uuid": "^9.0.1"
},
"devDependencies": {
@@ -28,6 +28,10 @@ import ChatRow from "@/components/chat/ChatRow"
import ChatTextArea from "@/components/chat/ChatTextArea"
import TaskHeader from "@/components/chat/TaskHeader"
import TelemetryBanner from "@/components/common/TelemetryBanner"
import { unified } from "unified"
import remarkStringify from "remark-stringify"
import rehypeRemark from "rehype-remark"
import rehypeParse from "rehype-parse"
interface ChatViewProps {
isHidden: boolean
@@ -36,6 +40,26 @@ interface ChatViewProps {
showHistoryView: () => void
}
async function convertHtmlToMarkdown(html: string) {
// Process the HTML to Markdown
const result = await unified()
.use(rehypeParse as any, { fragment: true }) // Parse HTML fragments
.use(rehypeRemark as any) // Convert HTML to Markdown AST
.use(remarkStringify as any, {
// Convert Markdown AST to text
bullet: "-", // Use - for unordered lists
emphasis: "*", // Use * for emphasis
strong: "_", // Use _ for strong
listItemIndent: "one", // Use one space for list indentation
rule: "-", // Use - for horizontal rules
ruleSpaces: false, // No spaces in horizontal rules
fences: true,
})
.process(html)
return String(result)
}
export const MAX_IMAGES_PER_MESSAGE = 20 // Anthropic limits to 20 images
const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryView }: ChatViewProps) => {
@@ -79,6 +103,33 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
const [showScrollToBottom, setShowScrollToBottom] = useState(false)
const [isAtBottom, setIsAtBottom] = useState(false)
useEffect(() => {
const handleCopy = async (e: ClipboardEvent) => {
if (window.getSelection) {
const selection = window.getSelection()
if (selection && selection.rangeCount > 0) {
// Get the selected HTML content
const range = selection.getRangeAt(0)
const clonedSelection = range.cloneContents()
const div = document.createElement("div")
div.appendChild(clonedSelection)
const selectedHtml = div.innerHTML
// Convert HTML to Markdown
const markdown = await convertHtmlToMarkdown(selectedHtml)
vscode.postMessage({ type: "copyToClipboard", text: markdown })
e.preventDefault()
}
}
}
document.addEventListener("copy", handleCopy)
return () => {
document.removeEventListener("copy", handleCopy)
}
}, [])
// UI layout depends on the last 2 messages
// (since it relies on the content of these messages, we are deep comparing. i.e. the button state after hitting button sets enableButtons to false, and this effect otherwise would have to true again even if messages didn't change
const lastMessage = useMemo(() => messages.at(-1), [messages])