Opening URL in ROAM_REFS field?

I use the browser button described somewhere as roam-refs, which when I click on it in Chrome, opens a new buffer on a new org-roam node for which the capture template looks like this:

:PROPERTIES:
:ID:       220416_145107
:ROAM_REFS: https://en.wikipedia.org/wiki/Foobar
:END:
#+title: Foobar - Wikipedia

Is there already a keystroke or function for opening the thing referred to by the ROAM_REFS property, from wherever in the org document point is?

If not, what approach would people suggest for the implementation of such a function?

I’m thinking it would be nice to open the ROAM_REFS destination as if you navigated to it and then did C-c C-o for org-open-at-point.

I think it would be simple if you can guarantee for your use:

  1. You only have file nodes; no headline nodes
  2. You only have one value in roam_refs prop (you could have multiple in theory)
  • save-excursion (to keep current point)
  • widen (in case you are narrowing the current buffer
  • when org-get-enty (or similar func name) to get roam_refs, returns value
  • org-open-link url (use the value as string)

Something like this?

The last steps to open url might need some tweaks depending on availability of functions — e.g. it might be easier to move to your point to the property and call c-c c-o.

I believe there is also an org macro to do the first two steps in one (called org-with-point or something)

—

If you can’t guarantee the first two conditions, you’d need to generalize this for any nodes:

  1. get the current node where the point is (I think org-roam has this fun, used to update the org-roam buffer as you move to a different node)
  2. Get the values of prop roam_refs (perhaps use the roam’s method for the instance of node to get the value for the current node)
  3. Iterate the values to check any is URL (there should be a func built in with prefix “url”)
  4. If url, open it (same as above)

—

Or… very simply for yourself:

  1. Move to point 1
  2. Move two lines down
  3. C-c C-o
1 Like

The macOS shortcut for this is command-up, so I added this line to my .emacs:

(global-set-key (kbd "s-<up>") 'beginning-of-buffer)

A bit off-topic but this works really well with org-mode links:

(global-set-key (kbd "M-s-<right>") 'org-next-link)
(global-set-key (kbd "M-s-<left>") 'org-previous-link)
(global-set-key (kbd "M-s-<down>") 'org-open-at-point)
(global-set-key (kbd "M-s-<up>") 'previous-buffer)
2 Likes

Thank you for your ideas @nobiot. Based on them, I’ve written this up, which is simple and does the trick:

(defun gpc/open-node-roam-ref-url ()
  "Open the URL in this node's ROAM_REFS property, if one exists"
  (interactive)
  (when-let ((ref-url (org-entry-get-with-inheritance "ROAM_REFS")))
    (browse-url ref-url)))

I bound it to a keystroke under my roam prefix in Doom, and am happy now :slight_smile:

2 Likes

Interesting idea, I hadn’t thought to bring that Mac binding into Emacs, but now I have, thank you :slight_smile:

Great! Love the simplicity :slight_smile:

This is a really nice idea, which I took and adapted for users of org-roam v1.

 (defun lt/open-roam_key-ref-url ()
    "Open the URL in this note's ROAM_KEY property, if one exists"
    (interactive)
    (when-let ((ref-url (org-roam--extract-global-props '("ROAM_KEY"))))
    (browse-url (cdar ref-url))))

It is rather simple but I learned a lot by writing this.

1 Like