Trigger the new link behaviour programmatically?

I’m excited about the new-in-20.0 link behaviour, including inter-document links using just the titles of drafts.

Is it possible to trigger this parsing/behaviour programmatically? For example, to act upon a selected portion of a draft which contains, say, “[[Some Title]]”, and have Drafts do what it would do if the user tapped on that text (i.e. go to the draft with that title, creating it if necessary)?

The use case I have in mind is creating a keyboard shortcut whereby the user, whilst editing, could jump to the draft she had just typed out a link to. This would allow rapid creation and traversal of an interconnected collection of documents without taking hands off the keyboard.

Should not be too hard to put that together. The Transclude examples I posted have logic to find a linked draft. Snippet from those examples:

let findLinkedDraft = (title) => {
	let found = Draft.query(title, "all", [], [], "modified", true, false);
	if (found.length == 0) {
		return null;
	}
	for (let d of found) {
		if (d.content.trim().length > 0) {
			let firstLine = d.content.trim().split("\n")[0];
			if (firstLine.toLowerCase().includes(title.toLowerCase())) {
				return d;
			}
		}
	}
}

You would need what logic you think is appropriate to scan for the [[title link]] - maybe matching on the current line? If you separate the title with a regex capture group, feed it to the above function to find the draft you could load it in the editor via script.

Alternately, you could construct the URL. A [[Title of Draft]] link would open this URL:

draft://open?title=Title%20of%20Draft&allowCreate=true

So if you had title separate, you could construct and open the appropriate URL to mimic tapping on the link.

3 Likes

I wrote the action I discussed above, by the way; it’s here: Keyboard-navigation of [[cross-links]]

2 Likes