@@ -145,8 +145,8 @@ export class InternalAPI {
145145 public async getContextForMarkdownCodeBlock ( path : string ) : Promise < MarkdownCodeBlockExecutionContext > {
146146 validateAPIArgs ( z . object ( { path : z . string ( ) } ) , { path } ) ;
147147
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 ;
150150
151151 return {
152152 executionSource : ExecutionSource . MarkdownCodeBlock ,
@@ -167,8 +167,8 @@ export class InternalAPI {
167167 public async getContextForMarkdownCallingJSFile ( markdownPath : string , jsPath : string ) : Promise < MarkdownCallingJSFileExecutionContext > {
168168 validateAPIArgs ( z . object ( { markdownPath : z . string ( ) , jsPath : z . string ( ) } ) , { markdownPath, jsPath } ) ;
169169
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 ;
172172
173173 const jsFile = this . getFileWithExtension ( jsPath , 'js' ) ;
174174
@@ -189,8 +189,8 @@ export class InternalAPI {
189189 public async getContextForMarkdownOther ( path : string ) : Promise < MarkdownOtherExecutionContext > {
190190 validateAPIArgs ( z . object ( { path : z . string ( ) } ) , { path } ) ;
191191
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 ;
194194
195195 return {
196196 executionSource : ExecutionSource . MarkdownOther ,
@@ -279,4 +279,15 @@ export class InternalAPI {
279279 }
280280 return file ;
281281 }
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+ }
282293}
0 commit comments