简体   繁体   English

gitbook代码块中带有突出显示的丑陋深色条纹:espresso(R bookdown)

[英]Ugly dark stripe in code chunk of gitbook with highlight: espresso (R bookdown)

I use R bookdown package to create a gitbook. 我使用R bookdown包来创建一个gitbook。 Recently I noticed an ugly dark stripe under the boxes of code chunks in rendered gitbook (see a printscreen below). 最近我发现在渲染的gitbook中的代码块下面有一条丑陋的暗条纹(见下面的版画屏幕)。 It appeared with highlight set to espresso . highlight espresso Stripes like this one distract attention when one tries to read the book and looks unattractive. 像这样的条纹在试图阅读这本书并且看起来没有吸引力时会分散注意力。

I can reproduce the result by creating a new bookdown project with RStudio (most probably the same project as the minimal bookdown example ) with the contents of file _output.yml replaced with two lines: 我可以通过使用RStudio创建一个新的bookdown项目来重现结果(很可能是与最小的bookdown示例相同的项目),文件_output.yml的内容被替换为两行:

bookdown::gitbook:
  highlight: espresso
  1. Can anyone else reproduce the problem? 其他人可以重现这个问题吗?
  2. How can I rid off the stripe, but still use highlight: espresso ? 我怎样才能摆脱条纹,但仍然使用高highlight: espresso

在此输入图像描述

The problem comes from the use of pandoc 2.x . 问题来自于pandoc 2.x的使用。
I reproduced the bug here and you can see the same gitbook with pandoc 1.19.x here . 我转载的bug 在这里 ,你可以看到同样的gitbookpandoc 1.19.x 这里
I wrote a note to explain my debugging worflow . 我写了一个说明来解释我的调试工作流程


In both versions, HTML sources are very close (I voluntary omit div and lines ids that @Yihui Xie dislikes ). 在这两个版本中, HTML源代码非常接近(我自愿省略@Yihui Xie不喜欢的 div和行ID)。

 <div class="sourceCode"> <pre class="sourceCode r"> <code class="sourceCode r"> <span class="kw">install.packages</span>(<span class="st">&quot;bookdown&quot;</span>) <span class="co"># or the development version</span> <span class="co"># devtools::install_github(&quot;rstudio/bookdown&quot;)</span> </code> </pre> </div> 

The problem you got is a consequence of a collision between gitbook theme and espresso highlighting . 你遇到的问题是gitbook主题和espresso突出显示之间发生冲突的结果。

Firstly, the dark stripe is the "real" background color of espresso (see here ): the background-color of espresso highlighting is set to #2a211c by pandoc (it is hard-coded here ). 首先,暗条纹是“真正的”背景色espresso (见这里 ):将background-colorespresso高亮设置为#2a211cpandoc (它是硬编码在这里 )。

The builtin espresso highlighting looks like this for pandoc v2.x (it is black, like an espresso): 对于pandoc v2.x ,它内置的espresso突出显示如此(它是黑色的,像浓咖啡):

 code span.kw { color: #43a8ed; font-weight: bold; } code span.st { color: #049b0a; } code span.co { color: #0066ff; font-weight: bold; font-style: italic; } div.sourceCode { color: #bdae9d; background-color: #2a211c; } div.sourceCode { overflow: auto; } div.sourceCode { margin: 1em 0; } pre.sourceCode { margin: 0; } 
 <div class="sourceCode"> <pre class="sourceCode r"> <code class="sourceCode r"> <span class="kw">install.packages</span>(<span class="st">&quot;bookdown&quot;</span>) <span class="co"># or the development version</span> <span class="co"># devtools::install_github(&quot;rstudio/bookdown&quot;)</span> </code> </pre> </div> 

It is slightly different of pandoc v1.19.x where the background-color is set for pre elements instead of div.sourceCode (it's important). 它与pandoc v1.19.x略有不同,其中background-color设置为pre元素而不是div.sourceCode (这很重要)。

Secondly, gitbook theme overrules only for pre elements the dark background color of espresso with a light grey background ( Hex Gray97 ) and defines a bottom margin (contrary to pandoc CSS) see this file, line #9, column #31963 : 其次, gitbook主题仅针对pre元素 gitbook espresso的深色背景颜色和浅灰色背景( Hex Gray97 )并定义底部边距(与pandoc CSS相反), 请参阅此文件,第9行,第31963行

.book .book-body .page-wrapper .page-inner section.normal pre {
  overflow: auto;
  word-wrap: normal;
  margin: 0 0 1.275em;
  padding: .85em 1em;
  background: #f7f7f7;
}

This bottom margin reveals the background color of the enclosing div element: 这个底部边距显示了封闭div元素的背景颜色:

  • in pandoc 1.19.x , the enclosing div has no background-color rule (the background color is applied to pre element). pandoc 1.19.x ,封闭div没有background-color规则(背景颜色应用于pre元素)。 So, there's no black stripe. 所以,没有黑条纹。

  • in pandoc 2.x , the background color is set at the div level. pandoc 2.x ,背景颜色设置在div级别。 There's a black stripe. 有一条黑色条纹。


From your question, I understand that you want the espresso highlighting without its "ugly" dark companion. 从你的问题来看,我知道你想要espresso突出而没有它“丑陋”的黑暗伴侣。 In other words, you want a "white coffee" highlighting. 换句话说,你想要一个“白咖啡”突出显示。

So, there are two options : 所以, 有两种选择

  • get rid-off the bottom margin 摆脱底部边缘
  • overrule the background color of espresso highlighting 否决espresso突出的背景颜色

It is a matter of taste. 这是一个品味问题。

First solution : get rid-off the bottom margin 第一个解决方案 :摆脱底部边缘
Save these lines in a file with .css extension (eg fix.css ): 将这些行保存在扩展名为.css的文件中(例如fix.css ):

.book .book-body .page-wrapper .page-inner section.normal pre {
  margin-bottom: 0!important;
}

Include this CSS file in your bookdown modifying _output.yml : 在您的bookdown中包含此CSS文件,修改_output.yml

bookdown::gitbook:
  highlight: espresso
  css: fix.css

Second solution : overruling the espresso background color with Hex Gray97 第二种解决方案 :使用Hex Gray97 espresso背景颜色
In this case, you can include these lines in fix.css : 在这种情况下,您可以在fix.css包含以下行:

div.sourceCode {
  background-color: #f7f7f7!important;
}

Since the use of the important rule is not recommended, you can obtain the same result in a more elegant fashion: with pandoc 2.x , you can customise the highlighting theme . 由于不建议使用important规则,因此您可以以更优雅的方式获得相同的结果: 使用pandoc 2.x ,您可以自定义突出显示主题
This gist defines a whitecoffee theme (it is the espresso theme with a Hex Gray97 background). 这个要点定义了一个whitecoffee主题(它是带有Hex Gray97背景的espresso主题)。
Save the whitecoffee.theme file at the root level of your project and modify the _output.yml file: whitecoffee.theme文件保存在项目的根级别并修改_output.yml文件:

bookdown::gitbook:
  highlight: whitecoffee.theme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM