How to set up org-roam-capture-templates to create a node headline?

I’d like for my default new node creation to create a new heading with it’s own id in a target file.

I currently have set up my org-roam-capture-templates as such:

  (setq org-roam-capture-templates
        '(("d" "default" entry "* ${title}"
           :target (file "/home/eddie/orgnotes/org-roam.org")
           :unnarrowed t)))

This does set up a new heading in the target file, however the new heading is not assigned an id. Instead, the file itself is given an id (if it does not have one already).

I would like instead for the new heading to be given an id and for the link to refer to the heading (instead of the file).

How can I achieve this?

1 Like

Is this thread of any help?

1 Like

That is very close, however when I run org-roam-node-insert and create a new node, the link uses the id for the file, not the node.

When I run org-roam-node-insert on an existing node it links correctly, however.

Would there be a way to fix this issue?

I had a quick look at the current code. It does not look to be designed to work in the way you would like. The headline node is not created in time to be passed to the capture finalize function, which inserts a new ID link.

I could suggest two options:

  • Code it yourself (with others’ help – I can’t see how this can be done easily)
  • Create a project’s GitHub issue to request a new feature

This is only as far as I could see. Other people might have better ideas.

I set up the following to capture with the standard org-capture and the org-capture templates. The key is to create an ID automatically in the capture buffer with the hook: :hook (org-capture-mode . org-id-get-create). I could not get it to work with org-roam-capture as it seems to use the ID of the file as mentioned above.

My setup.

(use-package org
  :ensure nil
  :hook (org-capture-mode . org-id-get-create)
  :bind
  ("C-c l" . org-store-link)
  ("C-c a" . org-agenda)
  ("C-c c" . org-capture)
  :config
  (setq org-startup-indented t)
  (setq org-directory "~/iCloud/org"
    org-default-notes-file (concat org-directory "/notes.org"))
    (let ((capture_template (concat
                             "*  %?\n"
                             ":PROPERTIES:\n"
                             ":DIR: %^{DIR|attachments|../../Privat|../../Arbete}\n"
                             ":END:\n"
                             )
                            ))
      (setq org-capture-templates `(
                                    ("p" "Private" entry (file "private.org") ,capture_template
                                     :empty-lines 1)
                                    ("w" "Work" entry (file "work.org") ,capture_template
                                     :empty-lines 1)
                                    )
            )
      )
    )

(use-package org-roam
  :ensure t
  :custom
  (org-roam-directory (file-truename org-directory))
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n g" . org-roam-graph)
         ("C-c n i" . org-roam-node-insert)
         ("C-c n c" . org-roam-capture)
         ("C-c n j" . org-roam-dailies-capture-today))
  :config
  (org-roam-db-autosync-mode)
  )

Huh, that’s a good idea. That’ll create an id from org-capture, but org-roam-node-insert still links to the file, not the heading.

I suspect this may be what I need to do - I’ll post back here when I do.

At least for me it links to the node in question, but maybe that’s because I don’t have any ID for the file.

Odd, for me it just seems to link the first id tag it comes across in the file.

No matter, the hook strategy also creates an id tag for all my captures, which I don’t like.

I think I’ll just stop trying to fight the package and just have all my org nodes be files. Thanks for the tips/help.

I use org-capture-prepare-finalize-hook for this:

(defun nom/org-roam-capture-create-id ()
  "Create id for captured note and add it to org-roam-capture-template." 
  (when (and (not org-note-abort)
             (org-roam-capture-p))
    (org-roam-capture--put :id (org-id-get-create))))
(add-hook 'org-capture-prepare-finalize-hook 'nom/org-roam-capture-create-id))
3 Likes