Skip to content

Commit

Permalink
new {{filepath}} variable
Browse files Browse the repository at this point in the history
  • Loading branch information
NomarCub committed Jan 1, 2022
1 parent 32c9f66 commit 5915b98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
19 changes: 15 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export default class OpenVSCode extends Plugin {
}
else {
const { exec } = require("child_process");
const template = this.settings.executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : this.settings.executeTemplate;
const command = template.replace("{{vaultpath}}", path);
const file = this.app.workspace.getActiveFile();
var command = this.settings.executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : this.settings.executeTemplate;
command = replaceAll(command, "{{vaultpath}}", path);
command = replaceAll(command, "{{filepath}}", file?.path ?? "");
exec(command, (error: any, stdout: any, stderr: any) => {
if (error) {
console.error(`exec error: ${error}`);
Expand Down Expand Up @@ -116,7 +118,7 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
);
new Setting(containerEl)
.setName('Default template for executing the `code` command')
.setDesc('You can use the following placeholders: {{vaultpath}}')
.setDesc('You can use the following variables: {{vaultpath}}, {{filepath}} (relative)')
.addText(text => text.setPlaceholder(DEFAULT_SETTINGS.executeTemplate)
.setValue(this.plugin.settings.executeTemplate || DEFAULT_SETTINGS.executeTemplate)
.onChange((value) => {
Expand All @@ -127,4 +129,13 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
}));
}

}
}

// https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript
function escapeRegExp(string: String) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function replaceAll(str: String, find: String, replace: any) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "open-vscode",
"name": "Open vault in VSCode",
"version": "1.1.2",
"version": "1.1.3",
"minAppVersion": "0.11.13",
"description": "Ribbon button and command to open vault as a Visual Studio Code workspace",
"author": "NomarCub",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "open-vscode",
"version": "1.1.2",
"version": "1.1.3",
"description": "Open vault in Visual Studio Code ribbon button and command for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 5915b98

Please sign in to comment.