Add autolink support

This commit is contained in:
Adrian Gruntkowski 2025-02-09 00:21:01 +01:00
parent 77a9f148cf
commit ca0f954b14
2 changed files with 16 additions and 4 deletions

View file

@ -13,7 +13,17 @@ export function html(strings, ...values) {
return stringToHTML(String.raw({ raw: strings }));
}
// Taken from https://stackoverflow.com/a/8943487
const urlRegex =
/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
function linkify(text) {
return text.replace(urlRegex, function (url) {
return '<a href="' + url + '" target="_blank">' + url + "</a>";
});
}
export function renderText(text) {
const content = text.replace(/(?:\r\n|\r|\n)/g, "<br>") + "<br>";
const content = linkify(text).replace(/(?:\r\n|\r|\n)/g, "<br>") + "<br>";
return stringToHTML(content, true);
}

View file

@ -253,9 +253,11 @@ function renderNote(note) {
}
container.addEventListener("click", async (e) => {
if (e.target.tagName !== "A") {
const note = await Notes.get(container.id);
editNote.load(structuredClone(note));
editNoteDialog.showModal();
}
});
return container;