Skip to content

Commit 86f7cdf

Browse files
committed
fix obsidian-meta-bind-plugin#516
1 parent b3bdbad commit 86f7cdf

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

jsEngine/api/Internal.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

jsEngine/engine/JsExecution.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export interface MarkdownCodeBlockExecutionContext {
2424
executionSource: ExecutionSource.MarkdownCodeBlock;
2525
/**
2626
* The file that the code block is in.
27+
* Since rendered markdown does not necessarily have an associated file, this can be undefined.
2728
*/
28-
file: TFile;
29+
file?: TFile;
2930
/**
3031
* The metadata of the file.
3132
*/
@@ -45,8 +46,9 @@ export interface MarkdownCallingJSFileExecutionContext {
4546
executionSource: ExecutionSource.MarkdownCallingJSFile;
4647
/**
4748
* The markdown file that the JS File is called from.
49+
* Since rendered markdown does not necessarily have an associated file, this can be undefined.
4850
*/
49-
file: TFile;
51+
file?: TFile;
5052
/**
5153
* The metadata of the markdown file.
5254
*/
@@ -60,9 +62,10 @@ export interface MarkdownCallingJSFileExecutionContext {
6062
export interface MarkdownOtherExecutionContext {
6163
executionSource: ExecutionSource.MarkdownOther;
6264
/**
63-
* The file that the code block is in.
65+
* The file that the markdown is associated with.
66+
* Since rendered markdown does not necessarily have an associated file, this can be undefined.
6467
*/
65-
file: TFile;
68+
file?: TFile;
6669
/**
6770
* The metadata of the file.
6871
*/

jsEngine/utils/Validators.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,23 @@ export class Validators {
9999
this.markdownCodeBlockExecutionContext = schemaForType<MarkdownCodeBlockExecutionContext>()(
100100
z.object({
101101
executionSource: z.literal(ExecutionSource.MarkdownCodeBlock),
102-
file: this.tFile,
102+
file: this.tFile.optional(),
103103
metadata: this.cachedMetadata.optional(),
104104
block: this.block.optional(),
105105
}),
106106
);
107107
this.markdownCallingJSFileExecutionContext = schemaForType<MarkdownCallingJSFileExecutionContext>()(
108108
z.object({
109109
executionSource: z.literal(ExecutionSource.MarkdownCallingJSFile),
110-
file: this.tFile,
110+
file: this.tFile.optional(),
111111
metadata: this.cachedMetadata.optional(),
112112
jsFile: this.tFile,
113113
}),
114114
);
115115
this.markdownOtherExecutionContext = schemaForType<MarkdownOtherExecutionContext>()(
116116
z.object({
117117
executionSource: z.literal(ExecutionSource.MarkdownOther),
118-
file: this.tFile,
118+
file: this.tFile.optional(),
119119
metadata: this.cachedMetadata.optional(),
120120
}),
121121
);

0 commit comments

Comments
 (0)