Changing the following settings improves the markdown export for the purposes of importing into roam:
(setq org-export-with-toc nil) ; remove TOC and anchors
(setq org-export-with-special-strings nil) ; do not encode '--' as '–'
(setq org-export-headline-levels 0) ; export headlines using lists, not '#', this preserves indentation when pasting into roam
(Some of the below is specific to my own setup)
I ran the following to export all my notes into markdown. I had subfolders, so I forced the export into a single folder to flatten the structure.
Could do this with org-publish, but this was going to be a one-off migration, so didn't bother.
(defun max/force-correct-pubdir (orig-fun extension
&optional subtreep pub-dir)
(apply orig-fun extension subtreep (list (concat max/note-directory "roam/"))))
(advice-add 'org-export-output-file-name
:around #'max/force-correct-pubdir)
(defun max/export-to-roam (&optional properties)
(interactive)
(let (
(org-export-with-toc nil) ; remove TOC and anchors
(org-export-with-special-strings nil) ; do not encode '--' as '–'
(org-export-headline-levels 0) ; export headlines using lists, not '#', this preserves indentation when pasting into roam
(all-files (remove-if
(lambda (f) (or
(file-directory-p f)
(string= f (concat max/note-directory "zetl/index.txt"))
(cl-search "Emacs Dotfile" f))) ; only want files, exclude index.txt, exclude dotfile
(directory-files-recursively (concat max/note-directory "zetl/") "" t))))
(mapcar
(lambda (file)
(find-file file)
(org-gfm-export-to-markdown))
all-files
)))
Finally, did some post-processing. The final step is specific to my notes, I had some elisp code that would add linkbacks to create bi-directional links between notes. Obviously roam does this itself (better), so I don't need those lines anymore.
sed -i 's/\(\[[^]]*\]\)([^)]*)/[\1]/g' *.md # make file links into roam links
sed -i 's/<span[^>]*>\([^<]*\)<\/span>/**\1**/g' *.md # replace spans for underline/timestamp with bold
for f in *.md; do echo "[[imported]]" >> "$f"; done # mark imported notes with a tag
sed -i '/Linkback: /d' *.md # remove my linkbacks, since Roam will handle that for us
Then you can just import the notes into roam. I had around 260 and it was pretty quick.