I’m searching for the capability of pasting a wikilink into a selection, akin to the “paste URL into selection” plugin.
This is the idea. Suppose I have this text in the clipboard: [[This is a wikilink to a note]] and that in the editor I have the following text:
- See here for details
I want to select here, hit Ctrl-V and paste the wikilink replacing the word here, but keeping that word as alias, so the final result is:
- See [[This is a wikilink to a note|here]] for details
The usefulness for this is when taking notes from PDFs (using PDF++ for example). Selecting text in the PDF can automatically copy into the clipboard a wikilink to the selected text in the pdf, but I want to replace the “alias part” (which can contain the pdf page, or the selected text, or other content depending on how PDF++ is configured), with the selected text in my markdown notes file, when pasting.
I don’t know if perhaps the plugin obsidian-paste-mode can be tweaked to do that. I was unable to.
You could for sure use Templater to build that kind of logic. Examples on how can be found in this forum of you search around. It’ll involve tp.file.selection and tp.system.clipboard, IIRC.
I solved the problem by writing the following template:
<%*
let s = await tp.file.selection();
let c = await tp.system.clipboard();
let match = c.match(/\[\[(.*?)\|.*?\]\]/);
if (!match) {
match = c.match(/!?\[\[(.*?)\]\]/);
}
if (match && s && c) {
tR += `[[${match[1]}|${s}]]`;
} else {
tR+= c;
}
%>
and assigning a hotkey to it (even Ctrl+v works)
Update, with this little addition, it also works as paste-url-into-selection, i.e: if the clipboard contains an url, the selection is replaced by [selection](url). If there is no selection in the editor, the clipboard is pasted verbatim.
<%*
let s = await tp.file.selection();
let c = await tp.system.clipboard();
let match = c.match(/\[\[(.*?)\|.*?\]\]/);
if (!match) {
match = c.match(/!?\[\[(.*?)\]\]/);
}
if (match && s && c) {
tR += `[[${match[1]}|${s}]]`;
} else if (s && c.startsWith("http")) {
tR += `[${s}](${c})`;
} else {
tR += c;
}
%>