Andrea Privitera

Creating Wikipedia-style external links in Obsidian with Templater

Nov 27, 2023 | obsidian templater javascript pkms

When I write a personal note in my Obsidian vault, I like to include an external links section at the bottom of the note. My inspiration for this format was, obviously, Wikipedia. I really like the format of links at the bottom of Wikipedia pages, which includes both the external link and a wikilink to the larger site/service that hosts the page.

A screenshot of external links on Wikipedia

It makes sense to the way I organize notes, because it allows me to track which services, people or organizations are part of a certain social media.

A screenshot of external links in my vault

There is only one issue with this solution: it takes quite a lot of time to type the external links and then the internal link. To make the process quicker, I wrote this script, which I then activate in my Obsidian vault using Templater.

<%* // Templater syntax
async function links() {
	let linkOutput, siteTitle, siteUrl, siteOrSocial;
	const noteTitle = tp.file.title; // Templater API
	const clipboard = tp.system.clipboard; // Templater API
	const suggester = tp.system.suggester; // Templater API
	const prompt = tp.system.prompt; // Templater API

	siteUrl = await prompt("What is the link's url?", clipboard);
	siteTitle = await prompt("What is the name of the site?");
	let options = [{ title: "Single Site", formElement: `[${siteTitle}](${siteUrl})`}, {title: "Page on Site", formElement: `[${noteTitle}](${siteUrl}) on [[${siteTitle}]]`}];
	linkOutput = await suggester(options.map((x) => x.title), options.map((x) => x.formElement), true, "Is this a link for a single site or for a page in a bigger site?");
	return linkOutput;
}
let string = await links();
%> // Templater syntax
- <%* tR += string %> // Templater syntax

Here is the script in action.

A recording of the script in action

Feel free to grab this script and use it for your own vault!


Comments

Leave a comment by email. I'll publish the most interesting ones in this section or in another post.