rstudio / rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
https://bookdown.org/yihui/rmarkdown-cookbook/
578 stars 224 forks source link

Add a new section about new `output_format_dependency()` #395

Open cderv opened 1 year ago

cderv commented 1 year ago

Idea from @atusy in https://github.com/rstudio/rmarkdown/pull/2508 following the example shared in

---
title: referring sections
output:
  html_document:
    keep_md: true
---

```{cat, engine.opts=list(file = "example.lua")}
local headers = {}

local function collect_header(header)
  headers["#" .. el.identifier] = header
end

local function link_header(link)
  if #link.content == 0 and headers[link.target] then
    link.content = headers[link.target].content
  end
  return link
end

return {
  { Header = collect_header },
  { Link = link_header },
}
ref <- function(id) {
  dep <- rmarkdown::output_format_dependency(
    name = "example",
    pandoc = list(lua_filters = "example.lua"),
  )
  rmarkdown:::attach_output_format_dependency(dep)
  return(sprintf("[](#%s)", id))
}

awesome section {#awesome}

see r ref("brilliant")

brilliant section {#brilliant}

see r ref("awesome")

cderv commented 1 year ago

Example was added in https://blog.atusy.net/2023/08/28/rmarkdown-output-format-dependency/ for now