Jekyll中文文档__Front Matter

齐坚成
2023-12-01

任何包含 YAML front matter 块的文件都将由Jekyll作为特殊文件进行处理。front matter 必须是文件中的第一个内容,并且必须采用在三条虚线之间设置的有效YAML的形式。以下是一个基本示例:

---
layout: post
title: Blogging Like a Hacker
---

在这三条虚线之间,您可以设置预定义的变量(请参阅下面的参考),甚至可以创建自己的自定义变量。然后,这些变量将可供您使用 Liquid tags 去访问,既能进一步访问文件,又能访问相关的页面或文章所依赖的任何 layouts 或 includes 。

UTF-8字符编码警告

如果使用UTF-8编码,请确保文件中不存在BOM头字符,否则Jekyll将发生非常非常糟糕的事情。如果您在Windows上运行Jekyll ,这一点尤其重要。

Front Matter变量是可选的

如果你想使用 Liquid tags and variables,但不需要 front matter 中的任何内容,就把它留空!这一组中间没有任何内容的三条虚线仍然可以让Jekyll处理您的文件。(这对 CSS 和 RSS feeds 等内容很有用!)

预定义的全部变量

有许多预定义的全局变量,您可以在页面或文章的 front matter 中进行设置。

VariableDescription

layout

If set, this specifies the layout file to use. Use the layout file name without the file extension. Layout files must be placed in the _layouts directory.

  • Using null will produce a file without using a layout file. This is overridden if the file is a post/document and has a layout defined in the front matter defaults.
  • Starting from version 3.5.0, using none in a post/document will produce a file without using a layout file regardless of front matter defaults. Using none in a page will cause Jekyll to attempt to use a layout named "none".

permalink

If you need your processed blog post URLs to be something other than the site-wide style (default /year/month/day/title.html), then you can set this variable and it will be used as the final URL.

published

Set to false if you don’t want a specific post to show up when the site is generated.

===================================================

渲染标记为未发布的文章

要预览未发布的页面,请使用 --unpublished 开关运行 jekyll servejekyll build 。Jekyll还有一个方便的草稿功能,专门为博客文章量身定制。

自定义变量

您也可以设置您自己的 front matter 变量,您可以在 Liquid 中访问这些变量。例如,如果您设置了一个名为 food 的变量,您可以在页面中使用该变量:

---
food: Pizza
---

<h1>{{ page.food }}</h1>

文章的预定义变量

这些都是开箱即用的,可以在一篇文章的 front matter 中使用。

VariableDescription

date

A date here overrides the date from the name of the post. This can be used to ensure correct sorting of posts. A date is specified in the format YYYY-MM-DD HH:MM:SS +/-TTTT; hours, minutes, seconds, and timezone offset are optional.

category

categories

Instead of placing posts inside of folders, you can specify one or more categories that the post belongs to. When the site is generated the post will act as though it had been set with these categories normally. Categories (plural key) can be specified as a YAML list or a space-separated string.

tags

Similar to categories, one or multiple tags can be added to a post. Also like categories, tags can be specified as a YAML list or a space-separated string.

===================================================

不要重复自己

如果你不想一遍又一遍地重复你经常使用的 front matter 变量,那么为它们定义 defaults,只在必要的时候覆盖它们(或者根本不覆盖)。这既适用于预定义变量,也适用于自定义变量。

 类似资料: