Skip to content

Instantly share code, notes, and snippets.

@4DA
Created September 7, 2023 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4DA/ee133151db122787b5d8c6a6a8fd202d to your computer and use it in GitHub Desktop.
Save 4DA/ee133151db122787b5d8c6a6a8fd202d to your computer and use it in GitHub Desktop.
split org entries with dates to separate org-dailies file
(defun my/split-org-daily (&optional max-level scope)
"Export each headline in the current buffer to a separate org-roam daily file.
The date of each headline is used as the filename. Existing files
are overwritten. With a prefix argument MAX-LEVEL, only
headlines up to the specified level will be exported. If SCOPE is
nil, the export will be performed on the entire buffer. For
other valid values of SCOPE, refer to `org-map-entries'."
(interactive "P")
(when max-level (setq max-level (format "LEVEL<=%s" max-level)))
;; Widen buffer temporarily as narrowing would affect the exporting.
(save-window-excursion
(org-with-wide-buffer
(save-mark-and-excursion
;; Loop through each headline.
(org-map-entries
(lambda ()
;; Get the plain headline text without statistics and make filename.
(let* ((title (car (last (org-get-outline-path t))))
(date (substring (org-read-date nil nil title) 0 10))
(dir (file-name-directory buffer-file-name))
(filename (concat dir date ".org"))
(entry-begin (point)))
;; Set the active region.
(outline-next-preface)
(write-region entry-begin (point) filename t)
(switch-to-buffer (find-file filename))
(newline)
(goto-char (point-min))
(org-id-get-create)
(save-buffer)
(kill-current-buffer)))
max-level scope)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment