rundel / parsermd

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

Specific R code can't be parsed in a bullet list, but can be parsed at the top level #33

Open jennybc opened 1 year ago

jennybc commented 1 year ago

This is super weird and it took me a long time to find a minimal-ish example. But this is happening to me in real life, where I am using parsermd on the .Rmd files in the R Packages book. This defines a character vector of some lines:

lines <- glue::glue("
  ```{r toplevel}

  # comment following an empty line
  goo <- function() {
    1 + 1

    # comment following an empty line
  }

If I write lines to file, it's valid .Rmd and it can be rendered. But parsermd can't parse it. AFAICT the problem seems to be this specific combination:

library(parsermd)

writeLines(lines)
#> ```{r toplevel}
#> goo <- function() {
#>   1 + 1
#> 
#>   # comment following an empty line
#> }
#> ```
#> -   List item
#>     ```{r inbulletlist}
#>     foo <- function() {
#>       1 + 1
#> 
#>       # comment following an empty line
#>     }
#>     ```

file <- withr::local_tempfile(lines = lines, pattern = "cheese-", fileext = ".Rmd")
rmarkdown::render(file, output_format = "md_document")
#> processing file: cheese-1109526d58284.Rmd
#> output file: cheese-1109526d58284.knit.md
#> /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc +RTS -K512m -RTS cheese-1109526d58284.knit.md --to markdown_strict-yaml_metadata_block --from markdown+autolink_bare_uris+tex_math_single_backslash --output cheese-1109526d58284.md
#> 
#> Output created: cheese-1109526d58284.md

# making sure the `.Rmd` renders fine
file |> 
  fs::path_ext_set("md") |> 
  readLines()
#>  [1] "    goo <- function() {"                    
#>  [2] "      1 + 1"                                
#>  [3] ""                                           
#>  [4] "      # comment following an empty line"    
#>  [5] "    }"                                      
#>  [6] ""                                           
#>  [7] "-   List item"                              
#>  [8] ""                                           
#>  [9] "        foo <- function() {"                
#> [10] "          1 + 1"                            
#> [11] ""                                           
#> [12] "          # comment following an empty line"
#> [13] "        }"

parse_rmd(file)
#> Error: Failed to parse line 9
#>     ```{r inbulletlist}
#> ^~~~~~~~~~~~~~~~~~~~~~~