I want to paste links to local files into a note. Unfortunately my filenames contain a lot of special characters like #, (, ), … which seem to not work in URIs, so escape characters are required, as described here:
Because I have to insert a lot of links to local files, I have to do a lot of manual work to finde & replace all special characters by their corresponding escape-characters.
Now I wonder, if there is a way to be able to do that work automatically.
Things I have tried
There is this plugin “File path to URI”, which apparently is not under development anymore. This plugin could be used to automatically replace these characters, but as I don’t have the skills to modify that plugin, I don’t have any idea to proceed here…
Here’s a completely not working script, that throws a lot of errors in the terminal … as I said, I don’t have a lot of coding skills…
<%*
console.log(tp.system.clipboard())
let text = tp.system.clipboard()
console.log(text)
if (text.includes("#")) {
text = text.replace(/#/g, "%23")
}
console.log(text)
%>
could anybody please help me correcting that script?
For now, I can only help with the structure of the Templater code. I cannot make time to look into all relevant character encodings or if they need double encoding, or ask about your operating system for treatment of slashes in the path, I’m afraid.
So your Templater script you can bind a hotkey to can look like this:
In my trimmed example the clipboard = clipboard.replace(/\s-\s/gi, " – "); replaces dashes to n-dashes (when a dash is not a hyphen).
You’ll need to populate all your rules here. The stupidest ChatGPT bot can help with this. Just make sure you prompt it with your exact requirements (specify OS for correct path).
Feed it this snippet as well so the bots knows what to build its answers around.
When you fire the script, the changed content gets pasted in the editor with tR +=.
to automatically fill in the URI-command, after I pressed “Strg + K” (= Insert Markdown link).
Enhancement of that script:
Now I’m wondering if I could combine both hotkeys, the “Strg + K”-Hotkey to insert a Markdown link on a selected string in Obsidian AND the script above.
… That’s a very strange behaviour, I don’t have an explanation for, because it looks like the string “tp.file.selection()” has been saved in the variable “clipboard”…
I’m a little vague on what you want to accomplish but one script can handle both clipboard and selection (not sure what you’d want to do with combining them).
Thanks holroy, it works.
I didn’t know that function. In my opinion, the resulting link, produced by the encodeURI() function is not as good readable as the one, that will be created with my manual script above, so I’ll stick to that.