Skip to content

Instantly share code, notes, and snippets.

@adam-james-v
Created April 22, 2022 04:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adam-james-v/97713207ec688218733f6c34af133749 to your computer and use it in GitHub Desktop.
Save adam-james-v/97713207ec688218733f6c34af133749 to your computer and use it in GitHub Desktop.
Elisp function that calls Pandoc's org to markdown exporter. Useful for org+clerk literate programming
;; You can use this function to export your current .org file as a .md file
;; The function requires Pandoc to be installed, so make sure you've got it!
;; Why use this instead of org's built in markdown exporter?
;; If you're a fan of both org mode and NextJournal's Clerk, you can use this to
;; effectively have Clerk render your .org files as you write them.
;; The built-in markdown exporter does not correctly annotate code blocks
;; with ```clojure \n ... \n```, which prevents Clerk from seeing and evaluating
;; your clojure code. Pandoc's exporter from org->md is simple and doesn't support
;; every org feature, but does annotate code blocks properly.
;; This works well enough for basic cases, and hopefully you can build a nice
;; workflow with it.
;; the function will save the markdown file as [org-file-name-with-extension].md
;; So, if you're editing "~/literate-clojure.org" the markdown file will be
;; "~/literate-clojure.org.md".
(defun export-md-on-save-org-mode-file ()
(let ((filename
(buffer-file-name)))
(when (and (string-match-p
(regexp-quote ".org") (message "%s" (current-buffer)))
(not (string-match-p
(regexp-quote "[") (message "%s" (current-buffer)))))
(shell-command
(concat "pandoc -f org -t markdown -o " filename ".md " filename)))))
;; Run the function on save.
;; You might alternatively set a kbd shortcut to only run exports manually
(add-hook 'after-save-hook 'export-md-on-save-org-mode-file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment