Permalinks 是页面、文章或 collections 的输出路径。它们允许您构建您的源代码目录,这个目录不同于输出中的目录。
Front Matter
设置 permalink 的最简单方法是使用 front matter 。您将 front matter 中的 permalink
变量设置为您想要的输出路径。
例如,您的网站上可能有一个位于 /my_pages/about-me.html
的页面,并且您希望输出 url 为 /about/
。在页面的 front matter 中,您将设置:
---
permalink: /about/
---
Global
在你的网站上的每个页面的 front matter 中设置一个 permalink 并不是一件有趣的事。幸运的是,Jekyll允许您在 _config.yml
中全局设置 permalink 结构。
要设置全局 permalink ,您可以在 _config.yml
中使用 permalink
变量。您可以使用占位符来获得所需的输出。例如:
permalink: /:categories/:year/:month/:day/:title:output_ext
请注意,页面和 collections(不包括 posts
和 drafts
)没有时间和类别(对于页面,上面的 :title
相当于 :basename
),permalink 样式的这些方面在输出时会被忽略。
例如,posts
collection 的 permalink 样式 /:categories/:year/:month/:day/:title:output_ext
对于页面和 collections(不包括 posts
和 drafts
)会变为 /:title.html
。
Placeholders
以下是可用占位符的完整列表:
Variable | Description |
---|---|
| Year from the post’s filename with four digits. May be overridden via the document’s |
| Year from the post’s filename without the century. (00..99) May be overridden via the document’s |
| Month from the post’s filename. (01..12) May be overridden via the document’s |
| Month without leading zeros from the post’s filename. May be overridden via the document’s |
| Three-letter month abbreviation, e.g. “Jan”. |
| Full month name, e.g. “January”. |
| Day of the month from the post’s filename. (01..31) May be overridden via the document’s |
| Day of the month without leading zeros from the post’s filename. May be overridden via the document’s |
| Ordinal day of the year from the post’s filename, with leading zeros. (001..366) |
| Week year which may differ from the month year for up to three days at the start of January and end of December |
| Week number of the current year, starting with the first week having a majority of its days in January. (01..53) |
| Day of the week, starting with Monday. (1..7) |
| Three-letter weekday abbreviation, e.g. “Sun”. |
| Weekday name, e.g. “Sunday”. |
| Hour of the day, 24-hour clock, zero-padded from the post’s |
| Minute of the hour from the post’s |
| Second of the minute from the post’s |
| Title from the document’s filename. May be overridden via the document’s |
| Slugified title from the document’s filename (any character except numbers and letters is replaced as hyphen). May be overridden via the document’s |
| The specified categories for this post. If a post has multiple categories, Jekyll will create a hierarchy (e.g. |
| The specified categories for this post but slugified. If a category is a composite of multiple words, Jekyll will downcase all alphabets and replace any non-alphanumeric character with a hyphen. (e.g. If a post has multiple categories, Jekyll will create a hierarchy (e.g. |
| Extension of the output file. (Included by default and usually unnecessary.) |
========================================================== |
Built-in formats
对于文章,为了方便起见,Jekyll还提供了以下内置样式:
Permalink Style | URL Template |
---|---|
|
|
|
|
|
|
| |
|
|
不必键入 permalink: /:categories/:year/:month/:day/:title/
, 只需键入 permalink: pretty
.
Specifying permalinks through the front matter
内置的 permalink 样式在 front matter 中不被识别。因此,permalink: pretty
是行不通的。
Collections
对于 collections(包括 posts
和 drafts
),您可以选择覆盖 _config.yml
中 collection 配置中的全局 permalink :
collections:
my_collection:
output: true
permalink: /:collection/:name
Collections 有以下可用的占位符:
Variable | Description |
---|---|
| Label of the containing collection. |
| Path to the document relative to the collection's directory, including base filename of the document. |
| The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen. |
| The |
| Extension of the output file. (Included by default and usually unnecessary.) |
================================================ |
Pages
对于页面,您必须使用 front matter 来覆盖全局 permalink ,如果您在 _config.yml
中通过 front matter 默认值设置 permalink ,它将被忽略。
页面有以下占位符可用:
Variable | Description |
---|---|
| Path to the page relative to the site's source directory, excluding base filename of the page. |
| The page's base filename |
| Extension of the output file. (Included by default and usually unnecessary.) |
================================================ |