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

[Breaking Changes] attribute-based node protection #107

Open zkamvar opened 7 months ago

zkamvar commented 7 months ago

As discussed in https://github.com/ropensci/tinkr/issues/105#issuecomment-2040238007, since I have some time on my hands, I wanted to give this a go.

This shifts that paradigm from splitting nodes that need protection to one where we would use attributes to tell us where the protection needs to be applied.

Not Ready to Merge

I'm not quite ready to merge this yet because this has ripple effects for both {babeldown} as it explicitly relies on asis and curly nodes to be separated out to avoid them entering the translation fields

  ## protect content inside curly braces and math ----
  woolish$body <- tinkr::protect_math(woolish$body)
  woolish$body <- tinkr::protect_curly(woolish$body)
  curlies <- xml2::xml_find_all(woolish$body, "//*[@curly]")
  purrr::walk(curlies, protect_curly)
  maths <- xml2::xml_find_all(woolish$body, "//*[@asis='true']")
  purrr::walk(maths, protect_math)

and {pegboard} as the link transformation routines (from Jekyll -> pandoc) explicitly assume that the asis nodes exist

as shown in the documentation fix_links.R#L38-L48:

#' However, if a link uses liquid templating for a variable such as: 
#' `[Home]({{ page.root }}/index.html) and other text`, it will appear in XML as
#'
#' ```xml
#' ...
#' <text asis="true">[</text>
#' <text>Home</text>
#' <text asis="true">]</text>
#' <text>({{ page.root }}/index.html) and other text</text>
#' ...
#' ```