Jekyll中文文档__Permalinks

Permalinks是用于定义页面和文章URL路径的方法。可以通过FrontMatter在每个页面设置,或者全局在_config.yml中设置。占位符如:year,:month,:title等可用于自定义输出路径。全局permalink设置允许简化URL结构,如/:categories/:year/:month/:day/:title:output_ext。页面、集合(包括posts和drafts)有不同的占位符和处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(不包括 postsdrafts )没有时间和类别(对于页面,上面的 :title 相当于 :basename ),permalink 样式的这些方面在输出时会被忽略。

例如,posts collection 的 permalink 样式 /:categories/:year/:month/:day/:title:output_ext 对于页面和 collections(不包括 postsdrafts )会变为 /:title.html

Placeholders

以下是可用占位符的完整列表:

VariableDescription

year

Year from the post’s filename with four digits. May be overridden via the document’s date front matter.

short_year

Year from the post’s filename without the century. (00..99) May be overridden via the document’s date front matter.

month

Month from the post’s filename. (01..12) May be overridden via the document’s date front matter.

i_month

Month without leading zeros from the post’s filename. May be overridden via the document’s date front matter.

short_month

Three-letter month abbreviation, e.g. “Jan”.

long_month

4.0

Full month name, e.g. “January”.

day

Day of the month from the post’s filename. (01..31) May be overridden via the document’s date front matter.

i_day

Day of the month without leading zeros from the post’s filename. May be overridden via the document’s date front matter.

y_day

Ordinal day of the year from the post’s filename, with leading zeros. (001..366)

w_year

4.0

Week year which may differ from the month year for up to three days at the start of January and end of December

week

4.0

Week number of the current year, starting with the first week having a majority of its days in January. (01..53)

w_day

4.0

Day of the week, starting with Monday. (1..7)

short_day

4.0

Three-letter weekday abbreviation, e.g. “Sun”.

long_day

4.0

Weekday name, e.g. “Sunday”.

hour

Hour of the day, 24-hour clock, zero-padded from the post’s date front matter. (00..23)

minute

Minute of the hour from the post’s date front matter. (00..59)

second

Second of the minute from the post’s date front matter. (00..59)

title

Title from the document’s filename. May be overridden via the document’s slug front matter. Preserves case from the source.

slug

Slugified title from the document’s filename (any character except numbers and letters is replaced as hyphen). May be overridden via the document’s slug front matter.

categories

The specified categories for this post. If a post has multiple categories, Jekyll will create a hierarchy (e.g. /category1/category2). Also Jekyll automatically parses out double slashes in the URLs, so if no categories are present, it will ignore this.

slugified_categories

4.1

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. "Work 2 Progress" will be converted into "work-2-progress")

If a post has multiple categories, Jekyll will create a hierarchy (e.g. /work-2-progress/category2). Also Jekyll automatically parses out double slashes in the URLs, so if no categories are present, it will ignore this.

:output_ext

Extension of the output file. (Included by default and usually unnecessary.)

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

Built-in formats

对于文章,为了方便起见,Jekyll还提供了以下内置样式:

Permalink StyleURL Template

date

/:categories/:year/:month/:day/:title:output_ext

pretty

/:categories/:year/:month/:day/:title/

ordinal

/:categories/:year/:y_day/:title:output_ext

weekdate

4.0

/:categories/:year/W:week/:short_day/:title:output_ext
(W will be prefixed to the value of :week)

none

/:categories/:title:output_ext

不必键入 permalink: /:categories/:year/:month/:day/:title/, 只需键入 permalink: pretty.

Specifying permalinks through the front matter

内置的 permalink 样式在 front matter 中不被识别。因此,permalink: pretty 是行不通的。

Collections

对于 collections(包括 postsdrafts ),您可以选择覆盖 _config.yml 中 collection 配置中的全局 permalink :

collections:
  my_collection:
    output: true
    permalink: /:collection/:name

Collections 有以下可用的占位符:

VariableDescription

:collection

Label of the containing collection.

:path

Path to the document relative to the collection's directory, including base filename of the document.

:name

The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen.

:title

The :title template variable will take the slug front matter variable value if any is present in the document; if none is defined then :title will be equivalent to :name, aka the slug generated from the filename. Preserves case from the source.

:output_ext

Extension of the output file. (Included by default and usually unnecessary.)

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

Pages

对于页面,您必须使用 front matter 来覆盖全局 permalink ,如果您在 _config.yml 中通过 front matter 默认值设置 permalink ,它将被忽略。

页面有以下占位符可用:

VariableDescription

:path

Path to the page relative to the site's source directory, excluding base filename of the page.

:basename

The page's base filename

:output_ext

Extension of the output file. (Included by default and usually unnecessary.)

================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值