diff --git a/README.md b/README.md index 9a89d9c..6194a61 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This plugin allows you to add custom URI commands to the command palette. Can be used with the [Obsidian URI scheme](https://help.obsidian.md/Advanced+topics/Using+obsidian+URI), as well as any other URI scheme your computer supports. ### Placeholders -You can use the placeholders below in your URI. All of these are [URL-encoded](https://en.wikipedia.org/wiki/Percent-encoding) for you, so you don't need to worry about your text having any unescaped illegal or reserved characters. +You can use the placeholders below in your URI. All of these are [URL-encoded](https://en.wikipedia.org/wiki/Percent-encoding) for you unless you turn off URL-encoding, so you don't need to worry about your text having any unescaped illegal or reserved characters. All commands with placeholders are hidden when there is no active file. @@ -31,6 +31,8 @@ All commands with placeholders are hidden when there is no active file. - Note that for websites, you *must* start your URI with `https://` or `http://`, not `www.` - Open the wikipedia page for the contents of the YAML field "topic": `https://en.wikipedia.org/wiki/{{meta:topic}}` - Look up your selection in your Calibre library: `calibre://search/_?q={{selection}}` +- Open the url in the "external-link" metadata field: `{{meta:external-link}}` + - Note that for this to work, URL encoding must be turned off ## Related plugins - [Advanced URI](https://github.com/Vinzent03/obsidian-advanced-uri): enables URIs for daily note, appending text to a file, jump to heading, search and replace, and more diff --git a/src/URIModal.ts b/src/URIModal.ts index ef9c440..f10dabc 100644 --- a/src/URIModal.ts +++ b/src/URIModal.ts @@ -21,6 +21,7 @@ export default class URIModal extends Modal { name: "", id: "", URITemplate: "", + encode: true, } } else { this.uriCommand = command; @@ -70,6 +71,16 @@ export default class URIModal extends Modal { }); }); + new Setting(contentEl) + .setName("URL-encode input") + .setDesc("Automatically URL-encode any user input text. Should only be off if content is already encoded or itself a URI scheme (e.g.. a bare URL with https://).") + .addToggle(toggle => { + toggle.setValue(this.uriCommand.encode) + .onChange(value => { + this.uriCommand.encode = value; + }) + }) + //https://github.com/phibr0/obsidian-macros/blob/master/src/ui/macroModal.ts#L132 const buttonDiv = contentEl.createDiv({ cls: "URI-flex-center" }); diff --git a/src/main.ts b/src/main.ts index c1679c6..c3e05c0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -92,35 +92,35 @@ export default class URIPlugin extends Plugin { new Notice(`The field ${metadataMatch[1]} does not exist on this file.`) return; } - uriString = replacePlaceholder(uriString, metadataMatch[0], metadataValue); + uriString = replacePlaceholder(command, uriString, metadataMatch[0], metadataValue); metadataMatch = METADATA_REGEX.exec(uriString); } } if (uriString.includes(FILE_NAME_TEMPLATE)) { // base name of file - uriString = replacePlaceholder(uriString, FILE_NAME_TEMPLATE, file.basename); + uriString = replacePlaceholder(command, uriString, FILE_NAME_TEMPLATE, file.basename); } if (uriString.includes(FILE_TEXT_TEMPLATE)) { //entire text of file const fileText = await this.app.vault.read(file); - return replacePlaceholder(uriString, FILE_TEXT_TEMPLATE, fileText); + return replacePlaceholder(command, uriString, FILE_TEXT_TEMPLATE, fileText); } if (uriString.includes(SELECTION_TEMPLATE)) { //current selection - uriString = replacePlaceholder(uriString, SELECTION_TEMPLATE, editor.getSelection()); //currently replaced with empty string if no selection + uriString = replacePlaceholder(command, uriString, SELECTION_TEMPLATE, editor.getSelection()); //currently replaced with empty string if no selection } if (uriString.includes(LINE_TEMPLATE)) { //current line const currentLine = editor.getCursor().line; - uriString = replacePlaceholder(uriString, LINE_TEMPLATE, editor.getLine(currentLine)); + uriString = replacePlaceholder(command, uriString, LINE_TEMPLATE, editor.getLine(currentLine)); } if (uriString.includes(FILE_PATH_TEMPLATE)) { //path inside the vault to the current file - uriString = replacePlaceholder(uriString, FILE_PATH_TEMPLATE, file.path); + uriString = replacePlaceholder(command, uriString, FILE_PATH_TEMPLATE, file.path); } if (uriString.includes(VAULT_NAME_TEMPLATE)) { //name of the current vault - uriString = replacePlaceholder(uriString, VAULT_NAME_TEMPLATE, this.app.vault.getName()); + uriString = replacePlaceholder(command, uriString, VAULT_NAME_TEMPLATE, this.app.vault.getName()); } window.open(uriString); @@ -128,12 +128,14 @@ export default class URIPlugin extends Plugin { new Notice(`Opening ${uriString}`); } } - } -function replacePlaceholder(uriString: string, placeholder: string | RegExp, replacementString: string) { - const encodedReplacement = encodeURIComponent(replacementString); - return uriString.replace(placeholder, encodedReplacement); +function replacePlaceholder(command: URICommand, uriString: string, placeholder: string | RegExp, replacementString: string) { + if (command.encode) { + replacementString = encodeURIComponent(replacementString); + } + + return uriString.replace(placeholder, replacementString); } diff --git a/src/settings.ts b/src/settings.ts index 4517881..3d0776d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,6 +4,7 @@ import URIModal from './URIModal'; export interface URICommand extends Command { URITemplate: string; + encode: boolean; } export interface URIPluginSettings { @@ -42,7 +43,7 @@ export class URISettingTab extends PluginSettingTab { }); this.plugin.settings.URICommands.forEach(command => { - if (command === null) { //this should *not* happen + if (command === null) { //this should *not* happen this.plugin.settings.URICommands.remove(command); console.log("Command was null, removing.") return; @@ -51,7 +52,7 @@ export class URISettingTab extends PluginSettingTab { let iconDiv: HTMLElement; if (command.icon) { //do want the "if null or empty string or undefined or etc" behavior iconDiv = createDiv({ cls: "URI-settings-icon" }); - setIcon(iconDiv, command.icon, 20); + setIcon(iconDiv, command.icon, 20); } @@ -76,7 +77,7 @@ export class URISettingTab extends PluginSettingTab { new URIModal(this.plugin, this, command, true).open() }) }); - + if (command.icon) { setting.nameEl.prepend(iconDiv); }