From 88bd65c6798609a39206722305fab4f8d91d618b Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 9 Feb 2020 10:45:07 +0100 Subject: [PATCH] external links are also parsed and label is created for them, closes #851 --- src/entities/note.js | 6 +++-- src/public/javascripts/widgets/attributes.js | 14 ++++++++++- src/services/import/tar.js | 5 ++++ src/services/notes.js | 25 ++++++++++++++++---- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index 8a7cf219b..3336f377a 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -811,8 +811,10 @@ class Note extends Entity { FROM attributes WHERE noteId = ? AND isDeleted = 0 AND - type = 'relation' AND - name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink')`, [this.noteId]); + ((type = 'relation' AND + name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink')) + OR + (type = 'label' AND name = 'externalLink'))`, [this.noteId]); } /** diff --git a/src/public/javascripts/widgets/attributes.js b/src/public/javascripts/widgets/attributes.js index f8fe3c9b0..912e950a0 100644 --- a/src/public/javascripts/widgets/attributes.js +++ b/src/public/javascripts/widgets/attributes.js @@ -71,7 +71,19 @@ class AttributesWidget extends StandardWidget { async renderAttributes(attributes, $container) { for (const attribute of attributes) { if (attribute.type === 'label') { - $container.append(utils.formatLabel(attribute) + " "); + if (attribute.name === 'externalLink') { + $container.append('@' + attribute.name + "="); + $container.append( + $('') + .text(attribute.value) + .attr('href', attribute.value) + .addClass('external') + ); + $container.append(" "); + } + else { + $container.append(utils.formatLabel(attribute) + " "); + } } else if (attribute.type === 'relation') { if (attribute.value) { $container.append('@' + attribute.name + "="); diff --git a/src/services/import/tar.js b/src/services/import/tar.js index 8b0b58ce1..748856a55 100644 --- a/src/services/import/tar.js +++ b/src/services/import/tar.js @@ -152,6 +152,11 @@ async function importTar(taskContext, fileBuffer, importRootNote) { continue; } + if (attr.type === 'label' && attr.name === 'externalLink') { + // also created automatically + continue; + } + if (attr.type === 'relation') { attr.value = getNewNoteId(attr.value); } diff --git a/src/services/notes.js b/src/services/notes.js index a64012714..ffafb4bb6 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -242,6 +242,20 @@ function findInternalLinks(content, foundLinks) { return content.replace(/href="[^"]*#root/g, 'href="#root'); } +function findExternalLinks(content, foundLinks) { + const re = /href="([a-zA-Z]+:\/\/[^"]*)"/g; + let match; + + while (match = re.exec(content)) { + foundLinks.push({ + name: 'externalLink', + value: match[1] + }); + } + + return content; +} + function findIncludeNoteLinks(content, foundLinks) { const re = /
/g; let match; @@ -281,6 +295,7 @@ async function saveLinks(note, content) { if (note.type === 'text') { content = findImageLinks(content, foundLinks); content = findInternalLinks(content, foundLinks); + content = findExternalLinks(content, foundLinks); content = findIncludeNoteLinks(content, foundLinks); } else if (note.type === 'relation-map') { @@ -293,9 +308,11 @@ async function saveLinks(note, content) { const existingLinks = await note.getLinks(); for (const foundLink of foundLinks) { - const targetNote = await repository.getNote(foundLink.value); - if (!targetNote || targetNote.isDeleted) { - continue; + if (foundLink.name !== 'externalLink') { + const targetNote = await repository.getNote(foundLink.value); + if (!targetNote || targetNote.isDeleted) { + continue; + } } const existingLink = existingLinks.find(existingLink => @@ -305,7 +322,7 @@ async function saveLinks(note, content) { if (!existingLink) { await new Attribute({ noteId: note.noteId, - type: 'relation', + type: foundLink.name === 'externalLink' ? 'label' : 'relation', name: foundLink.name, value: foundLink.value, }).save();