Skip to content

Build your personal knowledge base with Trilium Notes

fix backlink count

zadama232035d477d
diff --git a/src/public/app/widgets/backlinks.js b/src/public/app/widgets/backlinks.js
index f7ba2e08f0..bb11b8bf64 100644
--- a/src/public/app/widgets/backlinks.js
+++ b/src/public/app/widgets/backlinks.js
@@ -103,17 +103,19 @@ export default class BacklinksWidget extends NoteContextAwareWidget {
async refreshWithNote(note) {
this.clearItems();
- const targetRelationCount = note.getTargetRelations().length;
- if (targetRelationCount === 0) {
+ // can't use froca since that would count only relations from loaded notes
+ const resp = await server.get(`notes/${this.noteId}/backlink-count`);
+
+ if (!resp || !resp.count) {
this.$ticker.hide();
+ return;
}
- else {
- this.$ticker.show();
- this.$count.text(
- `${targetRelationCount} backlink`
- + (targetRelationCount === 1 ? '' : 's')
- );
- }
+
+ this.$ticker.show();
+ this.$count.text(
+ `${resp.count} backlink`
+ + (resp.count === 1 ? '' : 's')
+ );
}
clearItems() {
@@ -136,18 +138,22 @@ export default class BacklinksWidget extends NoteContextAwareWidget {
await froca.getNotes(backlinks.map(bl => bl.noteId)); // prefetch all
for (const backlink of backlinks) {
- this.$items.append(await linkService.createNoteLink(backlink.noteId, {
+ const $item = $("<div>");
+
+ $item.append(await linkService.createNoteLink(backlink.noteId, {
showNoteIcon: true,
showNotePath: true,
showTooltip: false
}));
if (backlink.relationName) {
- this.$items.append($("<p>").text("relation: " + backlink.relationName));
+ $item.append($("<p>").text("relation: " + backlink.relationName));
}
else {
- this.$items.append(...backlink.excerpts);
+ $item.append(...backlink.excerpts);
}
+
+ this.$items.append($item);
}
}
}
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 49c35607dc..6b43bec7f2 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -302,6 +302,21 @@ function uploadModifiedFile(req) {
note.setContent(fileContent);
}
+function getBacklinkCount(req) {
+ const {noteId} = req.params;
+
+ const note = becca.getNote(noteId);
+
+ if (!note) {
+ return [404, "Not found"];
+ }
+ else {
+ return {
+ count: note.getTargetRelations().length
+ };
+ }
+}
+
module.exports = {
getNote,
updateNote,
@@ -316,5 +331,6 @@ module.exports = {
duplicateSubtree,
eraseDeletedNotesNow,
getDeleteNotesPreview,
- uploadModifiedFile
+ uploadModifiedFile,
+ getBacklinkCount
};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index d0f69e39cd..923f9c078e 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -220,6 +220,7 @@ function register(app) {
apiRoute(DELETE, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.eraseNoteRevision);
route(GET, '/api/notes/:noteId/revisions/:noteRevisionId/download', [auth.checkApiAuthOrElectron], noteRevisionsApiRoute.downloadNoteRevision);
apiRoute(PUT, '/api/notes/:noteId/restore-revision/:noteRevisionId', noteRevisionsApiRoute.restoreNoteRevision);
+ apiRoute(GET, '/api/notes/:noteId/backlink-count', notesApiRoute.getBacklinkCount);
apiRoute(POST, '/api/notes/relation-map', notesApiRoute.getRelationMap);
apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow);
apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle);