2019-06-03 22:55:59 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-05-31 23:38:47 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2021-12-01 23:12:54 +01:00
|
|
|
const { JSDOM } = require("jsdom");
|
2019-06-03 22:55:59 +02:00
|
|
|
|
2021-09-17 22:34:23 +02:00
|
|
|
function buildDescendantCountMap() {
|
|
|
|
const noteIdToCountMap = {};
|
|
|
|
|
|
|
|
function getCount(noteId) {
|
|
|
|
if (!(noteId in noteIdToCountMap)) {
|
|
|
|
const note = becca.getNote(noteId);
|
|
|
|
|
|
|
|
noteIdToCountMap[noteId] = note.children.length;
|
|
|
|
|
|
|
|
for (const child of note.children) {
|
|
|
|
noteIdToCountMap[noteId] += getCount(child.noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return noteIdToCountMap[noteId];
|
|
|
|
}
|
|
|
|
|
|
|
|
getCount('root');
|
|
|
|
|
|
|
|
return noteIdToCountMap;
|
|
|
|
}
|
|
|
|
|
2021-10-21 22:52:52 +02:00
|
|
|
function getNeighbors(note, depth) {
|
|
|
|
if (depth === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const retNoteIds = [];
|
|
|
|
|
2021-11-01 21:11:16 +01:00
|
|
|
function isIgnoredRelation(relation) {
|
|
|
|
return ['relationMapLink', 'template', 'image'].includes(relation.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward links
|
2021-10-21 22:52:52 +02:00
|
|
|
for (const relation of note.getRelations()) {
|
2021-11-01 21:11:16 +01:00
|
|
|
if (isIgnoredRelation(relation)) {
|
2021-10-21 22:52:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetNote = relation.getTargetNote();
|
|
|
|
retNoteIds.push(targetNote.noteId);
|
|
|
|
|
|
|
|
for (const noteId of getNeighbors(targetNote, depth - 1)) {
|
|
|
|
retNoteIds.push(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:11:16 +01:00
|
|
|
// backward links
|
|
|
|
for (const relation of note.getTargetRelations()) {
|
|
|
|
if (isIgnoredRelation(relation)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceNote = relation.getNote();
|
|
|
|
retNoteIds.push(sourceNote.noteId);
|
|
|
|
|
|
|
|
for (const noteId of getNeighbors(sourceNote, depth - 1)) {
|
|
|
|
retNoteIds.push(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 22:52:52 +02:00
|
|
|
return retNoteIds;
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:02:38 +02:00
|
|
|
function getLinkMap(req) {
|
2021-09-20 23:04:41 +02:00
|
|
|
const mapRootNote = becca.getNote(req.params.noteId);
|
2021-10-03 11:04:56 +02:00
|
|
|
// if the map root itself has ignore (journal typically) then there wouldn't be anything to display so
|
|
|
|
// we'll just ignore it
|
|
|
|
const ignoreExcludeFromNoteMap = mapRootNote.hasLabel('excludeFromNoteMap');
|
2021-09-20 23:04:41 +02:00
|
|
|
|
2021-10-21 22:52:52 +02:00
|
|
|
const noteIds = new Set(
|
|
|
|
mapRootNote.getSubtreeNotes(false)
|
|
|
|
.filter(note => ignoreExcludeFromNoteMap || !note.hasLabel('excludeFromNoteMap'))
|
|
|
|
.map(note => note.noteId)
|
|
|
|
);
|
2021-09-20 20:02:23 +02:00
|
|
|
|
2021-10-21 22:52:52 +02:00
|
|
|
for (const noteId of getNeighbors(mapRootNote, 3)) {
|
|
|
|
noteIds.add(noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const notes = Array.from(noteIds).map(noteId => {
|
|
|
|
const note = becca.getNote(noteId);
|
|
|
|
|
|
|
|
return [
|
2021-09-20 20:02:23 +02:00
|
|
|
note.noteId,
|
|
|
|
note.isContentAvailable() ? note.title : '[protected]',
|
|
|
|
note.type
|
2021-10-21 22:52:52 +02:00
|
|
|
];
|
|
|
|
});
|
2021-09-20 20:02:23 +02:00
|
|
|
|
|
|
|
const links = Object.values(becca.attributes).filter(rel => {
|
2021-09-17 22:34:23 +02:00
|
|
|
if (rel.type !== 'relation' || rel.name === 'relationMapLink' || rel.name === 'template') {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 20:02:23 +02:00
|
|
|
else if (!noteIds.has(rel.noteId) || !noteIds.has(rel.value)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-17 22:34:23 +02:00
|
|
|
else if (rel.name === 'imageLink') {
|
|
|
|
const parentNote = becca.getNote(rel.noteId);
|
|
|
|
|
|
|
|
return !parentNote.getChildNotes().find(childNote => childNote.noteId === rel.value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-20 20:02:23 +02:00
|
|
|
})
|
|
|
|
.map(rel => ({
|
2021-09-17 22:34:23 +02:00
|
|
|
id: rel.noteId + "-" + rel.name + "-" + rel.value,
|
|
|
|
sourceNoteId: rel.noteId,
|
|
|
|
targetNoteId: rel.value,
|
|
|
|
name: rel.name
|
|
|
|
}));
|
|
|
|
|
2021-09-20 20:02:23 +02:00
|
|
|
return {
|
|
|
|
notes: notes,
|
|
|
|
noteIdToDescendantCountMap: buildDescendantCountMap(),
|
|
|
|
links: links
|
|
|
|
};
|
|
|
|
}
|
2021-09-17 22:34:23 +02:00
|
|
|
|
2021-09-22 23:02:38 +02:00
|
|
|
function getTreeMap(req) {
|
2021-09-20 23:04:41 +02:00
|
|
|
const mapRootNote = becca.getNote(req.params.noteId);
|
2021-09-29 12:39:28 +02:00
|
|
|
// if the map root itself has ignore (journal typically) then there wouldn't be anything to display so
|
|
|
|
// we'll just ignore it
|
2021-10-03 11:04:56 +02:00
|
|
|
const ignoreExcludeFromNoteMap = mapRootNote.hasLabel('excludeFromNoteMap');
|
2021-09-17 22:34:23 +02:00
|
|
|
const noteIds = new Set();
|
|
|
|
|
2021-09-20 23:04:41 +02:00
|
|
|
const notes = mapRootNote.getSubtreeNotes(false)
|
2021-10-03 11:04:56 +02:00
|
|
|
.filter(note => ignoreExcludeFromNoteMap || !note.hasLabel('excludeFromNoteMap'))
|
2021-09-21 22:45:06 +02:00
|
|
|
.filter(note => {
|
|
|
|
if (note.type !== 'image' || note.getChildNotes().length > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageLinkRelation = note.getTargetRelations().find(rel => rel.name === 'imageLink');
|
|
|
|
|
|
|
|
if (!imageLinkRelation) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !note.getParentNotes().find(parentNote => parentNote.noteId === imageLinkRelation.noteId);
|
|
|
|
})
|
2021-11-01 21:11:16 +01:00
|
|
|
.concat(...mapRootNote.getParentNotes())
|
2021-09-17 22:34:23 +02:00
|
|
|
.map(note => [
|
|
|
|
note.noteId,
|
|
|
|
note.isContentAvailable() ? note.title : '[protected]',
|
|
|
|
note.type
|
|
|
|
]);
|
|
|
|
|
|
|
|
notes.forEach(([noteId]) => noteIds.add(noteId));
|
|
|
|
|
2021-09-20 20:02:23 +02:00
|
|
|
const links = [];
|
|
|
|
|
2021-09-17 22:34:23 +02:00
|
|
|
for (const branch of Object.values(becca.branches)) {
|
|
|
|
if (!noteIds.has(branch.parentNoteId) || !noteIds.has(branch.noteId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
links.push({
|
|
|
|
id: branch.branchId,
|
|
|
|
sourceNoteId: branch.parentNoteId,
|
2021-09-21 22:45:06 +02:00
|
|
|
targetNoteId: branch.noteId
|
2021-09-17 22:34:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
notes: notes,
|
|
|
|
noteIdToDescendantCountMap: buildDescendantCountMap(),
|
|
|
|
links: links
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:12:54 +01:00
|
|
|
function removeImages(document) {
|
|
|
|
const images = document.getElementsByTagName('img');
|
|
|
|
while (images.length > 0) {
|
|
|
|
images[0].parentNode.removeChild(images[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBacklinks(req) {
|
|
|
|
const {noteId} = req.params;
|
|
|
|
const note = becca.getNote(noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return [404, `Note ${noteId} was not found`];
|
|
|
|
}
|
|
|
|
|
|
|
|
let backlinks = note.getTargetRelations();
|
|
|
|
|
|
|
|
if (backlinks.length > 50) {
|
|
|
|
backlinks = backlinks.slice(0, 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
return backlinks.map(backlink => {
|
|
|
|
const sourceNote = backlink.note;
|
|
|
|
|
|
|
|
const html = sourceNote.getContent();
|
|
|
|
const dom = new JSDOM(html);
|
|
|
|
|
|
|
|
const excerpts = [];
|
|
|
|
|
|
|
|
const document = dom.window.document;
|
|
|
|
|
|
|
|
removeImages(document);
|
|
|
|
|
|
|
|
for (const linkEl of document.querySelectorAll("a")) {
|
|
|
|
const href = linkEl.getAttribute("href");
|
|
|
|
|
|
|
|
if (!href || !href.includes(noteId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkEl.style.fontWeight = "bold";
|
|
|
|
linkEl.style.backgroundColor = "yellow";
|
|
|
|
|
|
|
|
const LIMIT = 200;
|
|
|
|
let centerEl = linkEl;
|
|
|
|
|
|
|
|
while (centerEl.tagName !== 'BODY' && centerEl.parentElement.textContent.length < LIMIT) {
|
|
|
|
centerEl = centerEl.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sub = [centerEl];
|
|
|
|
let counter = centerEl.textContent.length;
|
|
|
|
let left = centerEl;
|
|
|
|
let right = centerEl;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
let added = false;
|
|
|
|
|
|
|
|
const prev = left.previousElementSibling;
|
|
|
|
|
|
|
|
if (prev) {
|
|
|
|
const prevText = prev.textContent;
|
|
|
|
|
|
|
|
if (prevText.length + counter > LIMIT) {
|
|
|
|
const prefix = prevText.substr(prevText.length - (LIMIT - counter));
|
|
|
|
|
|
|
|
const textNode = document.createTextNode("…" + prefix);
|
|
|
|
sub.unshift(textNode);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
left = prev;
|
|
|
|
sub.unshift(left);
|
|
|
|
counter += prevText.length;
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const next = right.nextElementSibling;
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
const nextText = next.textContent;
|
|
|
|
|
|
|
|
if (nextText.length + counter > LIMIT) {
|
|
|
|
const suffix = nextText.substr(nextText.length - (LIMIT - counter));
|
|
|
|
|
|
|
|
const textNode = document.createTextNode(suffix + "…");
|
|
|
|
sub.push(textNode);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
right = next;
|
|
|
|
sub.push(right);
|
|
|
|
counter += nextText.length;
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!added) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.classList.add("ck-content");
|
|
|
|
div.classList.add("backlink-excerpt");
|
|
|
|
|
|
|
|
for (const childEl of sub) {
|
|
|
|
div.appendChild(childEl);
|
|
|
|
}
|
|
|
|
|
|
|
|
const subHtml = div.outerHTML;
|
|
|
|
|
|
|
|
excerpts.push(subHtml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
noteId: sourceNote.noteId,
|
|
|
|
excerpts
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:55:59 +02:00
|
|
|
module.exports = {
|
2021-09-17 22:34:23 +02:00
|
|
|
getLinkMap,
|
2021-12-01 23:12:54 +01:00
|
|
|
getTreeMap,
|
|
|
|
getBacklinks
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|