rr-markdownblogdown

Cannot get a toc with Rmd using blogdown


I'm attempting to add a table of contents to a blogdown page using yaml. Read this post before posting. Also read through docs.

Within rstudio if I create a new project and choose the new blogdown site option, a dir is auto populated and I can then blogdown::build_site(build_rmd = T) followed by blogdown::serve_site() to see a hellow world example site served locally. It defaults to the lithium-ion theme.

enter image description here

There is an auto created post 'Hello R Markdown' at content/post/2020-12-01-r-rmarkdown/index.Rmd that contains this yml:

---
title: "Hello R Markdown"
author: "Frida Gomam"
date: 2020-12-01T21:13:14-05:00
categories: ["R"]
tags: ["R Markdown", "plot", "regression"]
---

I added some sections:

## bla

bla

## ha 

ha

I wanted to add a table of contents toc. Tried several things, including:

---
title: "Hello R Markdown"
author: "Frida Gomam"
date: 2020-12-01T21:13:14-05:00
categories: ["R"]
tags: ["R Markdown", "plot", "regression"]
output: 
  html_document:
    toc: true # table of content true
---

Also tried:

---
title: "Hello R Markdown"
author: "Frida Gomam"
date: 2020-12-01T21:13:14-05:00
categories: ["R"]
tags: ["R Markdown", "plot", "regression"]
output:
  blogdown::html_page:
    toc: true
---

In each case, when I build and serve, no toc is rendered.

How can I add a table of contents?


Solution

  • There are two ways to generate the TOC:

    1. Set options(blogdown.method = 'html') in your .Rprofile (the default method is 'markdown'). Then restart R, delete index.md, and serve the site again. This method requires the YAML setting:

      output:
        blogdown::html_page:
          toc: true
      

      This method uses Pandoc to generate the TOC.

    2. If you don't change the blogdown.method option, you need to modify the Hugo template single.html like this, and then set the top-level YAML option toc: true. Note that Hugo generates the TOC starting from level-2 headings by default, so your body should start with ## headings. If you have to start with level-1 headings, you need to configure Hugo in config.yaml (or hugo.yaml for recent versions of Hugo).

    Personally I'd recommend the second method.