Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmarkdown breaks heading anchors for Pandoc > 2.8 #1538

Closed
maelle opened this issue Feb 26, 2021 · 6 comments · Fixed by #1798
Closed

rmarkdown breaks heading anchors for Pandoc > 2.8 #1538

maelle opened this issue Feb 26, 2021 · 6 comments · Fixed by #1798
Labels
articles ✍️ bug an unexpected problem or unintended behavior

Comments

@maelle
Copy link
Collaborator

maelle commented Feb 26, 2021

For Pandoc > 2.8 rmarkdown adds a HTML dependency on a script that removes all attributes from headings. https://github.com/rstudio/rmarkdown/blob/9c1498806fef286f3891238a8298e064969c8aec/R/html_dependencies.R#L423

Therefore it removes the "hasAnchor" class that's needed to have working heading anchors. 😢

I noticed the missing anchors in articles as I was working on #1536 but it's unrelated. Locally I use Pandoc 2.11.12. The preview of that PR doesn't show the problem, nor the pkgdown dev/production websites, as the GHA workflow uses an older Pandoc version.

cc @cderv

We could remove the dependency in one of the HTML tweaks, if a fix doesn't happen upstream (and if a fix happens upstream, we'll have to increase the rmarkdown minimal version in DESCRIPTION so this issue is useful here).

@cderv
Copy link
Contributor

cderv commented Feb 26, 2021

I think we need to revise this tweak that was included for a fix because of Pandoc change in 2.8 that may have been fix in later version. I'll look at that.

@hadley
Copy link
Member

hadley commented Sep 21, 2021

@cderv did this behaviour change in rmarkdown?

@cderv
Copy link
Contributor

cderv commented Sep 22, 2021

No, not yet. We did not focus on rmarkdown work for a long time now, and I still need to have a look. I can't remember exactly why this fix is needed for Pandoc 2.9 (meaning what would break if we remove it)

@hadley hadley added articles ✍️ bug an unexpected problem or unintended behavior labels Oct 1, 2021
@hadley
Copy link
Member

hadley commented Oct 1, 2021

To fix this, maybe build_rmarkdown_format() should override the pre_processor() from html_document_base() to do its own thing. Maybe something like this:

  pre_processor <- function(metadata, input_file, runtime, knit_meta,
                            files_dir, output_dir) {

    args <- c()

    # use files_dir as lib_dir if not explicitly specified
    if (is.null(lib_dir))
      lib_dir <<- files_dir

    # copy supplied output_dir (for use in post-processor)
    output_dir <<- output_dir

    # resolve and inject extras, including dependencies specified by the format
    # and dependencies specified by the user (via extra_dependencies)
    format_deps <- list()
    format_deps <- append(format_deps, html_dependency_header_attrs())
    format_deps <- append(format_deps, extra_dependencies)

    extras <- html_extras_for_document(knit_meta, runtime, dependency_resolver, format_deps)
    args <- c(args, pandoc_html_extras_args(extras, self_contained, lib_dir, output_dir))

    preserved_chunks <<- extract_preserve_chunks(input_file)

    # a lua filters added if pandoc2.0
    args <- c(args, pandoc_lua_filters(c("pagebreak.lua", "latex-div.lua")))

    args
  }

It would be pretty hacky though, because we'd have to get all the environments right and then do some tricks to stop R CMD check complaining. Maybe a better approach would be to surgically remove the call to html_dependency_header_attrs()?

Ooooh, actually we could probably just tweak the environment of the preprocessor so it finds a html_dependency_header_attrs() that returns NULL.

We should also bump the version of pandoc installed on GHA so we're more likely to spot these problems in the future.

@cderv
Copy link
Contributor

cderv commented Oct 4, 2021

FWIW, we have also merge the PR in rmarkdown mentioned in #1798.

Setting

options(rmarkdown.html_dependency.header_attr = FALSE)

will now be a way to prevent adding the HTML dependency with the JS script that remove class from headers for Pandoc 2.9+.

@salim-b
Copy link
Collaborator

salim-b commented Mar 10, 2022

Setting

options(rmarkdown.html_dependency.header_attr = FALSE)

will now be a way to prevent adding the HTML dependency with the JS script that remove class from headers for Pandoc 2.9+.

This option could simply be set right before calling rmarkdown::render() here:

function(...) rmarkdown::render(...),

i.e. replacing L29 with:

      function(...) {
        options(rmarkdown.html_dependency.header_attr = FALSE)
        rmarkdown::render(...)
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
articles ✍️ bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants