Finding Back Links that Incorporate Markdown Formatting

The Back Links action that Greg provided as an example of how to use the new linking capability added with version 20 looks for drafts using this code:

// query for other drafts with the link to this one…
let pattern = "[[${title}]]";
let drafts = Draft.query(pattern, “all”, [], [], “modified”, true);

The action finds notes with this structure:

This is a note

But, not with this structure:

This is a note

Or, this structure:

This is a note

I’ve tried a variety of different modifications trying to get the pattern to match a note title that uses Markdown formatting. But, I’ve had no luck.

Can someone please point me in the right direction?

Couldn’t find the action you’re referring to (I know it’s here somewhere, just didn’t have the time to search for it) but I wonder what happens when you swap “displayTitle” in for “title”? Without looking at the script, it’s hard to offer anything else…

Thanks for the reply. Your suggestion unfortunately did not work - or, at least, I couldn’t get it to work.

The Back Links action is in the Action Group “Examples: Cross-Linking” posted by Greg when Version 20 was released.

@Bjb In your copy of the action, a few lines above the pattern line, you should find this one:

let title = draft.displayTitle;

Can you confirm that yours says displayTitle, rather than just title? If yours says just title, then that’s likely the problem.

Explanation: a draft’s displayTitle is a version of its title (first non-blank line) with certain characters removed, including Markdown heading “#” symbols. Thus, the action is searching for other drafts which contain a cross-link to the current draft’s display title, rather than to its entire literal title (which might contain Markdown header syntax etc). That’s why it makes sense to use the displayTitle for the search: because it’s less strict, and more likely to find matching cross-links. Without it (using just the actual title), any other draft would have to cross-link to the full exact title of the current draft, including Markdown headers, or it wouldn’t be found by the action.

This is the code that I think is relevant. Unchanged from what Greg posted.

let f = () => {
let title1 = draft.displayTitle;
let title = title1.substring(2);
if (title.length == 0) { return; }

// query for other drafts with the link to this one...
let pattern = `"[[# ${title}]]"`;
let drafts = Draft.query(pattern, "all", [], [], "modified", true);

The version in the directory has been updated. I believe you had an early example posted during the beta cycle.

I downloaded the new version and still can’t get it to work. It does not find links with this format:

[[# note c]]

It does find links with this format: [[note c]]

Oh. Guess I didn’t really process the thread. Why would you want that? I don’t think I’d change my example for that behavior.

To make that work you would have to either do multiple queries for variants of the title, or query for the title without the brackets, then add logic to look around for the brackets - which could get a bit weird.

What is the use case where you would want your wiki-links or include the Markup?

I think I finally figured out the problem. I don’t actually want the Markdown in the link, but…

A different action in the Action Group you provided (Insert Link to Draft) includes the Markdown in links that it creates.

I’ve been trying to figure out how to find them, when I should have been trying to figure out how to modify the Insert Link to Draft action to prevent them from being formed in the first place.

That’s the right approach. In the “Insert Link to Draft” action, change this line:

editor.setSelectedText([[${d.title}]]);

to this:

editor.setSelectedText([[${d.displayTitle}]]);

The new version of Insert Link to Draft removes the Markdown from the link, so all is well.

Helps to try to solve the right problem.

Thanks for the assistance.

FYI, I’ve since found a better way to navigate back links is to simply use searches to find drafts with contain the string. This Search for Back Links example constructs a URL to open quick search with a query like [[Title of Current Draft]].

1 Like