【问题标题】:How to include an Rmarkdown/HTML file in Jekyll post如何在 Jekyll 帖子中包含 Rmarkdown/HTML 文件
【发布时间】:2016-08-04 18:04:02
【问题描述】:

我正在开发一个使用 Jekyll 制作并托管在 GitHub 上的静态网站。其中一个帖子看起来像this

另外,我创建了一个 Rmarkdown 文件,我想将生成的 html 文件嵌入到帖子中。我读到here 我只需要这样做:

您只需在项目的 DocumentRoot 中创建一个名为 _includes/ 的文件夹,然后在其中创建一个 HTML 文件,例如“mycomponent.html”并在您的帖子中调用它像这样:

{% include mycomponent.html %}

我在 _includes/ 文件夹中添加了我的 html 文件,并在相应帖子的降价文件末尾添加了这样一段代码。但是,当我这样做时,网站布局会完全改变

有什么办法可以避免这种情况吗?网站所有文件都存储在here

编辑:

我发现了另一个问题,建议做this

我认为最好的解决方案是:

使用 jQuery:

a.html:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 
  </head>
  <body> 
     <div id="includedContent"></div>
  </body> 
</html>

b.html:

<p> This is my include file </p>

我完全不明白。不知何故,网站的布局恢复了,但现在一些图像和 htmlwidgets 丢失了。另外,页面的页脚完全乱了。

【问题讨论】:

    标签: html jekyll r-markdown


    【解决方案1】:

    从 repo 中,我假设您尝试将 _includes/Report.html 包含到另一个文件中。

    _includes/Report.html 是一个完整的 HTML 页面(带有 doctypehtml 标签),而不是 include 设计的部分页面。 Liquid 将用完整的 HTML 替换 include 标记,创建无效标记和布局问题的可能来源:

    <!doctype html>
    <html>
      <head>...</head>
      <body>
        ...
        <!doctype html>
        <html>
          ...
        </html>
      </body>
    </html>
    

    要解决此问题,请从 _includes/Report.html 中删除多余的标记(保留 script 标记),并使用 Liquid 包含更正的部分:

    {% include Report.html %}
    

    【讨论】:

    • 谢谢罗斯,我去看看。
    【解决方案2】:

    我发现没有必要将 html 嵌入到另一个中。使用 R,可以创建带有帖子的 Rmd 文件,然后将其转换为所需的 md 文件。 Jason Fisher 的website 通过分步说明很好地解释了这一点。他的 GitHub site 也有有用的信息。

    另一个有用的site 是 Juuso Parkkinen 的。他是那个告诉我他从来没有将 html 嵌入到另一个中的人,并且只使用 R 直接通过使用以下 code 创建他的 Jekyll 网站:

    # compiles all .Rmd files in _R directory into .md files in _posts directory,
    # if the input file is older than the output file.
    
    # run ./knitpages.R to update all knitr files that need to be updated.
    
    KnitPost <- function(input, outfile, base.url="/") {
      # this function is a modified version of an example here:
      # http://jfisher-usgs.github.com/r/2012/07/03/knitr-jekyll/
      require(knitr);
      opts_knit$set(base.url = base.url)
      fig.path <- paste0("blog/figs/", sub(".Rmd$", "", basename(input)), "/")
      opts_chunk$set(fig.path = fig.path)
      opts_chunk$set(fig.cap = "testing")
      render_jekyll()
      knit(input, outfile, envir = parent.frame())
    }
    
    for (infile in list.files("blog/_R", pattern="*.Rmd", full.names=TRUE)) {
      outfile = paste0("blog/_posts/", sub(".Rmd$", ".md", basename(infile)))
    
      # knit only if the input file is the last one modified
      if (!file.exists(outfile) | file.info(infile)$mtime > file.info(outfile)$mtime) {
        KnitPost(infile, outfile)
      }
    }
    

    他的 GitHub 帐户也是一个有用的参考。

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多