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

This commit is contained in:
zadam 2020-02-09 10:45:07 +01:00
parent 9eab3026bb
commit 88bd65c679
4 changed files with 43 additions and 7 deletions

View File

@ -811,8 +811,10 @@ class Note extends Entity {
FROM attributes FROM attributes
WHERE noteId = ? AND WHERE noteId = ? AND
isDeleted = 0 AND isDeleted = 0 AND
type = 'relation' AND ((type = 'relation' AND
name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink')`, [this.noteId]); name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'))
OR
(type = 'label' AND name = 'externalLink'))`, [this.noteId]);
} }
/** /**

View File

@ -71,7 +71,19 @@ class AttributesWidget extends StandardWidget {
async renderAttributes(attributes, $container) { async renderAttributes(attributes, $container) {
for (const attribute of attributes) { for (const attribute of attributes) {
if (attribute.type === 'label') { 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') { } else if (attribute.type === 'relation') {
if (attribute.value) { if (attribute.value) {
$container.append('@' + attribute.name + "="); $container.append('@' + attribute.name + "=");

View File

@ -152,6 +152,11 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
continue; continue;
} }
if (attr.type === 'label' && attr.name === 'externalLink') {
// also created automatically
continue;
}
if (attr.type === 'relation') { if (attr.type === 'relation') {
attr.value = getNewNoteId(attr.value); attr.value = getNewNoteId(attr.value);
} }

View File

@ -242,6 +242,20 @@ function findInternalLinks(content, foundLinks) {
return content.replace(/href="[^"]*#root/g, 'href="#root'); 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) { function findIncludeNoteLinks(content, foundLinks) {
const re = /<section class="include-note" data-note-id="([a-zA-Z0-9]+)">/g; const re = /<section class="include-note" data-note-id="([a-zA-Z0-9]+)">/g;
let match; let match;
@ -281,6 +295,7 @@ async function saveLinks(note, content) {
if (note.type === 'text') { if (note.type === 'text') {
content = findImageLinks(content, foundLinks); content = findImageLinks(content, foundLinks);
content = findInternalLinks(content, foundLinks); content = findInternalLinks(content, foundLinks);
content = findExternalLinks(content, foundLinks);
content = findIncludeNoteLinks(content, foundLinks); content = findIncludeNoteLinks(content, foundLinks);
} }
else if (note.type === 'relation-map') { else if (note.type === 'relation-map') {
@ -293,9 +308,11 @@ async function saveLinks(note, content) {
const existingLinks = await note.getLinks(); const existingLinks = await note.getLinks();
for (const foundLink of foundLinks) { for (const foundLink of foundLinks) {
const targetNote = await repository.getNote(foundLink.value); if (foundLink.name !== 'externalLink') {
if (!targetNote || targetNote.isDeleted) { const targetNote = await repository.getNote(foundLink.value);
continue; if (!targetNote || targetNote.isDeleted) {
continue;
}
} }
const existingLink = existingLinks.find(existingLink => const existingLink = existingLinks.find(existingLink =>
@ -305,7 +322,7 @@ async function saveLinks(note, content) {
if (!existingLink) { if (!existingLink) {
await new Attribute({ await new Attribute({
noteId: note.noteId, noteId: note.noteId,
type: 'relation', type: foundLink.name === 'externalLink' ? 'label' : 'relation',
name: foundLink.name, name: foundLink.name,
value: foundLink.value, value: foundLink.value,
}).save(); }).save();