@@ -39,8 +39,10 @@ export default class OpenVSCode extends Plugin {
3939 }
4040 else {
4141 const { exec } = require ( "child_process" ) ;
42- const template = this . settings . executeTemplate . trim ( ) === "" ? DEFAULT_SETTINGS . executeTemplate : this . settings . executeTemplate ;
43- const command = template . replace ( "{{vaultpath}}" , path ) ;
42+ const file = this . app . workspace . getActiveFile ( ) ;
43+ var command = this . settings . executeTemplate . trim ( ) === "" ? DEFAULT_SETTINGS . executeTemplate : this . settings . executeTemplate ;
44+ command = replaceAll ( command , "{{vaultpath}}" , path ) ;
45+ command = replaceAll ( command , "{{filepath}}" , file ?. path ?? "" ) ;
4446 exec ( command , ( error : any , stdout : any , stderr : any ) => {
4547 if ( error ) {
4648 console . error ( `exec error: ${ error } ` ) ;
@@ -116,7 +118,7 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
116118 ) ;
117119 new Setting ( containerEl )
118120 . setName ( 'Default template for executing the `code` command' )
119- . setDesc ( 'You can use the following placeholders : {{vaultpath}}' )
121+ . setDesc ( 'You can use the following variables : {{vaultpath}}, {{filepath}} (relative) ' )
120122 . addText ( text => text . setPlaceholder ( DEFAULT_SETTINGS . executeTemplate )
121123 . setValue ( this . plugin . settings . executeTemplate || DEFAULT_SETTINGS . executeTemplate )
122124 . onChange ( ( value ) => {
@@ -127,4 +129,13 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
127129 } ) ) ;
128130 }
129131
130- }
132+ }
133+
134+ // https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript
135+ function escapeRegExp ( string : String ) {
136+ return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
137+ }
138+
139+ function replaceAll ( str : String , find : String , replace : any ) {
140+ return str . replace ( new RegExp ( escapeRegExp ( find ) , 'g' ) , replace ) ;
141+ }
0 commit comments