Matthew Brett
Matthew Brett

Reputation: 925

Compile Bookdown to Markdown?

Is there any way to take a Bookdown project, and build it as Markdown instead of HTML or TeX?

I ask because I need to post-process the final Markdown output from Bookdown, in order to extract R and Python notebooks for download.

In more detail, I am using Bookdown to build a textbook that embeds notebooks to download, where the notebooks contain subsets of the code and text in the bookdown .Rmd files. For example, a single chapter could contain more than one notebook.

In order to do this, I put start and end comment markers in the RMarkdown input text to identify the section that will be a notebook, and then post-process the generated Markdown files to extract the notebook section. As in something like:

<!--- notebook: first_section.Rmd
-->

Some explanation, maybe using Bookdown extra markup such as @a_citation.

```{r}
a <- 1
a
```

<!--- end of notebook
-->

More markdown.

```{r}
# More code not in notebook.
b <- 2
```

Obviously I could use the input RMarkdown pages, but this would be ugly, because all the extended Bookdown markup such as citations, cross-references and so on, would appear in raw and ugly form in the generated notebook. So I'd really like to be able to get the final output Markdown, after merging, resolving of citations and cross references. Is there any way of doing that?

My question is similar to this as-yet unanswered question, but adds my motivation for an official solution to this problem.

Upvotes: 1

Views: 235

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

With the latest version of bookdown on CRAN, you can use the output format bookdown::markdown_document2, e.g.,

output: 
  bookdown::markdown_document2:
    base_format: rmarkdown::md_document
    variant: gfm

Upvotes: 1

Related Questions