如何使用 Jekyll 提供 YAML 文件?

How to serve YAML files with Jekyll?

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

---

some: content

...

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

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

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

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

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

some: content

...

虽然 YAML 文件是一个 swagger 定义,但添加这个额外的块将:

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

没有办法告诉 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

Cross-posting 我在这里的回答可能是相关的(不确定;我不是 Jekyll 用户,但 github pages 使用它。)