Relative path to local Lua filter saved in blogdown repo?

Hi there,

I'm collaborating on a website rendered through blogdown and GitHub Pages. Our experience has been great so far, but we've run into one small hiccup:

We have a local copy of a Lua filter that we'd like to pass our Rmds through before knitting, using the pandoc_args: "--lua-filter=PATH/TO/filter.lua" YAML header. The filter is saved in a subfolder of the project directory.

However, when any of us tries to write the path as a relative path, with the project directory as the current directory, Pandoc fails to find the filter file.

On one person's machine we can get around the relative path failure by using the $HOME shell environment variable to absolute-path to the Lua filter, but this doesn't generalize to other collaborators' machines if the folder(s) containing the project directory on their computers aren't the same.

When R Markdown calls Pandoc, where is the working directory? And can we change the Pandoc working directory so that relative paths to Lua filters will work across machines?

(All collaborators are on Macs. I at least have Mojave installed, with R 3.6.1, pandoc 2.9.2, rmarkdown 2.3, blogdown 0.20.)

Thank y'all for your help! Please let me know what additional info you might need.

I think pandoc will run from where the Rmd file is rendered, like classic rmarkdown rendering: the document folder. As a global solution, it is better to use absolute path in this case.

With a combination of here :package: to create full path from a project dir, working for everyone, and r expression in yaml header, I think it could work.

pandoc_args: !expr sprintf("--lua-filter=", here::here("PATH/RELATIVE/TO/PROJECT/filter.lua")
  • !expr is a special syntax to execute R code when reading a yaml file with yaml package
  • here is a very useful package to work with relative directory but get full path when needed. Very useful for Rmarkdown!

See for more

Would that works for you ? I think it is the easiest to not tweak too much Rmarkdown rendering paths, and I think it will work in blogdown.

Ahh, thank you, this works!

It took a little bit of brain wiggling to realize that while pandoc_args: takes a vector specified with hard brackets, !expr will pass a correctly formatted character vector if you use c() to stitch together your args instead:

pandoc_args: !expr c(paste0("--lua-filter=", here::here("subfolder", "highlight.lua")), "--no-highlight")

For some reason I thought Pandoc wouldn't be happy with an R character vector but turns out all is well.

1 Like

Oh sorry not to have mentioned that !

Glad it works ok now!

For --no-highlight, you should be able to use highlight = NULL in the html_document or pdf_document function. It will pass the correct argument to pandoc.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.