Syntax Highlighting in Hugo with blogdown in Rmarkdown

David

I'm using blogdown and the lxndrblz/anatole theme to create a blog. The blog should have usual syntax highlighting, which should be supported.

When I create a new post in markdown (not Rmarkdown!) like the following, it works as expected.

Eg index.md (not Rmd!)

---
author: Someone
date: "sometime"
title: Something
---

```R
library(tidyverse)
  starwars %>%
  select(name, mass)
```

Resulting in this version in the blog:

enter image description here

When I use the same content in Rmarkdown (not even an evaluated code chunk), there is no syntax highlighting.

Eg index.Rmd

---
author: Someone
date: "sometime"
title: Something
---

```R
library(tidyverse)
starwars %>%
  select(name, mass)
```

returns

enter image description here

When I look at the output of the knitted document of index.Rmd I see that it is a .html file with this content:

---
title: Something
author: Someone
date: Sometime
---

<script src="{{< blogdown/postref >}}index_files/header-attrs/header-attrs.js"></script>
<div id="something-r" class="section level1">

<h1>Something R</h1>
<pre class="r"><code>library(tidyverse)
  starwars %&gt;%
select(name, mass)</code></pre>
</div>

whereas the earlier, syntax highlighted markdown will be translated into this (taken from inspecting the html code of the blog):

<pre tabindex="0" class="chroma">
  <code class="language-R" data-lang="R">
    <span class="nf">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span>
    <span class="n">starwars</span> <span class="o">%&gt;%</span>
    <span class="nf">select</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">mass</span><span class="p">)</span>
  </code>
</pre>

Question

Now my question is this: is there a yaml-output mode that preserves the yaml-headers but does its magic on the chunks in the right format (note the appendix below)? With this, knitting the chunks would turn the "wrong" index.Rmd to an accurate index.md which is then picked up and formatted by Hugo. (Using output: md_document leads to the removal of the yaml-headers.)

Alternative?

Alternatively, at the moment I use the following config.yaml to enable syntax highlighting.

markup:
  goldmark:
    renderer:
      unsafe: yes
  highlight:
    anchorLineNos: true
    codeFences: true
    guessSyntax: true
    hl_Lines: ""
    lineAnchors: ""
    lineNoStart: 1
    lineNos: true
    lineNumbersInTable: true
    noClasses: true
    style: monokai
    tabWidth: 4

Any ideas how to fix this?

Appendix

I have tried to set to preseve the yaml and knit to .md like so

output: 
  md_document:
    preserve_yaml: true

but the output code is indented and not kept in the backticks and is not highlighted.

Yihui Xie

You can let .Rmd generate .md output instead of the default .html by setting options(blogdown.method = 'markdown') in your .Rprofile. See this section in the blogdown book.

Then restart R, delete index.html, and serve the site again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related