From ca0f954b149d19119a0af0322c43446f3ae9f9e6 Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Sun, 9 Feb 2025 00:21:01 +0100 Subject: [PATCH] Add autolink support --- priv/static/js/dom.js | 12 +++++++++++- priv/static/js/jenot.js | 8 +++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/priv/static/js/dom.js b/priv/static/js/dom.js index 64fc95c..75d392a 100644 --- a/priv/static/js/dom.js +++ b/priv/static/js/dom.js @@ -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 '' + url + ""; + }); +} + export function renderText(text) { - const content = text.replace(/(?:\r\n|\r|\n)/g, "
") + "
"; + const content = linkify(text).replace(/(?:\r\n|\r|\n)/g, "
") + "
"; return stringToHTML(content, true); } diff --git a/priv/static/js/jenot.js b/priv/static/js/jenot.js index bf6cc25..be51c06 100644 --- a/priv/static/js/jenot.js +++ b/priv/static/js/jenot.js @@ -253,9 +253,11 @@ function renderNote(note) { } container.addEventListener("click", async (e) => { - const note = await Notes.get(container.id); - editNote.load(structuredClone(note)); - editNoteDialog.showModal(); + if (e.target.tagName !== "A") { + const note = await Notes.get(container.id); + editNote.load(structuredClone(note)); + editNoteDialog.showModal(); + } }); return container;