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

Linebreaks in footnotes #104

Open maelle opened 4 months ago

maelle commented 4 months ago

Via https://github.com/ropensci-review-tools/babeldown/issues/63 @xtimbeau

lines <- c("---",
'title: "footnote"',
'---',
'',
'let us insert a footnote[^1]',
'',
'[^1]: here it is. We want a line break',
'',
'    nice and clean inside the footnote',
'',
'    another one',
'')

temp_file <- withr::local_tempfile()
brio::write_lines(lines, temp_file)
tinkr::yarn$new(temp_file)$body
#> {xml_document}
#> <document xmlns="http://commonmark.org/xml/1.0">
#> [1] <paragraph>\n  <text xml:space="preserve">let us insert a footnote[^1]</t ...
#> [2] <paragraph>\n  <text xml:space="preserve">[^1]: here it is. We want a lin ...
#> [3] <code_block xml:space="preserve" name="">nice and clean inside the footno ...

Created on 2024-04-05 with reprex v2.1.0

To me it seems to be a limitation of commonmark.

zkamvar commented 4 months ago

It's kind of a limitation with {commonmark}. The thing is: we don't process footnotes per se, we just find a way to ignore them.

The solution to this particular problem at the moment is to use two or three spaces, not four for the footnote, which is causing commonmark to see a code block (but see below as to why this is not future-proof 😞)

Last year {commonmark} gained footnote support, but it produces results that are difficult to work with: (see https://github.com/r-lib/commonmark/issues/23 and https://github.com/r-lib/commonmark/issues/24). That being said, this is upstream in the cmark-gfm repository and GitHub has them enabled for their markdown renderer (but not for comments, apparently!)

That being said, apparently commonmark detects footnote blocks by checking for indentations of four or more spaces following the definition 😩, but of course then we run into the problems of the footnotes labelled as <unknown>, which is invalid XML. I've opened https://github.com/github/cmark-gfm/pull/362 to see if it could be fixed upstream, but we'll see how it goes.

lines <- c("---",
'title: "footnote"',
'---',
'',
'let us insert a footnote[^1]',
'',
'[^1]: here it is. We want a line break',
'    ',
'    nice and clean inside the footnote',
'    ',
'    another one',
'')
temp_file <- withr::local_tempfile()
brio::write_lines(lines, temp_file)
commonmark::markdown_xml(lines, footnotes = TRUE) |> writeLines()
#> <?xml version="1.0" encoding="UTF-8"?>
#> <!DOCTYPE document SYSTEM "CommonMark.dtd">
#> <document xmlns="http://commonmark.org/xml/1.0">
#>   <thematic_break />
#>   <heading level="2">
#>     <text xml:space="preserve">title: &quot;footnote&quot;</text>
#>   </heading>
#>   <paragraph>
#>     <text xml:space="preserve">let us insert a footnote</text>
#>     <<unknown> />
#>   </paragraph>
#>   <<unknown>>
#>     <paragraph>
#>       <text xml:space="preserve">here it is. We want a line break</text>
#>     </paragraph>
#>     <paragraph>
#>       <text xml:space="preserve">nice and clean inside the footnote</text>
#>     </paragraph>
#>     <paragraph>
#>       <text xml:space="preserve">another one</text>
#>     </paragraph>
#>   </<unknown>>
#> </document>

Created on 2024-04-05 with reprex v2.0.2

maelle commented 4 months ago

Thank you!!