rmdfiltr icon indicating copy to clipboard operation
rmdfiltr copied to clipboard

Quarto support

Open andrewheiss opened this issue 2 years ago • 0 comments

This isn't a bug report or anything—just a note about how to use {rmdfiltr}'s word count with Quarto.

Quarto doesn't support inline R code in YAML (https://github.com/quarto-dev/quarto-cli/issues/1391#issuecomment-1185348644), so adding !expr rmdfiltr::add_wordcount_filter() to the YAML in a .qmd file doesn't work. Additionally, there's no way to specify pandoc_args, so there's no straightforward way to make citeproc run before the wordcount filter.

Fortunately, this GitHub comment shows that it's possible to make a Lua filter that basically behaves like --citeproc by feeding the whole document to pandoc.utils.citeproc(). That means we can create a little Lua script like citeproc.lua:

-- Lua filter that behaves like `--citeproc`
function Pandoc (doc)
  return pandoc.utils.citeproc(doc)
end

…and then include that as a filter:

format:
  html:
    citeproc: false
    filter:
      - '/path/to/citeproc.lua'
      - '/path/to/wordcount.lua'

This creates a pandoc command that looks something like this, feeding the document to the citeproc "filter" first, then feeding that to the word count script:

pandoc whatever.md --output whatever.html  --lua-filter citeproc.lua --lua-filter wordcount.lua

There's still no way that I've found to dynamically generate /path/to/wordcount.lua like !expr rmdfiltr::add_wordcount_filter(), so users need to download the Lua file themselves and put it somewhere in their project.

The Lua files can also be included in a Quarto format extension like this so there's no need to manually deal with Lua files in the YAML. Instead, users can just specify a wordcount-enabled format:

title: Something
format:
  wordcount-html: default

andrewheiss avatar Apr 05 '23 13:04 andrewheiss