I like using links to Markdown files, and there are already several existing solutions, each with their pros and cons:
- Wikilinks, like [[Note_Name]], can easily break if the note is moved or renamed. Additionally, they don’t work across DEVONthink databases or in apps like Apple Notes.
- File paths can change.
- Hookmark links, like hook://file/pxbWfCG2u?p=cnRmLzEx&n=External%20Sources.rtf, and DEVONthink links, such as x-devonthink-item://4F70343B-1153-4DED-97F2-F1388A754AB5, are much too long and tied to their respective apps. This makes them less flexible for workflows involving other tools.
I wanted links in the following format:
http://id/1A22832D
The links should be as short as possible while still remaining unique. Each link would point to a key in the frontmatter of a Markdown file, like this:
---
title: Test Note
aliases: 1A22832D
---
The Current Solution
At first glance, this solution may seem like a Rube Goldberg machine, but once it’s set up, it’s simple to use.
Generating the ID
Coming up with a unique alias for every note is cumbersome. While DEVONthink’s 32-character UUID is too long, the first 8 characters are unique enough for my needs.
Here’s a quick calculation:
Generate 10,000 random 8-character IDs from the hex set {0-9, a-f}.
The total possible unique IDs: 16^8 = 4,294,967,296. This means about 2 duplicates in 10,000 IDs, which is acceptable for my use case.
I wrote a script that:
1. Takes a DEVONthink UUID,
2. Shortens it to 8 characters,
3. Copies it to the clipboard.
Here’s how I use it in practice:
1. I copy a DEVONthink link.
2. I run my script (dlink) in the WARP terminal’s visor mode, and the short UUID is ready to paste.
3. I add the shortened UUID to the frontmatter of my notes.
With this setup, I can:
- Search for the note in DEVONthink: aliases BEFORE 1A22832D
- It recognises the key/search term aliases in DEVONthink: aliases==1A22832D
- If I copy the ID to the alias field in the Inspector, I can use Wikilinks: [[1A22832D]].
A search with a URL looks like this
x-devonthink://search?query=aliases%20BEFORE%201A22832D
This makes it easy to create URLs for other apps like iA Writer, nvUltra, or Apple Shortcuts.
Redirecting the Link on macOS
Now, I wanted http://id/1A22832D to redirect to x-devonthink://search?query=aliases%20BEFORE%201A22832D.
I should be able to click on the short link in any note-taking app, and the search would open.
On macOS, this is possible with two apps: Redirect Web for Safari and Link Unshortener (by the developer of StopTheMadness). Both apps work well for this purpose.
For Link Unshortener, I use the following regex in the Redirects settings:
- Match: /http://id/(.+)/
- Replace: x-devonthink://search?query=aliases%20BEFORE%20$1
Where $1 is replaced by the ID.
Redirecting the Link on macOS and iOS
The apps Redirect Web for Safari and StopTheMadness also work on iOS, creating redirects to usual web-URLs. However, they cannot handle app-specific URLs like devonthink://.
To solve this, I built a plugin for a self-hosted URL shortener, YOURLS. YOURLS can be installed via Docker on a Synology NAS. For example:
https://yourls.ralf.example.com/brett redirects to https://brettterpstra.com.
Normally, YOURLS creates a 1:1 mapping from the short URL to the target URL. My plugin, however, carries over the ID.
Now, https://yourls.ralf.example.com/1A22832D resolves to:
x-devonthink://search?query=aliases%20BEFORE%201A22832D
By combining YOURLS with Redirect Web for Safari, I can go from:
http://id/1A22832D → YOURLS → DEVONthink search.
The Tools You Need
To make this work, I use:
1. A script to generate the unique IDs.
2. Redirect Web for Safari for macOS/iOS or Link Unshortener for Mac only.
3. A self-hosted YOURLS instance with my plugin.
4. A search program that supports URLs, such as Apple Shortcuts, nvUltra, or DEVONthink.
The short scripts I’ve written are available to share. Ideas welcome!
By the way, I’ve been using the solution on the Mac for a year now. Only the extension with Yourls was added today to get it work on the iPhone (again).