Skip to content

Build your personal knowledge base with Trilium Notes

backlinks improvements, #2349

zadam630d9f2e4547
diff --git a/.gitpod.yml b/.gitpod.yml
index 40dcc1b958..a7f713d3a2 100644
--- a/.gitpod.yml
+++ b/.gitpod.yml
@@ -2,7 +2,7 @@ image:
file: .gitpod.dockerfile
tasks:
- - before: nvm install 16.13.0 && nvm use 16.13.0
+ - before: nvm install 16.13.1 && nvm use 16.13.1
init: npm install
command: npm run start-server
diff --git a/Dockerfile b/Dockerfile
index 0258e6c6d3..b1ebbe7cef 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM node:16.13.0-alpine
+FROM node:16.13.1-alpine
# Create app directory
WORKDIR /usr/src/app
diff --git a/bin/build-server.sh b/bin/build-server.sh
index 437c87c5a7..805c2234f9 100755
--- a/bin/build-server.sh
+++ b/bin/build-server.sh
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
PKG_DIR=dist/trilium-linux-x64-server
-NODE_VERSION=16.13.0
+NODE_VERSION=16.13.1
if [ "$1" != "DONTCOPY" ]
then
diff --git a/bin/copy-trilium.sh b/bin/copy-trilium.sh
index 975917b354..c9439f79b9 100755
--- a/bin/copy-trilium.sh
+++ b/bin/copy-trilium.sh
@@ -5,7 +5,7 @@ if [[ $# -eq 0 ]] ; then
exit 1
fi
-n exec 16.13.0 npm run webpack
+n exec 16.13.1 npm run webpack
DIR=$1
@@ -27,7 +27,7 @@ cp -r electron.js $DIR/
cp webpack-* $DIR/
# run in subshell (so we return to original dir)
-(cd $DIR && n exec 16.13.0 npm install --only=prod)
+(cd $DIR && n exec 16.13.1 npm install --only=prod)
# cleanup of useless files in dependencies
rm -r $DIR/node_modules/image-q/demo
diff --git a/src/public/app/widgets/backlinks.js b/src/public/app/widgets/backlinks.js
index 2b1353b6aa..8ed93d9cf5 100644
--- a/src/public/app/widgets/backlinks.js
+++ b/src/public/app/widgets/backlinks.js
@@ -23,6 +23,7 @@ const TPL = `
display: flex;
justify-content: space-between;
align-items: center;
+ z-index: 10;
}
.backlinks-count {
@@ -56,6 +57,15 @@ const TPL = `
opacity: 80%;
font-size: 90%;
}
+
+ .backlink-excerpt .backlink-link { /* the actual backlink */
+ font-weight: bold;
+ background-color: yellow;
+ }
+
+ /* relation map has already buttons in that position */
+ .type-relation-map .backlinks-ticker { top: 50px; }
+ .type-relation-map .backlinks-items { top: 100px; }
</style>
<div class="backlinks-ticker">
@@ -136,8 +146,12 @@ export default class BacklinksWidget extends NoteContextAwareWidget {
showTooltip: false
}));
- this.$items.append("<br/>");
- this.$items.append(...backlink.excerpts);
+ if (backlink.relationName) {
+ this.$items.append($("<p>").text("relation: " + backlink.relationName));
+ }
+ else {
+ this.$items.append(...backlink.excerpts);
+ }
}
}
}
diff --git a/src/routes/api/note_map.js b/src/routes/api/note_map.js
index 5e14a71408..dc9a99b8d4 100644
--- a/src/routes/api/note_map.js
+++ b/src/routes/api/note_map.js
@@ -182,115 +182,123 @@ function removeImages(document) {
}
}
-function getBacklinks(req) {
- const {noteId} = req.params;
- const note = becca.getNote(noteId);
+const EXCERPT_CHAR_LIMIT = 200;
- if (!note) {
- return [404, `Note ${noteId} was not found`];
- }
+function findExcerpts(sourceNote, referencedNoteId) {
+ const html = sourceNote.getContent();
+ const document = new JSDOM(html).window.document;
- let backlinks = note.getTargetRelations();
+ const excerpts = [];
- if (backlinks.length > 50) {
- backlinks = backlinks.slice(0, 50);
- }
+ removeImages(document);
- return backlinks.map(backlink => {
- const sourceNote = backlink.note;
+ for (const linkEl of document.querySelectorAll("a")) {
+ const href = linkEl.getAttribute("href");
- const html = sourceNote.getContent();
- const dom = new JSDOM(html);
+ if (!href || !href.endsWith(referencedNoteId)) {
+ continue;
+ }
- const excerpts = [];
+ linkEl.classList.add("backlink-link");
- const document = dom.window.document;
+ let centerEl = linkEl;
- removeImages(document);
+ while (centerEl.tagName !== 'BODY' && centerEl.parentElement.textContent.length <= EXCERPT_CHAR_LIMIT) {
+ centerEl = centerEl.parentElement;
+ }
- for (const linkEl of document.querySelectorAll("a")) {
- const href = linkEl.getAttribute("href");
+ const excerptEls = [centerEl];
+ let excerptLength = centerEl.textContent.length;
+ let left = centerEl;
+ let right = centerEl;
- if (!href || !href.includes(noteId)) {
- continue;
- }
+ while (excerptLength < EXCERPT_CHAR_LIMIT) {
+ let added = false;
- linkEl.style.fontWeight = "bold";
- linkEl.style.backgroundColor = "yellow";
+ const prev = left.previousElementSibling;
- const LIMIT = 200;
- let centerEl = linkEl;
+ if (prev) {
+ const prevText = prev.textContent;
- while (centerEl.tagName !== 'BODY' && centerEl.parentElement.textContent.length < LIMIT) {
- centerEl = centerEl.parentElement;
- }
+ if (prevText.length + excerptLength > EXCERPT_CHAR_LIMIT) {
+ const prefix = prevText.substr(prevText.length - (EXCERPT_CHAR_LIMIT - excerptLength));
- const sub = [centerEl];
- let counter = centerEl.textContent.length;
- let left = centerEl;
- let right = centerEl;
+ const textNode = document.createTextNode("…" + prefix);
+ excerptEls.unshift(textNode);
- while (true) {
- let added = false;
+ break;
+ }
- const prev = left.previousElementSibling;
+ left = prev;
+ excerptEls.unshift(left);
+ excerptLength += prevText.length;
+ added = true;
+ }
- if (prev) {
- const prevText = prev.textContent;
+ const next = right.nextElementSibling;
- if (prevText.length + counter > LIMIT) {
- const prefix = prevText.substr(prevText.length - (LIMIT - counter));
+ if (next) {
+ const nextText = next.textContent;
- const textNode = document.createTextNode("…" + prefix);
- sub.unshift(textNode);
+ if (nextText.length + excerptLength > EXCERPT_CHAR_LIMIT) {
+ const suffix = nextText.substr(nextText.length - (EXCERPT_CHAR_LIMIT - excerptLength));
- break;
- }
+ const textNode = document.createTextNode(suffix + "…");
+ excerptEls.push(textNode);
- left = prev;
- sub.unshift(left);
- counter += prevText.length;
- added = true;
+ break;
}
- const next = right.nextElementSibling;
+ right = next;
+ excerptEls.push(right);
+ excerptLength += nextText.length;
+ added = true;
+ }
- if (next) {
- const nextText = next.textContent;
+ if (!added) {
+ break;
+ }
+ }
- if (nextText.length + counter > LIMIT) {
- const suffix = nextText.substr(nextText.length - (LIMIT - counter));
+ const excerptWrapper = document.createElement('div');
+ excerptWrapper.classList.add("ck-content");
+ excerptWrapper.classList.add("backlink-excerpt");
- const textNode = document.createTextNode(suffix + "…");
- sub.push(textNode);
+ for (const childEl of excerptEls) {
+ excerptWrapper.appendChild(childEl);
+ }
- break;
- }
+ excerpts.push(excerptWrapper.outerHTML);
+ }
+ return excerpts;
+}
- right = next;
- sub.push(right);
- counter += nextText.length;
- added = true;
- }
+function getBacklinks(req) {
+ const {noteId} = req.params;
+ const note = becca.getNote(noteId);
- if (!added) {
- break;
- }
- }
+ if (!note) {
+ return [404, `Note ${noteId} was not found`];
+ }
- const div = document.createElement('div');
- div.classList.add("ck-content");
- div.classList.add("backlink-excerpt");
+ let backlinks = note.getTargetRelations();
- for (const childEl of sub) {
- div.appendChild(childEl);
- }
+ let backlinksWithExcerptCount = 0;
- const subHtml = div.outerHTML;
+ return backlinks.map(backlink => {
+ const sourceNote = backlink.note;
- excerpts.push(subHtml);
+ if (sourceNote.type !== 'text' || backlinksWithExcerptCount > 50) {
+ return {
+ noteId: sourceNote.noteId,
+ relationName: backlink.name
+ };
}
+ backlinksWithExcerptCount++;
+
+ const excerpts = findExcerpts(sourceNote, noteId);
+
return {
noteId: sourceNote.noteId,
excerpts