Skip to content

Build your personal knowledge base with Trilium Notes

external links are also parsed and label is created for them, closes #851

zadam88bd65c67986
diff --git a/src/entities/note.js b/src/entities/note.js
index 8a7cf219ba..3336f377a3 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 f8fe3c9b03..912e950a04 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(
+ $('<a>')
+ .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 8b0b58ce13..748856a554 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 a64012714d..ffafb4bb68 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 = /<section class="include-note" data-note-id="([a-zA-Z0-9]+)">/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();