# Ironing out kinks in org-roam-extract-subtree

**URL:** <https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472>\
**Category:** Troubleshooting\
**Created:** [February 23, 2022, 4:30pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472 "2022-02-23T16:30:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![midas](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@midas](https://org-roam.discourse.group/u/midas)\
**Post date:** [February 23, 2022, 4:30pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/1 "2022-02-23T16:30:45Z")

</div>

Refactoring a header within a current node into a separate file-node is a very common action for me. The function `org-roam-extract-subtree` seems built for exactly this purpose. However, I have been encountering problems or unexpected behaviors. I will add to this thread as I observe and enumerate all the issues. ~~Meanwhile, here is the first issue~~ Here is the list:

1. First paragraph of subtree is treated strangely. The first paragraph is placed one line above `#+title:` in the newly-created file-node.
2. What template, if any, does the newly-created file-node use? For example, I’d like to insert `:CREATED:` in the PROPERTIES drawer of the file-node if subheader doesn’t already have this property. If subheader already has this property, I’d like to insert `:MODIFIED:`.
3. Is it possible to open the newly-created file-node in a new window (perhaps a preference variable whose value can be set by the user)? One reason I want to see it in a new window is to confirm that the file has indeed been created with the appropriate content. Often what is being refactored is precious text. Some visual feedback confirming that it has been saved in its full form can be reassuring.
4. When extracting a subtree from a non-roam org file, I’d like it to create the node-file in my org-roam-directory. Currently, it seems to create the node-file in the directory in which the original file is found. And, there is some strange behavior after the extraction (I haven’t fully characterized the behavior but something is off (sorry for that vague description… I’ll add detail when I get the chance)).

I think that captures my major concerns/issues.

---

<div class="post-metadata">

**Author:** ![midas](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@midas](https://org-roam.discourse.group/u/midas)\
**Post date:** [February 26, 2022, 6:31pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/2 "2022-02-26T18:31:06Z")

</div>

I believe Issue #1 is a problem with `org-roam-promote-entire-buffer`. To recreate, follow these steps:  
a. Create a new `.org` file in your `org-roam-directory` and insert the following text:

```auto
* asfasdfasfsdasdfa
:PROPERTIES:
:CREATED: [2022-02-26 Sat 13:24]
:END:
asdfasdfasdggbvbx
fsdfsfsfs

```

b. `M-x org-roam-promote-entire-buffer`

---

<div class="post-metadata">

**Author:** ![midas](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@midas](https://org-roam.discourse.group/u/midas)\
**Post date:** [February 28, 2022, 10:51am UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/3 "2022-02-28T10:51:26Z")

</div>

I am writing my own version of `extract-subtree`. Below is a version that appears to address Issue 4 and, in a clunky way, Issue 1. I took chunks of code from the current version of `org-roam-extract-subtree` from Github. I will post again when I eventually address Issues 3 and 4. The end-product is a non-elegant patched-up solution — good enough, I hope, until the real `org-roam-extract-subtree` is fixed in a future release.

```auto
  (defun aa/roam-extract-subtree ()
    (interactive)
    (save-excursion
      (org-back-to-heading-or-point-min t)
      (when (bobp) (user-error "Already a top-level node, possibly in a narrowed buffer"))
      (org-id-get-create)
      (save-buffer)
      (let* ((target-path (expand-file-name org-roam-directory))
             (file-path (concat target-path "/" (aa/slug-maker (substring-no-properties (org-get-heading t t))) ".org")))
        (when (file-exists-p file-path)
          (user-error "%s exists. Aborting" file-path))
        (when (string-prefix-p target-path (buffer-file-name))
          (org-roam-db-update-file))
        (org-cut-subtree)
        (save-buffer)
        (with-current-buffer (find-file-noselect file-path)
          (org-paste-subtree)
          (org-roam-promote-entire-buffer)
        ;;; --- a crude fix to correct placement of #+title: ---
          (search-forward "#+title:")
          (beginning-of-line)
          (org-metaup)
        ;;; -----------------------------------------------
          (save-buffer)))
      ))

  (defun aa/slug-maker (thestring)
    ;; (interactive "r")
    (string-trim
     (replace-regexp-in-string
      "[^a-z0-9_-]" ""
      (replace-regexp-in-string
       "\s+" "_"
       (downcase thestring)
       )) "[\t\n\r\s_-]+" "[\t\n\r\s_-]+")
    )

```

---

<div class="post-metadata">

**Author:** ![jmay](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmay](https://org-roam.discourse.group/u/jmay)\
**Post date:** [February 28, 2022, 8:11pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/4 "2022-02-28T20:11:27Z")

</div>

PR is pending for #1:

> <https://github.com/org-roam/org-roam/pull/2091>
>
> \###### Motivation for this change
> Fixes #1857 and #2058 
> 
> \###### Changes
> Run… safety checks before executing \`org-roam-promote-entire-buffer\`. These are unnecessary when it is called from \`org-roam-extract-subtree\`, but provide some extract protection if invoked directly.
> 
> Replaced an invocation of \`(org-roam-set-keyword "title" title)\` with explicit code, to avoid the situation where \`org-roam-set-keyword\` steps over a line and injects the \`#+title:\` line into the middle of body text instead of at the top.
> 
> Added a call to \`save-buffer\` to prevent \`org-map-entries\` from triggering a confusing & unwanted org-agenda prompt "Non-existent agenda file. \[R\]emove from list or \[A\]bort?".

---

<div class="post-metadata">

**Author:** ![midas](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@midas](https://org-roam.discourse.group/u/midas)\
**Post date:** [March 28, 2022, 5:25pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/5 "2022-03-28T17:25:15Z")

</div>

Updated to org-roam 2022-03-15.637 from MELPA and #1 still an issue.

---

<div class="post-metadata">

**Author:** ![jmay](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmay](https://org-roam.discourse.group/u/jmay)\
**Post date:** [March 28, 2022, 6:17pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/6 "2022-03-28T18:17:26Z")

</div>

The PR has not been merged. I’ll update it and resubmit.

---

<div class="post-metadata">

**Author:** ![jethro](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jethro](https://org-roam.discourse.group/u/jethro)\
**Post date:** [April 3, 2022, 8:14pm UTC](https://org-roam.discourse.group/t/ironing-out-kinks-in-org-roam-extract-subtree/2472/7 "2022-04-03T20:14:51Z")

</div>

Sorry for the delay on the review @jmay, I think there are a couple of issues that are inherited from the original code, I’ll write a longform comment on the issue today.
