From 082d44bd397807492c7f0ebf65a0ccf6b12baba8 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 27 May 2024 15:51:23 -0700 Subject: Update showing backlinks to work for Org Roam v2 This doesn't include backlinks from nodes within the same file. I've intentionally excluded these since they should be visible within the same file anyway. This is only for file nodes, doesn't include anything for headline nodes. If I get to adding backlinks for headline nodes as well I should show backlinks from headlines in the same file as well. - ‘org-roam--get-backlinks’ has been removed and is now replaced by ‘org-roam-backlinks-get’. The new function returns a list of ‘org-roam-backlink’ objects, which is different from what was returned before. - ‘org-roam-format-link’ has been removed. This was just a small wrapper around ‘org-link-make-string’. For now I've hard-coded the ‘file:’ link type since it's easy, but in the future this should probably use the org id instead so that it doesn't just go to the right file, but also the right heading. The line number could also be added. - In ‘nroam-backlinks--insert-backlink’ the ‘backlink’ argument is now an ‘org-roam-backlink’ object, not a string and a property list. - ‘nroam--fix-links’ has been removed for now since generally in org-roam v2 everything uses ‘id:’ links that are completely independent of where their source (or target) file are located. --- nroam-backlinks.el | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'nroam-backlinks.el') diff --git a/nroam-backlinks.el b/nroam-backlinks.el index f4ee29e..ba5f140 100644 --- a/nroam-backlinks.el +++ b/nroam-backlinks.el @@ -47,13 +47,18 @@ (defun nroam-backlinks--get-backlinks () "Return a list of backlinks for the current buffer." - (if-let* ((file-path (buffer-file-name (current-buffer))) - (titles (org-roam--extract-titles))) - (org-roam--get-backlinks (cons file-path titles)))) + (save-excursion + (goto-char (point-min)) + (let ((node (org-roam-node-at-point))) + (seq-remove (lambda (b) + (string= (org-roam-node-file (org-roam-backlink-source-node b)) + (org-roam-node-file node))) + (org-roam-backlinks-get node))))) (defun nroam-backlinks--group (backlinks) "Return BACKLINKS grouped by source file." - (seq-group-by #'car backlinks)) + (seq-group-by (lambda (l) (org-roam-node-file (org-roam-backlink-source-node l))) + backlinks)) (defun nroam-backlinks--insert-heading (count) "Insert the heading for the backlinks section with a COUNT." @@ -66,19 +71,21 @@ "Insert all backlinks in GROUP." (let* ((file (car group)) (backlinks (cdr group)) - (title (org-roam-format-link file - (org-roam-db--get-title file) - "file"))) + (title (org-link-make-string (concat "file:" file) + (caar (org-roam-db-query + [:select title :from nodes :where (= file $s1)] + file))))) (nroam--insert-heading 3 title) (nroam--do-separated-by-newlines #'nroam-backlinks--insert-backlink backlinks))) (defun nroam-backlinks--insert-backlink (backlink) "Insert the source element where BACKLINK is defined." - (seq-let (file _ props) backlink - (when-let* ((point (plist-get props :point)) - (elt (nroam-backlinks--crawl-source file point)) + (let* ((backlink-node (org-roam-backlink-source-node backlink)) + (file (org-roam-node-file backlink-node)) + (point (org-roam-backlink-point backlink))) + (when-let* ((elt (nroam-backlinks--crawl-source file point)) (type (plist-get elt :type)) - (content (nroam--fix-links (string-trim (plist-get elt :string)) file)) + (content (org-roam-preview-get-contents file point) file) (beg (point))) (let ((outline (plist-get elt :outline)) (full-outline (plist-get elt :full-outline))) -- cgit v1.2.3-54-g00ecf From 96d383d8c2d6c8cd310eea89b5c200e5c2c502d5 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Sat, 8 Jun 2024 15:36:26 -0700 Subject: Fix syntax error --- nroam-backlinks.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nroam-backlinks.el') diff --git a/nroam-backlinks.el b/nroam-backlinks.el index ba5f140..0e8c06b 100644 --- a/nroam-backlinks.el +++ b/nroam-backlinks.el @@ -85,7 +85,7 @@ (point (org-roam-backlink-point backlink))) (when-let* ((elt (nroam-backlinks--crawl-source file point)) (type (plist-get elt :type)) - (content (org-roam-preview-get-contents file point) file) + (content (org-roam-preview-get-contents file point)) (beg (point))) (let ((outline (plist-get elt :outline)) (full-outline (plist-get elt :full-outline))) -- cgit v1.2.3-54-g00ecf