3

I'm building a blog with dillonzq/loveit theme in conjunction with R's blogdown package. The loveit theme has a really cool floating table of contents. It looks like this

enter image description here

and you can see an example of it in action here. Unfortunately I can't get this to play nicely with .Rmd files rendered by blogdown. I can, however, get it to work with .md files.

Minimal Reproducible Example

  1. Set up a new site with the loveit theme
blogdown::new_site(theme = "dillonzq/LoveIt")
  1. Make two very similar posts, where one is a .Rmd and the other a .md.

content/posts/test_Rmd.Rmd

---
title: "Test Rmd"
author: "John Johnson"
---

## Something cool
Here' something cool

### Details
Here are some details

## Something cooler
Here's something cooler

content/posts/test_md.md

---
title: "Test md"
author: "John Johnson"
---

## Something cool
Here' something cool

### Details
Here are some details

## Something cooler
Here's something cooler
  1. Launch the site with blogdown::serve_site(), then compare http://127.0.0.1:4797/test_md/ and http://127.0.0.1:4797/test_rmd/.

The .md version works (note the source code) enter image description here enter image description here

and the .Rmd version doesn't work. enter image description here enter image description here

Attempted Solutions

I've tried setting

output:
  blogdown::html_page:
    toc: true

in the front matter of my .Rmd. This creates a table of contents, but not the pretty, floating toc as in the loveit theme. I've also tried tinkering with the toc related variables in config.toml but to no avail.

I see where the loveit theme generates a table of contents in the template posts/single.html but I don't understand why this doesn't play nicely with blogdown. Any help would be much appreciated!

Ben
  • 20,038
  • 30
  • 112
  • 189

1 Answers1

2

Apparently, the problem comes from the HTML file created by the .Rmd file. There are two options to deal with this.

1) Keep the .md file

You can add the option keep_md: true in the YAML. Therefore, the file test_rmd.Rmd now looks like that:

---
title: "Test Rmd"
author: "John Johnson"
output: 
  html_document:
    keep_md: true
---
  
## Something cool
Here' something cool

### Details
Here are some details  

## Something cooler
Here's something cooler 

However, blogdown::serve_site() does not actually compile the .Rmd file. Therefore, you need to knit it to generate the .md file before using blogdown::serve_site(). Apparently, for some reason that I don't understand, .md files have the priority when using blogdown::serve_site(), and the HTML file generated is therefore useless.

2) Replace .Rmd by .Rmarkdown

Again, for a reason that I don't understand, using .Rmarkdown extension instead of .Rmd solves the problem. However, these two extensions have different capacities, that are detailed here.

bretauv
  • 7,756
  • 2
  • 20
  • 57
  • 1
    I ended up re-building all my articles as `.Rmarkdown`, except for one which needed to build a .gif (which is incompatible with `.Rmarkdown`). Appreciate the answer but I'm going to leave this open to see if someone comes along with a better solution. – Ben Jun 29 '20 at 00:53