Skip to content

Instantly share code, notes, and snippets.

@zzamboni
Last active January 20, 2020 21:01
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 zzamboni/2e6ac3c4f577249d98efb224d9d34488 to your computer and use it in GitHub Desktop.
Save zzamboni/2e6ac3c4f577249d98efb224d9d34488 to your computer and use it in GitHub Desktop.
org-multi-file-md-export
;; Call this function with "M-x org-multi-file-md-export"
(defun org-multi-file-md-export ()
"Export current buffer to multiple Markdown files."
(interactive)
;; Loop over all entries in the file
(org-map-entries
(lambda ()
(let* ((level (nth 1 (org-heading-components)))
(title (or (nth 4 (org-heading-components)) ""))
;; Export filename is the EXPORT_FILE_NAME property, or the
;; lower-cased section title if it's not set.
(filename
(or (org-entry-get (point) "EXPORT_FILE_NAME")
(concat (replace-regexp-in-string " " "-" (downcase title)) ".md"))))
(when (= level 1) ;; export only first level entries
;; Mark the subtree so that the title also gets exported
(org-mark-subtree)
;; Call the export function. This is one of the base org
;; functions, the 'md defines the backend to use for the
;; conversion. For exporting to other formats, simply use the
;; correct backend name, and also change the file extension
;; above.
(org-export-to-file 'md filename nil t nil))))
;; skip headlines tagged with "noexport" (this is an argument to
;; org-map-entries above)
"-noexport")
nil nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment