ropensci / tinkr

Convert (R)Markdown files to XML, edit them, write them back as (R)Markdown
https://docs.ropensci.org/tinkr
GNU General Public License v3.0
57 stars 3 forks source link

known issue: commonmark cannot parse code chunk options with backtics #89

Open zkamvar opened 1 year ago

zkamvar commented 1 year ago

I just found a fun wrinkle: R chunk options that contain backticks cannot be parsed by {commonmark}, and thus {tinkr}:

test <- r"{
```{r, fig.cap = "caption with `backticks`"}
code block
```}"
commonmark::markdown_xml(test) |> writeLines()
#> <?xml version="1.0" encoding="UTF-8"?>
#> <!DOCTYPE document SYSTEM "CommonMark.dtd">
#> <document xmlns="http://commonmark.org/xml/1.0">
#>   <paragraph>
#>     <text xml:space="preserve">```{r, fig.cap = &quot;caption with </text>
#>     <code xml:space="preserve">backticks</code>
#>     <text xml:space="preserve">&quot;}</text>
#>     <softbreak />
#>     <text xml:space="preserve">code block</text>
#>   </paragraph>
#>   <code_block xml:space="preserve"></code_block>
#> </document>

Created on 2023-02-15 with reprex v2.0.2

I don't think there is much to be done about this because R Markdown is a superset of commonmark, so it's not expected to fully parse correctly in the first place.