As a proof of concept I present to you a template which scans through all aliases, and if the selected text starts with the selected text, it inserts an link to that file using the alias. If multiple options are present it allows you to choose which file you want to link to.
Currently it does not match against actual file name, it’s just for the aliases. It also lists the full file path in parentheses in case you’ve got multiple notes with the same name. This could of course be adapted to your liking.
To use this template, insert the code in the section below into a template in your template folder.
The actual template
Insert the following into a template in your template folder:
<%*
const dv = app.plugins.plugins.dataview.api
// Bail out if nothing is selected
const selection = tp.file.selection()
if ( selection == "" ) {
alert("Please select some alias text before inserting template")
return
}
// Build the two lists, file path vs alias,
const pathToAliases = {} // file.path to alias(es)
const aliasToPaths = {} // alias to file.path(s)
await dv.pages()
.where(p => p.file.aliases.length > 0)
.forEach(p => {
pathToAliases[p.file.path] = p.file.aliases
p.file.aliases.forEach( a => {
if ( !(a in aliasToPaths ) )
aliasToPaths[a] = []
aliasToPaths[a].push(p.file.path)
})
})
// Look through the alias, and pick any alias
// which starts with the pre-selected text
// Can be changed by changing the `k.startsWith()`
const suggestions = []
Object.keys(aliasToPaths)
.filter(k => k.startsWith(selection))
.forEach(a => {
aliasToPaths[a].forEach( p => {
suggestions.push([a, p, a + " (" + p + ")"])
} )
})
// Build a suitable link based on alias matching
// the selected text. If only suggestion is available
// return directly, and ask the user if multiple aliases
// are possible
if ( suggestions.length == 0 ) {
// No matching aliases found
alert("No matching aliases was found. Will return selected text")
tR = selection
} else if ( suggestions.length == 1 ) {
// Single suggestion found, return directly
tR += dv.fileLink(suggestions[0][1], false, suggestions[0][0])
} else {
// Multiple suggestion, ask user which to use
const response = await tp.system.suggester(t => t[2], suggestions, false, "Choose alias")
if ( !response )
tR = selection
else
tR = dv.fileLink(response[1], false, response[0])
}
_%>
A short explanation of the template
The template starts by checking that you’ve actually selected some text, and gives a warning if not, and bails out of any further processing. Keeping the selection as it was.
The template then loops through all pages to gather all the aliases, and restructure them into two variables, pathToAliases
and aliasToPaths
. The first is ignored in this settings, but could possible be useful in other settings. The second is the one we use to search for possible matches in.
And that search is the next step, where we try to match the alias against the selection
text. I’ve opted for using k.startsWith()
which matches the selection against the start of the alias key (k
). This can of course be changed to whatever variant of matching you want. Like in-case sensitive matches, or whether the selection is just somewhere within the alias, and so on.
To differentiate between re-used aliases, the full file path of the file holding the aliases is inserted into the suggestions
list. This could possibly be ignored, or used in a secondary suggester if needed. Or one could adapt this to show only the file name or similar stuff. For this proof of concept I included the full file path.
After building the suggestions
list, it’s checked against the length, as if there are no matches, it gives a warning and returns the selection text. If there is only match in the aliases, it returns the proper link with the alias. And finally if multiple matches was found, it asks the user which of the matches to use before inserting that link into the document.
After inserting the template, go to Settings > Templater > Template Hotkeys, and hit the Add new hotkey for template. Select your newly created template in the first box, and hit the plus icon, which sends you to the hotkey settings. Select your template, and hit the plus icon and select a key combination of your choice.
Now you should be ready to select thealias
, and hit your key combination and let the magic unfold.