Skip to content

Commit fe13f9b

Browse files
committed
feat: Migrate link navigation feature
1 parent 193395f commit fe13f9b

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

webview-ui/src/components/ui/markdown/Markdown.tsx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { FC, memo } from "react"
1+
import React, { FC, memo, MouseEvent } from "react"
22
import ReactMarkdown, { Options } from "react-markdown"
33
import remarkGfm from "remark-gfm"
44
import { cn } from "@/lib/utils"
5+
import { vscode } from "@src/utils/vscode"
56

67
import { Separator } from "@/components/ui"
78

@@ -117,9 +118,17 @@ export function Markdown({ content, isComplete }: { content: string; isComplete?
117118
</td>
118119
)
119120
},
120-
a({ href, children }) {
121+
a: ({ href, children }: { href?: string; children?: React.ReactNode }) => {
122+
if (typeof href === "undefined") {
123+
return <span>{children}</span>
124+
}
125+
121126
return (
122-
<a href={href} target="_blank" rel="noopener noreferrer">
127+
<a
128+
href={href}
129+
title={href}
130+
onClick={(e) => handleLinkClick(e, href)} // Call the helper function
131+
>
123132
{children}
124133
</a>
125134
)
@@ -129,3 +138,37 @@ export function Markdown({ content, isComplete }: { content: string; isComplete?
129138
</MemoizedReactMarkdown>
130139
)
131140
}
141+
142+
const handleLinkClick = (event: MouseEvent<HTMLAnchorElement>, href: string | undefined) => {
143+
if (!href) {
144+
return
145+
}
146+
147+
const isLocalPath = href.startsWith("file://") || href.startsWith("/") || !href.includes("://")
148+
149+
if (!isLocalPath) {
150+
// For non-local links, allow default behavior
151+
return
152+
}
153+
154+
event.preventDefault() // Prevent default navigation for local links
155+
156+
let filePath = href.replace("file://", "")
157+
158+
const match = filePath.match(/(.*):(\d+)(-\d+)?$/)
159+
let values = undefined
160+
if (match) {
161+
filePath = match[1]
162+
values = { line: parseInt(match[2], 10) }
163+
}
164+
165+
if (!filePath.startsWith("/") && !filePath.startsWith("./")) {
166+
filePath = "./" + filePath
167+
}
168+
169+
vscode.postMessage({
170+
type: "openFile",
171+
text: filePath,
172+
values,
173+
})
174+
}

0 commit comments

Comments
 (0)