V2. Org-roam capture from code

I need from time to time to convert an Org Heading to a file-note. With v1 of Org-roam I used to do it using a custom function that eventually calls (org-roam-capture--capture).

That function was removed from v2 and the org-roam capture mechanism has been changed.

Can someone share a snippet or give me some pointers how to rewrite my old function and make it compatible with v2?

A very simple way would be to create a function to work on the current subtree to do something like this:

  1. Narrow to subtree org-narrow-to-subtree
  2. Create a new buffer
  3. Insert the entire subtree to the new buffer
  4. Save the buffer with .org extension

Not sure if there is anything more you would like to do, but you could also add the following rather easily:

  • Use the current subtree’s heading (title) as the new file’s title
    e.g. At point 1 of the narrowed buffer, org-element-at-point and org-element-property to get the headline’s title and insert it to as #+title: <insert the text here> – Simply use format or concat
  • Generate a new Org-ID for the file
    Simply use org-id-get-create

Just a quick idea.

That makes sense. However, this won’t protect from accidentally creating a note file with the same title multiple times. Besides, using org-roam-capture’s machinery would allow creating new file-notes exactly as stated in the template.

And also opens the possibility for implementing the reverse - re-filing the entire content of a file-note as a subheading to another node (with subsequent deletion of the file). For example, no need to keep whole file titled “Taxonomy”, if it can easily be just a heading in a file-note for “Biology”.

Sure; it’s up to you how you would like to craft it.

What’s the reason why you can’t use org-roam-capture on V2?

Finally, I had some time to dig around the issue and wrote this:

Currently, it only knows how to convert a Heading to a separate file-note. At some point I’d like to extend this, so it works the other way around too. And I also would like to be able to “re-file” content, moving it from one node to another.

btw: I opened an issue to track this: Org-roam-refile · Issue #1658 · org-roam/org-roam · GitHub

1 Like