@@ -7,7 +7,7 @@ import { cn } from "@/lib/utils";
7
7
import { ChatMessage } from "@/sharedState" ;
8
8
import { insertIntoEditor } from "@/utils" ;
9
9
import { Bot , User } from "lucide-react" ;
10
- import { App , Component , MarkdownRenderer } from "obsidian" ;
10
+ import { App , Component , MarkdownRenderer , TFile } from "obsidian" ;
11
11
import React , { useCallback , useEffect , useRef , useState } from "react" ;
12
12
13
13
function MessageContext ( { context } : { context : ChatMessage [ "context" ] } ) {
@@ -81,6 +81,15 @@ const ChatSingleMessage: React.FC<ChatSingleMessageProps> = ({
81
81
82
82
const preprocess = useCallback (
83
83
( content : string ) : string => {
84
+ const activeFile = app . workspace . getActiveFile ( ) ;
85
+ const sourcePath = activeFile ? activeFile . path : "" ;
86
+
87
+ const replaceLinks = ( text : string , regex : RegExp , template : ( file : TFile ) => string ) =>
88
+ text . replace ( regex , ( match : string , selection : string ) => {
89
+ const file = app . metadataCache . getFirstLinkpathDest ( selection , sourcePath ) ;
90
+ return file ? template ( file ) : match ;
91
+ } ) ;
92
+
84
93
// Process LaTeX
85
94
const latexProcessed = content
86
95
. replace ( / \\ \[ \s * / g, "$$" )
@@ -89,22 +98,28 @@ const ChatSingleMessage: React.FC<ChatSingleMessageProps> = ({
89
98
. replace ( / \s * \\ \) / g, "$" ) ;
90
99
91
100
// Process only Obsidian internal images (starting with ![[)
92
- const activeFile = app . workspace . getActiveFile ( ) ;
93
- const sourcePath = activeFile ? activeFile . path : "" ;
101
+ const noteImageProcessed = replaceLinks (
102
+ latexProcessed ,
103
+ / ! \[ \[ ( .* ?) ] ] / g,
104
+ ( file ) => ` } )`
105
+ ) ;
94
106
95
- const noteImageProcessed = latexProcessed . replace (
96
- / ! \[ \[ ( .* ?) \] \] / g,
97
- ( match : string , imageName : string ) => {
98
- const imageFile = app . metadataCache . getFirstLinkpathDest ( imageName , sourcePath ) ;
99
- if ( imageFile ) {
100
- const imageUrl = app . vault . getResourcePath ( imageFile ) ;
101
- return `` ;
102
- }
103
- return match ;
104
- }
107
+ const clickable = ( file : TFile ) =>
108
+ `[${ file . name } ](obsidian://open?vault=${ encodeURIComponent ( app . vault . getName ( ) ) } &file=${ encodeURIComponent ( file . path ) } )` ;
109
+ // Transform [[link]] format but exclude ![[]] image links
110
+ const noteLinksProcessed = replaceLinks (
111
+ noteImageProcessed ,
112
+ / (?< ! ! ) \[ \[ ( [ ^ \] ] + ) ] ] / g,
113
+ clickable
114
+ ) ;
115
+ // Transform [title](app://obsidian.md/note) format but exclude ![[]] image links
116
+ const appNoteLinks = replaceLinks (
117
+ noteLinksProcessed ,
118
+ / (?< ! ! ) \[ ( [ ^ \] ] + ) ] \( a p p : \/ \/ o b s i d i a n \. m d \/ ( [ ^ ) ] + ) \) / g,
119
+ clickable
105
120
) ;
106
121
107
- return noteImageProcessed ;
122
+ return appNoteLinks ;
108
123
} ,
109
124
[ app ]
110
125
) ;
0 commit comments