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
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]);
}
/**

View File

@ -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 + "=");

View File

@ -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);
}

View File

@ -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();