Show nodes of same tag in org-roam-buffer

Hello,

according to the documentation, the following types of related nodes can be shown:

  • Backlinks: View (preview of) nodes that link to this node
  • Reference Links: Nodes that reference this node (see Refs)
  • Unlinked references: View nodes that contain text that match the nodes title/alias but are not linked

Obviously, there is no function that returns the nodes that share the same tag as the current node?

Is it possible, out of the box, to show nodes that have the same tag as the current node?

If not, the org-roam-buffer seems to be easily extendable. A follow up question in this direction: how to search for nodes with matching tags programmatically?

Thanks!

Refer to SystemCrafters’ code below referenced in this post — this post also points out a potential pitfall in code.

(defun my/org-roam-filter-by-tag (tag-name)
  (lambda (node)
    (member tag-name (org-roam-node-tags node))))

(defun my/org-roam-list-notes-by-tag (tag-name)
  (mapcar #'org-roam-node-file
          (seq-filter
           (my/org-roam-filter-by-tag tag-name)
           (org-roam-node-list))))

1 Like

Thanks for your pointer!

My current work-in-progress-but-works code is:

(setq org-roam-mode-section-functions
      (list 'org-roam-backlinks-section
            'org-roam-reflinks-section
            'flo/org-roam-tags-section))

(defun flo/org-roam-list-notes-by-tag (tag-name)
  (seq-filter (lambda (node)
                (member tag-name (org-roam-node-tags node)))
              (org-roam-node-list)))

(defun flo/org-roam-tags-section (node)
  "The tags section for NODE."
  ;; (message node)
  (when-let ((tags (org-roam-node-tags node)))
    (setq tags (cl-remove-if (lambda (tag) (string= "ATTACH" tag)) tags))
    (magit-insert-section (org-roam-tags)
      (magit-insert-heading "Tags")
      (dolist (tag tags)
        (magit-insert-section (org-roam-tags)
          (magit-insert-heading tag)
          (dolist (tagged-node (flo/org-roam-list-notes-by-tag tag))
            (org-roam-node-insert-section
             :source-node tagged-node
             :point 1
             ;; :properties (org-roam-backlink-properties tag
             )
            )
          )
        ))
    (insert ?\n)))

Any comments appreciated!

The output is not exactly prety, the nested magit heading is not really visible as nested. Also, I wasn’t able to get a value for the point in org-roam-node-insert-section.

Best!

1 Like