如何使用Jekyll提供YAML文件?

时间:2017-11-09 13:45:14

标签: jekyll github-pages

我有一个YAML文件,它包含一些数据,需要在客户端处理。

---

some: content

...

Jekyll使用Front Matter处理所有内容,这导致整个YAML内容被视为Front Matter,然后页面没有内容。

有没有办法告诉Jekyll将文件视为静态,即使它有YAML标题?

我尝试了excludekeep_files的不同组合,同时在我的_config.yml中设置了Front Matter默认值,但没有任何效果。

我现在的解决方法是在内容前面添加一个额外的Front Matter块:

---
layout: null
---
---

some: content

...

YAML文件虽然是一个昂首阔步的定义,但添加这个额外的块都是:

  • 使在本地或在Swagger编辑器中测试/生成定义变得复杂
  • 如果通过github下载文件,则会产生混淆。

理想情况下,我想存储文件而不做任何修改,并以某种方式教Jekyll不管它。

2 个答案:

答案 0 :(得分:2)

没有办法告诉Jekyll在没有插件的情况下避免处理带有前端内容的文件。

一个简单的解决方案是使用Jekyll hook's在生成整个网站后立即将带有前端内容的原始未处理文件复制到生成的网站。

例如,添加_plugins/copyfile.rb

Jekyll::Hooks.register :site, :post_write do |site|
  src = "_unprocessed/about.md"
  dest = "_site/assets"
  puts "Copying #{src} to #{dest}"
  FileUtils.cp(src, dest)
end

这会将_unprocessed/about.md 与前端问题复制到最终网站assets目录。

_unprocessed以下划线开头时,它不会出现在结果网站中。

构建网站:

/tmp/s⟫jekyll b
Configuration file: /tmp/s/_config.yml
            Source: /tmp/s
       Destination: /tmp/s/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
Copying _unprocessed/about.md to _site/assets
                    done in 0.884 seconds.
 Auto-regeneration: disabled. Use --watch to enable.


/tmp/s⟫ tree _site/
_site/
├── 404.html
├── assets
│   ├── about.md
│   └── main.css
├── feed.xml
├── index.html
└── jekyll
    └── update
        └── 2017
            └── 11
                └── 09
                    └── welcome-to-jekyll.html

6 directories, 6 files

答案 1 :(得分:1)

在这里交叉发布我的答案,因为它可能是相关的(不确定;我不是Jekyll用户,但github页面使用它。)https://stackoverflow.com/a/48954668/1461007

相关问题