@@ -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.` ) ;
0 commit comments