@@ -145,8 +145,8 @@ export class InternalAPI {
145
145
public async getContextForMarkdownCodeBlock ( path : string ) : Promise < MarkdownCodeBlockExecutionContext > {
146
146
validateAPIArgs ( z . object ( { path : z . string ( ) } ) , { path } ) ;
147
147
148
- const file = this . getFileWithExtension ( path , 'md' ) ;
149
- const metadata = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
148
+ const file = this . tryGetFileWithExtension ( path , 'md' ) ;
149
+ const metadata = file ? this . apiInstance . app . metadataCache . getFileCache ( file ) : undefined ;
150
150
151
151
return {
152
152
executionSource : ExecutionSource . MarkdownCodeBlock ,
@@ -167,8 +167,8 @@ export class InternalAPI {
167
167
public async getContextForMarkdownCallingJSFile ( markdownPath : string , jsPath : string ) : Promise < MarkdownCallingJSFileExecutionContext > {
168
168
validateAPIArgs ( z . object ( { markdownPath : z . string ( ) , jsPath : z . string ( ) } ) , { markdownPath, jsPath } ) ;
169
169
170
- const markdownFile = this . getFileWithExtension ( markdownPath , 'md' ) ;
171
- const metadata = this . apiInstance . app . metadataCache . getFileCache ( markdownFile ) ;
170
+ const markdownFile = this . tryGetFileWithExtension ( markdownPath , 'md' ) ;
171
+ const metadata = markdownFile ? this . apiInstance . app . metadataCache . getFileCache ( markdownFile ) : undefined ;
172
172
173
173
const jsFile = this . getFileWithExtension ( jsPath , 'js' ) ;
174
174
@@ -189,8 +189,8 @@ export class InternalAPI {
189
189
public async getContextForMarkdownOther ( path : string ) : Promise < MarkdownOtherExecutionContext > {
190
190
validateAPIArgs ( z . object ( { path : z . string ( ) } ) , { path } ) ;
191
191
192
- const file = this . getFileWithExtension ( path , 'md' ) ;
193
- const metadata = this . apiInstance . app . metadataCache . getFileCache ( file ) ;
192
+ const file = this . tryGetFileWithExtension ( path , 'md' ) ;
193
+ const metadata = file ? this . apiInstance . app . metadataCache . getFileCache ( file ) : undefined ;
194
194
195
195
return {
196
196
executionSource : ExecutionSource . MarkdownOther ,
@@ -279,4 +279,15 @@ export class InternalAPI {
279
279
}
280
280
return file ;
281
281
}
282
+
283
+ private tryGetFileWithExtension ( path : string , extension : string ) : TFile | undefined {
284
+ const file = this . apiInstance . app . vault . getAbstractFileByPath ( path ) ;
285
+ if ( ! file || ! ( file instanceof TFile ) ) {
286
+ return undefined ;
287
+ }
288
+ if ( file . extension !== extension && file . extension !== `.${ extension } ` ) {
289
+ return undefined ;
290
+ }
291
+ return file ;
292
+ }
282
293
}
0 commit comments