Skip to content

Commit a2385f7

Browse files
committed
add __filename and __dirname variables to JS views
1 parent 234ab89 commit a2385f7

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/api/script-cache.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class ScriptCache {
105105
if (!maybeSource.successful) return maybeSource;
106106

107107
// Transpile to vanilla javascript first...
108-
const { code, language } = maybeSource.value;
108+
const { code, language, filepath } = maybeSource.value;
109109
let basic;
110110
try {
111111
basic = transpile(code, language);
@@ -114,7 +114,15 @@ export class ScriptCache {
114114
}
115115

116116
// Then finally execute the script to 'load' it.
117-
const finalContext = Object.assign({ h: h, Fragment: Fragment }, context);
117+
const finalContext = Object.assign(
118+
{
119+
h: h,
120+
Fragment: Fragment,
121+
__filename: filepath,
122+
__dirname: filepath.split("/").slice(0, -1).join("/"),
123+
},
124+
context
125+
);
118126
try {
119127
return Result.success(await asyncEvalInContext(basic, finalContext));
120128
} catch (error) {
@@ -129,22 +137,31 @@ export class ScriptCache {
129137
}
130138

131139
/** Attempts to resolve the source to load given a path or link to a markdown section. */
132-
private async resolveSource(
133-
path: string | Link
134-
): Promise<Result<{ code: string; language: ScriptLanguage }, string>> {
140+
private async resolveSource(path: string | Link): Promise<
141+
Result<
142+
{
143+
code: string;
144+
language: ScriptLanguage;
145+
filepath: string;
146+
},
147+
string
148+
>
149+
> {
135150
const object = this.store.resolveLink(path);
136151
if (!object) return Result.failure("Could not find a script at the given path: " + path.toString());
137152

138153
const tfile = this.store.vault.getFileByPath(object.$file!);
139154
if (!tfile) return Result.failure(`File "${object.$file}" not found.`);
140155

156+
const filepath = object.$file!;
157+
141158
// Check if this is a JS file we should load directly.
142159
if (tfile.extension.toLocaleLowerCase() in ScriptCache.FILE_EXTENSIONS) {
143160
const language = ScriptCache.FILE_EXTENSIONS[tfile.extension.toLocaleLowerCase()];
144161

145162
try {
146163
const code = await this.store.vault.cachedRead(tfile);
147-
return Result.success({ code, language });
164+
return Result.success({ code, language, filepath });
148165
} catch (error) {
149166
return Result.failure("Failed to load javascript/typescript source file: " + error);
150167
}
@@ -165,7 +182,7 @@ export class ScriptCache {
165182
ScriptCache.SCRIPT_LANGUAGES[
166183
maybeBlock.$languages.find((lang) => lang.toLocaleLowerCase() in ScriptCache.SCRIPT_LANGUAGES)!
167184
];
168-
return (await this.readCodeblock(tfile, maybeBlock)).map((code) => ({ code, language }));
185+
return (await this.readCodeblock(tfile, maybeBlock)).map((code) => ({ code, language, filepath }));
169186
} else if (object instanceof MarkdownCodeblock) {
170187
const maybeLanguage = object.$languages.find(
171188
(lang) => lang.toLocaleLowerCase() in ScriptCache.SCRIPT_LANGUAGES
@@ -174,7 +191,7 @@ export class ScriptCache {
174191
return Result.failure(`The codeblock referenced by '${path}' is not a JS/TS codeblock.`);
175192

176193
const language = ScriptCache.SCRIPT_LANGUAGES[maybeLanguage];
177-
return (await this.readCodeblock(tfile, object)).map((code) => ({ code, language }));
194+
return (await this.readCodeblock(tfile, object)).map((code) => ({ code, language, filepath }));
178195
}
179196

180197
return Result.failure(`Cannot import '${path.toString()}: not a JS/TS file or codeblock reference.`);

src/ui/javascript.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export class DatacoreJSRenderer extends MarkdownRenderChild {
3636
dc: this.api,
3737
h: h,
3838
Fragment: Fragment,
39+
__filename: this.path,
40+
__dirname: this.path.split("/").slice(0, -1).join("/"),
3941
})) as Promise<Literal | VNode | Function>;
4042
};
4143

0 commit comments

Comments
 (0)