【问题标题】:Code folding in rmarkdown: can't get div wrapper to workrmarkdown 中的代码折叠:无法让 div 包装器工作
【发布时间】:2021-01-10 15:39:38
【问题描述】:

我很高兴尝试this 代码折叠方法,但在实现它时遇到了麻烦。我以前从未使用过 javascript,也不知道如何添加 div 包装器。它应该是代码块的一部分(灰色背景)还是块前后的常规文本?如果是前者,每当我尝试将 ```{r} 向下移动 1 行以为第一个 div 调用腾出空间时,块就会消失。如果是后者,我在编织时不会得到想要的结果。

这是一个可重现的例子:

---
title: "Toy markdown doc for js"
author: "Carrie Perkins"
date: "1/10/2021"
output: html_document
---


```{js}
$(document).ready(function() {

  $chunks = $('.fold');

  $chunks.each(function () {

    // add button to source code chunks
    if ( $(this).hasClass('s') ) {
      $('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
      $('pre.r', this).children('code').attr('class', 'folded');
    }

    // add button to output chunks
    if ( $(this).hasClass('o') ) {
      $('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
      $('pre:not(.r)', this).children('code:not(r)').addClass('folded');

      // add button to plots
      $(this).find('img').wrap('<pre class=\"plot\"></pre>');
      $('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
      $('pre.plot', this).children('img').addClass('folded');

    }
  });

  // hide all chunks when document is loaded
  $('.folded').css('display', 'none')

  // function to toggle the visibility
  $('.showopt').click(function() {
    var label = $(this).html();
    if (label.indexOf("Show") >= 0) {
      $(this).html(label.replace("Show", "Hide"));
    } else {
      $(this).html(label.replace("Hide", "Show"));
    }
    $(this).siblings('code, img').slideToggle('fast', 'swing');
  });
});
```


```{js}
.showopt {
  background-color: #004c93;
  color: #FFFFFF; 
  width: 100px;
  height: 20px;
  text-align: center;
  vertical-align: middle !important;
  float: right;
  font-family: sans-serif;
  border-radius: 8px;
}

.showopt:hover {
    background-color: #dfe4f2;
    color: #004c93;
}

pre.plot {
  background-color: white !important;
}


```


<div class="fold s o">

```{r cars}
summary(cars)
```

</div>

这是我编织时得到的:

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

    不太清楚为什么 ```{js} 不起作用,但如果将其替换为 &lt;script&gt;&lt;style&gt; 标记,它似乎可以按预期工作。

    ---
    title: "Toy markdown doc for js"
    author: "Carrie Perkins"
    date: "1/10/2021"
    output: html_document
    ---
    
    
    <script>
    $(document).ready(function() {
    
      $chunks = $('.fold');
    
      $chunks.each(function () {
    
        // add button to source code chunks
        if ( $(this).hasClass('s') ) {
          $('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
          $('pre.r', this).children('code').attr('class', 'folded');
        }
    
        // add button to output chunks
        if ( $(this).hasClass('o') ) {
          $('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
          $('pre:not(.r)', this).children('code:not(r)').addClass('folded');
    
          // add button to plots
          $(this).find('img').wrap('<pre class=\"plot\"></pre>');
          $('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
          $('pre.plot', this).children('img').addClass('folded');
    
        }
      });
    
      // hide all chunks when document is loaded
      $('.folded').css('display', 'none')
    
      // function to toggle the visibility
      $('.showopt').click(function() {
        var label = $(this).html();
        if (label.indexOf("Show") >= 0) {
          $(this).html(label.replace("Show", "Hide"));
        } else {
          $(this).html(label.replace("Hide", "Show"));
        }
        $(this).siblings('code, img').slideToggle('fast', 'swing');
      });
    });
    </script>
    
    
    <style>
    .showopt {
      background-color: #004c93;
      color: #FFFFFF; 
      width: 100px;
      height: 20px;
      text-align: center;
      vertical-align: middle !important;
      float: right;
      font-family: sans-serif;
      border-radius: 8px;
    }
    
    .showopt:hover {
        background-color: #dfe4f2;
        color: #004c93;
    }
    
    pre.plot {
      background-color: white !important;
    }
    </style>
    
    
    <div class="fold s o">
    
    ```{r cars}
    summary(cars)
    ```
    
    </div>
    

    虽然说了这么多,但我还是建议您考虑使用内置的代码折叠选项,看看它是否可以满足您的需求,而不是依赖自定义 JS 代码: https://bookdown.org/yihui/rmarkdown/html-document.html#code-folding

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多