可以做到的。
这适用于 Jekyll 3,但它当然可以移植到 Jekyll 2。
从基础安装开始 (jekyll new)
_config.yml
collections:
guide:
sasssamples:
样式指南文件
示例文件:_guide/header/header1.hmtl
---
title: Header level 1
---
<h1>Header level 1</h1>
SCSS 示例
- 我们希望我们的 SCSS 示例包含在我们的
css/main.scss 中,并使用我们其他 SCSS 文件中定义的变量。我们的样品将整合在我们的末尾css/main.scss
- 我们不希望我们的 SCSS 样本呈现为 css,因此没有 .scss 扩展名。切换到 .txt 扩展名
- 我们希望从列表中访问 SCSS 示例。让我们将它们放入
sasssamples 集合中。
示例文件:_sasssamples/header/header1.txt
---
---
h1{
color: $brand-color;
border: 1px solid $brand-color;
}
SCSS 示例集成
在引导 scss 文件的最后添加此代码(css/main.scss 在基本 Jekyll 安装上)
css/main.scss
[ original code ... ]
{% comment %} Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
{% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
{% comment %}
Printing documents in sasssamples collection.
All SCSS from style guide are sandboxed in .guide class
This allows us to apply styles only to style guide html samples
{% endcomment %}
.guide{
{% for doc in scssCollection.docs %}
{{ doc.content }}
{% endfor %}
}
风格指南
<h2>Style guide</h2>
{% comment %}Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
{% assign guideCollection = site.collections | where: 'label', 'guide' | first %}
{% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
{% comment %} Looping hover style guide samples {% endcomment %}
{% assign samples = guideCollection.docs %}
{% for sample in samples %}
<article>
<h3>Element : {{ sample.title }}</h3>
<h4>Render</h4>
<div class="guide">
{{ sample.content }}
</div>
<h4>html code</h4>
{% highlight html %}{{ sample.content }}{% endhighlight %}
{% comment %}
Changing a path like : _guide/headers/header1.html
to : _sasssamples/headers/header1.txt
{% endcomment %}
{% assign scssPath = sample.path | replace: '_guide', '_sasssamples' %}
{% assign scssPath = scssPath | replace: '.html', '.txt' %}
{% comment %} Try to find a SCSS sample with equivalent path {% endcomment %}
{% assign scssSample = scssCollection.docs | where: 'path', scssPath | first %}
{% comment %}We print SCSS sample only if we found an equivalent path{% endcomment %}
{% if scssSample != nil %}
<h4>SCSS code</h4>
{% highlight css %}{{ scssSample.content }}{% endhighlight %}
{% endif %}
</article>
{% endfor %}
完成!