【问题标题】:Syntax Highlighting in Hugo with blogdown in RmarkdownHugo 中的语法高亮和 Rmarkdown 中的 blogdown
【发布时间】:2022-01-14 06:02:08
【问题描述】:

我正在使用blogdownlxndrblz/anatole 主题来创建博客。 博客应该有通常的语法高亮,应该支持。

当我在 markdown(不是 Rmarkdown!)中创建新帖子时,如下所示,它按预期工作。

例如index.md(不是Rmd!)

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

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

在博客中产生了这个版本:

当我在 Rmarkdown 中使用相同的内容(甚至不是评估的代码块)时,没有语法高亮显示。

例如index.Rmd

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

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

返回

当我查看index.Rmd 的编织文档的输出时,我看到它是一个具有以下内容的.html 文件:

---
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>

而早先,语法突出显示的降价将被翻译成这个(取自检查博客的 html 代码):

<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>

问题

现在我的问题是: 是否有一种 yaml-output 模式可以保留 yaml 标头,但它对正确格式的块有魔力(请注意下面的附录)?这样,编织这些块会将“错误”index.Rmd 转换为准确的index.md,然后由 Hugo 拾取并格式化。 (使用 output: md_document 会导致删除 yaml-headers。)

替代方案?

另外,目前我使用以下config.yaml 来启用语法突出显示。

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

任何想法如何解决这个问题?

附录

我已经尝试设置为 preseve yaml 并像这样编织到 .md

output: 
  md_document:
    preserve_yaml: true

但输出代码是缩进的,没有保留在反引号中,也没有突出显示。

【问题讨论】:

    标签: r hugo blogdown


    【解决方案1】:

    您可以通过在.Rprofile 中设置options(blogdown.method = 'markdown'),让.Rmd 生成.md 输出而不是默认的.html。见this section in the blogdown book

    然后重启R,删除index.html,再次服务站点。

    【讨论】:

    • 太棒了,效果很好!顺便说一句,感谢您提供的精美包裹!
    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 2020-10-22
    • 1970-01-01
    • 2021-09-25
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多