rstudio/rmarkdown-cookbook

Add a new section about new `output_format_dependency()`

Opened this issue · 1 comments

cderv commented

Idea from @atusy in rstudio/rmarkdown#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 },
}
```

```{r}
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")`