distill
distill copied to clipboard
TOC does not include headers produced by a loop
Please see the attached zip with an .Rmd
file and the resulting .html
produced via knit to distill_article
from Rstudio. This should serve as an example of the problem.
The gist of what I'm seeing is that headers produced from within a loop that iterates > 1
times (lapply
in my case) are not rendered in the distill table of contents. If the loop only iterates once, then the header produced from the loop is rendered in the distill table of contents. I would expect both cases to produce entries in the distill table of contents.
Package/R versions:
packageVersion("distill")
#> [1] '1.2'
packageVersion("rmarkdown")
#> [1] '2.7'
R.version
#> _
#> platform x86_64-apple-darwin17.0
#> arch x86_64
#> os darwin17.0
#> system x86_64, darwin17.0
#> status
#> major 4
#> minor 0.1
#> year 2020
#> month 06
#> day 06
#> svn rev 78648
#> language R
#> version.string R version 4.0.1 (2020-06-06)
#> nickname See Things Now
Thanks for sharing the example: I can reproduce this.
I think this is related to how distill works by modifying the knitr's hook. The toc is created by Pandoc and this is the file that is passed to Pandoc (that I obtain by setting keep_md: true
)
Source Rmd file
---
author: Lucas Johnson
date: 2021-05-06
title: "Test Loop+Md Headers+Distill TOC"
description:
output:
distill::distill_article:
toc: true
keep_md: true
---
```{r, include = F}
test <- c("A", "B", "C")
```
# Section 1
Fails with > 1 iteration
```{r echo=F, warning=F, message=F, results='asis'}
collect <- lapply(test, function(element) {
cat("\n## ", element, "\n")
cat("This is a section")
cat(" \n")
})
test <- test[1:1]
```
# Section 2
Works with 1 iteration
```{r echo=F, warning=F, message=F, results='asis'}
collect <- lapply(test, function(element) {
cat("\n\n## ", element, "\n")
cat("This is a section")
})
```
---
author: Lucas Johnson
date: 2021-05-06
title: "Test Loop+Md Headers+Distill TOC"
description:
output:
distill::distill_article:
toc: true
keep_md: true
---
# Section 1
Fails with > 1 iteration
<div class="layout-chunk" data-layout="l-body">
## A
This is a section
## B
This is a section
## C
This is a section
</div>
# Section 2
Works with 1 iteration
<div class="layout-chunk" data-layout="l-body">
## A
This is a section
</div>
```{.r .distill-force-highlighting-css}
```
distill will wrap a chunk in a div and this seems to prevent Pandoc --toc
to work. Could be something with Pandoc: https://github.com/jgm/pandoc/issues/3057 and https://github.com/jgm/pandoc/issues/997.
By default div are considered Pandoc Divs, and this cause the issue. Disabling native_divs
makes this work
output:
distill::distill_article:
toc: true
md_extensions: -native_divs
However, I also wonder if we should really add this <div>
for chunk that have results='asis'
🤔
@jjallaire would it make sense to not wrap such chunks in these special div ? Usualy using result = 'asis'
means you produce Markdown or HTML directly and maybe this layout-chunk
div is not required in this case.
I think this would fix this issue.
Thanks for this! md_extensions: -native_divs
works great.