AutoLink Plugin

name

AutoLink

description

A plugin to creates links your existing notes automatically.

icon

insert_link

setting

Min Page Name Length

instructions

The plugin automatically creates links for words in selected text that match your existing notes. To use this feature, simply select the desired text and click on the AutoLink option in the floating toolbar:



☕ If you like my work, you can buy me a coffee!


linkInstructions & Demo:

The plugin automatically creates links for words in selected text that match your existing notes. To use this feature, simply select the desired text and click on the AutoLink option in the floating toolbar:



linkCode:

{
replaceText: async function (app, text) {
const noteHandle = { uuid: app.context.noteUUID };
const pages = await this._getSortedPages(app);
const textWithFormatting = app.context.selectionContent;
app.context.replaceSelection(await this._textAutolink(app, textWithFormatting, pages));
return null;
},
async _textAutolink(app, text, pages) {
try {
text = '\n' + text + '\n';
pages.forEach(page => {
const pageName = this._escapeRegExp(page.name);
let regex = new RegExp(`(^|\\s|,|\\*\\*||~~)(${pageName})((,|!|\\.|\\*\\*|<\\/mark>|~~)*?)($|\\s|\\n)`, 'gi');
text = text.replace(regex, `$1[$2](https://www.amplenote.com/notes/${page.uuid})$3$5`);
});
return text.trim();
} catch (e) {
app.alert('Failed _textAutolink - ' + e);
}
},
async _getSortedPages(app) {
try {
const allPages = await app.filterNotes({});
const nonEmptyPages = allPages.filter(page => page.name != null && typeof page.name === 'string' && page.name.trim() !== '');
 
app.settings["Min Page Name Length"] = app.settings["Min Page Name Length"] || 3;
 
const filteredPages = nonEmptyPages.filter(page => page.name.length >= app.settings["Min Page Name Length"]);
 
const sortedPages = filteredPages.sort((a, b) => {
if (a.name > b.name) {
return -1;
}
if (a.name < b.name) {
return 1;
}
return 0;
});
return sortedPages;
} catch (e) {
app.alert('Failed _getSortedPages - ' + e);
}
},
_escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
}