rundel / parsermd

https://rundel.github.io/parsermd/
Other
76 stars 4 forks source link

Is is possible to recreate a parsed Rmd? #39

Open davidkane9 opened 5 months ago

davidkane9 commented 5 months ago

I would like to take a file, in-file.Rmd, parse it and then use as_document() or related functions to create an exact copy, called out-file.Rmd. Despite some experimentation, I can't figure out how to do this. out-file.Rmd always has some extra blank lines.

Consider this file: input-file.Rmd:

---
title: Example for Diagnosing as_document()
---

Parsing and Rmd and then recreating that same Rmd tends to add extra blank lines. You can see this in the example from the documentation: https://rundel.github.io/parsermd/articles/parsermd.html#parsing-rmds

The input file, system.file("minimal.Rmd", package = "parsermd")), has 41 lines. The file recreated from the parsed object has 43 lines. Consider another example:

## Section Header
### 

We want "in-file.Rmd" to equal "out-file.Rmd"

```{r}
library(tibble)
library(parsermd)
writeLines(as_document(as_tibble(parse_rmd("in-file.Rmd"))), "out-file.Rmd")

Alas, we end up with three extra blank lines. How to avoid that?

Exercise 1

Last line of text should be line 24. Instead, it is 28. And note the two extra blank lines at the end of out-file.Rmd.



If you execute this code, you will create `out-file.Rmd` which includes all the same text as `in-file.Rmd`, but with a bunch of (annoying!) extra blank lines.

How can I avoid this? I want `in-file.Rmd` to be identical to `out-file.Rmd`.