Skip to content

Commit 5915b98

Browse files
committed
new {{filepath}} variable
1 parent 32c9f66 commit 5915b98

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

main.ts

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

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "open-vscode",
33
"name": "Open vault in VSCode",
4-
"version": "1.1.2",
4+
"version": "1.1.3",
55
"minAppVersion": "0.11.13",
66
"description": "Ribbon button and command to open vault as a Visual Studio Code workspace",
77
"author": "NomarCub",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "open-vscode",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Open vault in Visual Studio Code ribbon button and command for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)