Skip to content

Commit

Permalink
Added encode URL toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
kzhovn committed May 20, 2022
1 parent 7d1062c commit 622cfab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/URIModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class URIModal extends Modal {
name: "",
id: "",
URITemplate: "",
encode: true,
}
} else {
this.uriCommand = command;
Expand Down Expand Up @@ -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" });
Expand Down
24 changes: 13 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,48 +92,50 @@ 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);
if (this.settings.notification === true) {
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);
}


Expand Down
7 changes: 4 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import URIModal from './URIModal';

export interface URICommand extends Command {
URITemplate: string;
encode: boolean;
}

export interface URIPluginSettings {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}


Expand All @@ -76,7 +77,7 @@ export class URISettingTab extends PluginSettingTab {
new URIModal(this.plugin, this, command, true).open()
})
});

if (command.icon) {
setting.nameEl.prepend(iconDiv);
}
Expand Down

0 comments on commit 622cfab

Please sign in to comment.