Use R to Convert MD to various document formats (html, docx, epub, pdf etc.)

In case there are other R users out there, I thought I would share how to convert Obsidian Markdown to .html, .docx, .pdf etc. It has come in handy a few times to share notes with team members who don’t work in Obsidian.md

The rmarkdown() library is used for the pandoc conversion functions.

These examples convert a single .md to a single .html - the .docx example saves the converted file to another directory.

If you want to convert multiple files into a single document you can use the c(“file1.md”, “file2.md”, “file3.md”…) format. You could make this more sophisticated by creating a vector with all the .md files in a directory or a loop to convert multiple files separately etc.

library(rmarkdown)

#HTML
pandoc_convert(“U:/ObsidianNotesFolder/Team/2020-10-06.md”,
to=“html”,
output=“20201006.html”)

#PDF
pandoc_convert(“U:/ObsidianNotesFolder/Team/2020-10-06.md”,
to=“pdf”,
output=“20201006.pdf”)

#docx saved to a Temp directory
pandoc_convert(“U:/ObsidianNotesFolder/Team/2020-10-06.md”,
to=“docx”,
output=“C:/Temp/2020-10-06.docx”)

#epub
pandoc_convert(“U:/ObsidianNotesFolder/Team/2020-10-06.md”,
to=“epub”,
output=“20201006.epub”)

2 Likes

I think R is a good method to resolve it . But I indeed suggest using Typora using the same method of Pandoc to deal with it.

1 Like

I’m unfamiliar with Typora and grateful for your introduction to it! Definitely checking it out.

dose it work with .md file that contains image embeded using wiki link(![[IMAGE]]) ?

1 Like

I wonder if it would preserve the linking system from Obsidian? I know when converting from OneNote to epub the links are preserved, but I wasn’t sure for Obsidian. It’s nice to have all of the links in tact on a tablet. Any thoughts?

A couple quick questions for those with more experience:

  1. Can this be done via a CLI? Or do I need to launch RStudio every time? If so, how? I’d appreciate any help, as I’d like to not having to open rstudio every time I’d want to perform a conversion.
  2. Can it insert citations from a bib-file and still have them render correctly?)
  3. Can it include images? The Obsidian-Pandoc-plugin is currently unable to insert images into its output.
  4. Do I need to care about image locations being in a relative path? Can it be in the vault’s global asset-folder?