Prevent org-roam-capture from adding to property file drawer

This is pretty much the same issue as this question, except that I am trying to do something slightly different.

I’m using org-roam-protocol to capture notes from web pages and save them to a single Notes-1.org file – the captured text is stored under a first-level headline which is given by a link to the webpage.

However, when I perform a capture of a page, the PROPERTIES drawer of the file has ROAM_REFS property updated with the URL of the web page.

I don’t want this behavior in this instance, and instead would prefer the ROAM_REFS property is added under the newly-created headline. But I’m OK with it happening in other circumstances.

This is how I’ve set up the capture template:

        ("N" "notes" entry
         "* [[${ref}][${title}]] \n:PROPERTIES: 
:ROAM_REFS: ${} 
:END: \n\n#+begin_quote\n${body}\n#+end_quote\n\n%?"
         :target (file "~/Org/Notes-1.org)
         :empty-lines 2
         :jump-to-captured t)

What I’ve tried to do is to change the template so that the following function is called somewhere:

(defun my/remove-roam-refs-property-from-file ()
  "Remove the ROAM_REFS property associated to the last captured node from the file property drawer"
  (interactive)
  (goto-char (point-min))
  (org-delete-property "ROAM_REFS")
  (goto-char (point-max))
  )

This function works as intended when used interactively. But I don’t know how to get it to work as intended when the org-roam-capture is called with this template.

I have played around with doing

        ("N" "notes" entry
         "* [[${ref}][${title}]] \n:PROPERTIES:
:ROAM_REFS: ${ref}
:END: \n\n#+begin_quote\n${body}\n#+end_quote\n\n%?"
         :target (file "~/Org/Notes-1.org" )              
                       
         :after-finalize my/org-roam-capture--install-finalize-h
         :empty-lines 2
         :jump-to-captured t)

where we have

(defun my/org-roam-capture--install-finalize-h ()
  "Add function `my/org-roam-capture-finalize-hook' to `org-capture-finalize-hook'"
  (add-hook 'org-capture-after-finalize-hook #'my/org-roam-capture--finalize-hook 'append)
  )

(defun my/org-roam-capture--finalize-hook ()
  "Run function and remove itself from `org-capture-finalize-hook'"
  (my/remove-roam-refs-property-from-file)
  (remove-hook 'org-capture-after-finalize-hook #'my/org-roam-capture--finalize-hook)
  )

But this doesn’t work, nor does modifying the template to use

         :after-finalize my/org-roam-capture--install-finalize-a

where

(defun my/org-roam-capture--install-finalize-a ()
  "Advise function `org-roam-capture--finalize' with `my/remove-roam-refs-property-from-file-a'"
  (advice-add #'org-roam-capture--finalize :after #'my/remove-roam-refs-property-from-file-a )
  )


(defun my/remove-roam-refs-property-from-file-a ()
  "Remove the ROAM_REFS property associated to the last captured node from the file property drawer"
  (interactive)
  (goto-char (point-min))
  (org-delete-property "ROAM_REFS")
  (goto-char (point-max))
  (advice-remove #'org-roam-capture--finalize #'my/remove-roam-refs-property-from-file-a)
  )

But this doesn’t work either (though I’m not sure I’ve implemented the advice-add correctly).

Again, the difference is that I’m happy for this behavior (i.e. adding ROAM_REFS to the file property drawer) to persist normally; I just don’t want it to occur for one particular org-roam capture template more specifically, an org-roam-capture-ref-template). But I’m not sure how to implement this.

After trying a few more things, I think I’ve found what works – the function my/remove-roam-refs-property-from-file has to be in the :before-finalize slot (which I picked up from this answer on the Emacs SE.

Here’s what I ended up using:

    ("N" "notes" entry
         "* [[${ref}][${title}]] \n:PROPERTIES: 
:ROAM_REFS: ${} 
:END: \n\n#+begin_quote\n${body}\n#+end_quote\n\n%?"
         :target (file "~/Org/Notes-1.org)
         :unnarrowed t
         :before-finalize my/remove-roam-refs-property-from-file
         :no-save t
         :empty-lines 2
         :jump-to-captured t)


(defun my/remove-roam-refs-property-from-file ()
  "Remove the ROAM_REFS property associated to the last captured node from the file property drawer"
  (interactive)
  (goto-char (point-min))
  (org-delete-property "ROAM_REFS")
  (goto-char (point-max)) 
  )

1 Like