Notes/src/routes/api/note_map.js

364 lines
10 KiB
JavaScript
Raw Normal View History

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);
const hiddenImageNoteIds = note.getRelations('imageLink').map(rel => rel.value);
const childNoteIds = note.children.map(child => child.noteId);
const nonHiddenNoteIds = childNoteIds.filter(childNoteId => !hiddenImageNoteIds.includes(childNoteId));
noteIdToCountMap[noteId] = nonHiddenNoteIds.length;
2021-09-17 22:34:23 +02:00
for (const child of note.children) {
noteIdToCountMap[noteId] += getCount(child.noteId);
}
}
return noteIdToCountMap[noteId];
}
getCount('root');
return noteIdToCountMap;
}
function getNeighbors(note, depth) {
if (depth === 0) {
return [];
}
const retNoteIds = [];
function isIgnoredRelation(relation) {
return ['relationMapLink', 'template', 'image'].includes(relation.name);
}
// forward links
for (const relation of note.getRelations()) {
if (isIgnoredRelation(relation)) {
continue;
}
const targetNote = relation.getTargetNote();
2022-10-08 22:24:58 +02:00
if (!targetNote || targetNote.hasLabel('excludeFromNoteMap')) {
continue;
}
retNoteIds.push(targetNote.noteId);
for (const noteId of getNeighbors(targetNote, depth - 1)) {
retNoteIds.push(noteId);
}
}
// backward links
for (const relation of note.getTargetRelations()) {
if (isIgnoredRelation(relation)) {
continue;
}
const sourceNote = relation.getNote();
2022-10-08 22:24:58 +02:00
if (!sourceNote || sourceNote.hasLabel('excludeFromNoteMap')) {
continue;
}
retNoteIds.push(sourceNote.noteId);
for (const noteId of getNeighbors(sourceNote, depth - 1)) {
retNoteIds.push(noteId);
}
}
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);
// if the map root itself has exclude attribute (journal typically) then there wouldn't be anything to display, so
// we'll just ignore it
const ignoreExcludeFromNoteMap = mapRootNote.hasLabel('excludeFromNoteMap');
const subtree = mapRootNote.getSubtree({includeArchived: false, resolveSearch: true});
2021-09-20 23:04:41 +02:00
const noteIds = new Set(
subtree.notes
.filter(note => ignoreExcludeFromNoteMap || !note.hasLabel('excludeFromNoteMap'))
.map(note => note.noteId)
);
2021-09-20 20:02:23 +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.getTitleOrProtected(),
2021-09-20 20:02:23 +02:00
note.type
];
});
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
const ignoreExcludeFromNoteMap = mapRootNote.hasLabel('excludeFromNoteMap');
const subtree = mapRootNote.getSubtree({includeArchived: false, resolveSearch: true});
2021-09-17 22:34:23 +02:00
const notes = subtree.notes
.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-09-17 22:34:23 +02:00
.map(note => [
note.noteId,
note.getTitleOrProtected(),
2021-09-17 22:34:23 +02:00
note.type
]);
2022-08-02 17:00:47 +02:00
const noteIds = new Set();
2021-09-17 22:34:23 +02:00
notes.forEach(([noteId]) => noteIds.add(noteId));
2021-09-20 20:02:23 +02:00
const links = [];
for (const {parentNoteId, childNoteId} of subtree.relationships) {
if (!noteIds.has(parentNoteId) || !noteIds.has(childNoteId)) {
2021-09-17 22:34:23 +02:00
continue;
}
links.push({
sourceNoteId: parentNoteId,
targetNoteId: childNoteId
2021-09-17 22:34:23 +02:00
});
}
const noteIdToDescendantCountMap = buildDescendantCountMap();
updateDescendantCountMapForSearch(noteIdToDescendantCountMap, subtree.relationships);
2021-09-17 22:34:23 +02:00
return {
notes: notes,
noteIdToDescendantCountMap: noteIdToDescendantCountMap,
2021-09-17 22:34:23 +02:00
links: links
};
}
function updateDescendantCountMapForSearch(noteIdToDescendantCountMap, relationships) {
for (const {parentNoteId, childNoteId} of relationships) {
const parentNote = becca.notes[parentNoteId];
if (!parentNote || parentNote.type !== 'search') {
continue;
}
noteIdToDescendantCountMap[parentNote.noteId] = noteIdToDescendantCountMap[parentNoteId] || 0;
noteIdToDescendantCountMap[parentNote.noteId] += noteIdToDescendantCountMap[childNoteId] || 1;
}
}
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]);
}
}
2021-12-02 22:00:42 +01:00
const EXCERPT_CHAR_LIMIT = 200;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
function findExcerpts(sourceNote, referencedNoteId) {
const html = sourceNote.getContent();
const document = new JSDOM(html).window.document;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const excerpts = [];
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
removeImages(document);
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (!href || !href.endsWith(referencedNoteId)) {
continue;
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
linkEl.classList.add("backlink-link");
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
let centerEl = linkEl;
2021-12-01 23:12:54 +01:00
2022-05-27 21:55:58 +02:00
while (centerEl.tagName !== 'BODY' && centerEl.parentElement?.textContent?.length <= EXCERPT_CHAR_LIMIT) {
2021-12-02 22:00:42 +01:00
centerEl = centerEl.parentElement;
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const excerptEls = [centerEl];
let excerptLength = centerEl.textContent.length;
let left = centerEl;
let right = centerEl;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
while (excerptLength < EXCERPT_CHAR_LIMIT) {
let added = false;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const prev = left.previousElementSibling;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (prev) {
const prevText = prev.textContent;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (prevText.length + excerptLength > EXCERPT_CHAR_LIMIT) {
const prefix = prevText.substr(prevText.length - (EXCERPT_CHAR_LIMIT - excerptLength));
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const textNode = document.createTextNode("…" + prefix);
excerptEls.unshift(textNode);
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
break;
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
left = prev;
excerptEls.unshift(left);
excerptLength += prevText.length;
added = true;
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const next = right.nextElementSibling;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (next) {
const nextText = next.textContent;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (nextText.length + excerptLength > EXCERPT_CHAR_LIMIT) {
const suffix = nextText.substr(nextText.length - (EXCERPT_CHAR_LIMIT - excerptLength));
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const textNode = document.createTextNode(suffix + "…");
excerptEls.push(textNode);
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
break;
2021-12-01 23:12:54 +01:00
}
2021-12-02 22:00:42 +01:00
right = next;
excerptEls.push(right);
excerptLength += nextText.length;
added = true;
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (!added) {
break;
}
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
const excerptWrapper = document.createElement('div');
excerptWrapper.classList.add("ck-content");
excerptWrapper.classList.add("backlink-excerpt");
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
for (const childEl of excerptEls) {
excerptWrapper.appendChild(childEl);
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
excerpts.push(excerptWrapper.outerHTML);
}
return excerpts;
}
2021-12-01 23:12:54 +01:00
function getFilteredBacklinks(note) {
return note.getTargetRelations()
// search notes have "ancestor" relations which are not interesting
.filter(note => note.getNote().type !== 'search');
}
function getBacklinkCount(req) {
const {noteId} = req.params;
const note = becca.getNote(noteId);
if (!note) {
return [404, "Not found"];
}
else {
return {
count: getFilteredBacklinks(note).length
};
}
}
2021-12-02 22:00:42 +01:00
function getBacklinks(req) {
const {noteId} = req.params;
const note = becca.getNote(noteId);
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (!note) {
return [404, `Note ${noteId} was not found`];
}
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
let backlinksWithExcerptCount = 0;
2021-12-01 23:12:54 +01:00
return getFilteredBacklinks(note).map(backlink => {
2021-12-02 22:00:42 +01:00
const sourceNote = backlink.note;
2021-12-01 23:12:54 +01:00
2021-12-02 22:00:42 +01:00
if (sourceNote.type !== 'text' || backlinksWithExcerptCount > 50) {
return {
noteId: sourceNote.noteId,
relationName: backlink.name
};
2021-12-01 23:12:54 +01:00
}
2021-12-02 22:00:42 +01:00
backlinksWithExcerptCount++;
const excerpts = findExcerpts(sourceNote, noteId);
2021-12-01 23:12:54 +01:00
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,
getBacklinkCount,
2021-12-01 23:12:54 +01:00
getBacklinks
2020-06-20 12:31:38 +02:00
};