Skip to content

Commit

Permalink
feat: add toggle to exclude vault param, and specify name or vault ID (
Browse files Browse the repository at this point in the history
…#153)

* add toggle to NOT include vault name in copied URI

(squashed commit)
- ensure vault param always comes first
- add missing type definition
- add toggle to select whether to use Vault Name or ID in URI

* update

* style: format

---------

Co-authored-by: Vinzent <vinzent03@proton.me>
  • Loading branch information
luckman212 and Vinzent03 authored Jan 24, 2024
1 parent 969f212 commit 209160e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const DEFAULT_SETTINGS: AdvancedURISettings = {
useUID: false,
addFilepathWhenUsingUID: false,
allowEval: false,
includeVaultName: true,
vaultParam: "name",
};
30 changes: 30 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ export class SettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Include vault name/ID parameter")
.addToggle((cb) =>
cb
.setValue(this.plugin.settings.includeVaultName)
.onChange((value) => {
this.plugin.settings.includeVaultName = value;
this.plugin.saveSettings();
this.display();
})
);

if (this.plugin.settings.includeVaultName) {
new Setting(containerEl)
.setName("Vault identifying parameter")
.setDesc(
"Choose whether to use the vault Name or its internal ID as the identifying parameter."
)
.addDropdown((cb) =>
cb
.addOption("name", "Name")
.addOption("id", "ID")
.setValue(this.plugin.settings.vaultParam)
.onChange((value) => {
this.plugin.settings.vaultParam = value;
this.plugin.saveSettings();
})
);
}

if (this.plugin.settings.useUID) {
new Setting(containerEl)
.setName("Add filepath parameter")
Expand Down
22 changes: 13 additions & 9 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ export default class Tools {
}

async generateURI(parameters: Parameters, doubleEncode: boolean) {
const prefix = `obsidian://advanced-uri?vault=${encodeURIComponent(
app.vault.getName()
)}`;
const prefix = "obsidian://advanced-uri";
let suffix = "";
const file = app.vault.getAbstractFileByPath(parameters.filepath);

if (this.settings.includeVaultName) {
suffix += "?vault=";
if (this.settings.vaultParam == "id" && app.appId) {
suffix += app.appId;
} else {
suffix += app.vault.getName();
}
}
if (
this.settings.useUID &&
file instanceof TFile &&
Expand All @@ -91,11 +96,10 @@ export default class Tools {
}
for (const parameter in parameters) {
if ((parameters as any)[parameter] != undefined) {
suffix =
suffix +
`&${parameter}=${encodeURIComponent(
(parameters as any)[parameter]
)}`;
suffix += suffix ? "&" : "?";
suffix += `${parameter}=${encodeURIComponent(
(parameters as any)[parameter]
)}`;
}
}
if (doubleEncode) {
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PaneType, Plugin } from "obsidian";
import { PaneType } from "obsidian";

declare module "obsidian" {
interface App {
Expand All @@ -18,6 +18,7 @@ declare module "obsidian" {
activeTab: SettingTab;
open(): void;
};
appId: string;
commands: {
executeCommandById(id: string): void;
commands: {
Expand Down Expand Up @@ -97,6 +98,8 @@ export interface AdvancedURISettings {
useUID: boolean;
addFilepathWhenUsingUID: boolean;
allowEval: boolean;
includeVaultName: boolean;
vaultParam: "id" | "name";
}

export interface Parameters {
Expand Down

0 comments on commit 209160e

Please sign in to comment.