我想使用Jekyll和GitHub Pages构建文档网站。问题在于,Jekyll只接受_posts
目录下文件名的确切模式,如YYYY-MM-DD-your-title-is-here.md
。
如何在不使用这种文件名模式的情况下在Jekyll中发布页面?类似于:
awesome-title.md
yet-another-title.md
etc.md
谢谢您提前的帮助。
我想使用Jekyll和GitHub Pages构建文档网站。问题在于,Jekyll只接受_posts
目录下文件名的确切模式,如YYYY-MM-DD-your-title-is-here.md
。
如何在不使用这种文件名模式的情况下在Jekyll中发布页面?类似于:
awesome-title.md
yet-another-title.md
etc.md
谢谢您提前的帮助。
不要使用文章;文章是有日期的东西。看起来你可能想使用集合;你可以获得所有文章的功能,但没有烦人的日期/命名要求。
https://jekyllrb.com/docs/collections/
对于几乎所有不是文章的内容,我都使用集合。这是我自己网站如何配置使用集合来处理“页面”以及更特定的站点部分的方法:
http://domaine.tld/category/2014/11/22/post.html
感到烦恼。permalink
(请参阅文档)。
_posts/2014-11-22-other-post.md
---
title: "Other post"
date: 2014-11-22 09:49:00
permalink: anything-you-want
---
文件将是 anything-you-want/index.html
网址将为 http://domaine.tld/anything-you-want
_plugins/no_date.rb
:class Jekyll::PostReader
# Don't use DATE_FILENAME_MATCHER so we don't need to put those stupid dates
# in the filename. Also limit to just *.markdown, so it won't process binary
# files from e.g. drags.
def read_posts(dir)
read_publishable(dir, "_posts", /.*\.markdown$/)
end
def read_drafts(dir)
read_publishable(dir, "_drafts", /.*\.markdown$/)
end
end
这会覆盖("猴子补丁")标准的Jekyll函数; 这些的默认值是:
# Read all the files in <source>/<dir>/_drafts and create a new
# Document object with each one.
#
# dir - The String relative path of the directory to read.
#
# Returns nothing.
def read_drafts(dir)
read_publishable(dir, "_drafts", Document::DATELESS_FILENAME_MATCHER)
end
# Read all the files in <source>/<dir>/_posts and create a new Document
# object with each one.
#
# dir - The String relative path of the directory to read.
#
# Returns nothing.
def read_posts(dir)
read_publishable(dir, "_posts", Document::DATE_FILENAME_MATCHER)
end
参考常量如下:
DATELESS_FILENAME_MATCHER = %r!^(?:.+/)*(.*)(\.[^.]+)$!.freeze
DATE_FILENAME_MATCHER = %r!^(?>.+/)*?(\d{2,4}-\d{1,2}-\d{1,2})-([^/]*)(\.[^.]+)$!.freeze
如您所见,read_posts()
中使用的 DATE_FILENAME_MATCHER
需要一个日期 ((\d{2,4}-\d{1,2}-\d{1,2})
);我在文章前置元数据中添加了 date: 2021-07-06
。
我无法让集合正常工作,而这也解决了我在将二进制文件(如图片)存储在 _drafts
中时可能出现的错误。
可以说有点丑陋,但它能很好地工作。缺点是它可能会在更新时出现问题,尽管我多年来一直在修补各种东西,但到目前为止没有遇到任何问题。这是在 Jekyll 4.2.0 下进行的。
在不“放弃”文章的情况下,我所做的是结合了@igneousaur在评论中提到的方法并使用相同的日期作为文件名的前缀(看起来使用集合或页面是更好且更深入的解决方案):
_config.yml
中使用permalink: /:title.html
(发布的URL中没有日期)。_posts
文件夹中使用格式为0001-01-01-name.md
的命名方式(jekyll对文件名很满意,而我对文件的排序很满意)。当然,我们可以在名称中包含任何“额外信息”,例如一些增量id或任何有助于我们组织文件的内容,例如:0001-01-01-001-name.md
。
无法评论,但想对@martin-tournoij的答案进行补充。它可行,但你需要将/.*\.markdown$/
更改为 /.*\.md$/
。除此之外,这就是正确的答案。
我想使用文章,但不想在日期中包含文件名。我最接近的方法是使用任意的“日期”命名帖子,例如0001-01-01cool-post.md
,然后使用不同的属性来访问日期。
如果您使用了last-modified-at插件 - https://github.com/gjtorikian/jekyll-last-modified-at - 那么您可以在_layouts/post.html
中使用page.last_modified_at
,以及您运行{% for post in site.posts %}
的任何文件中。
现在,日期是从最后一次git提交日期(而不是作者日期)检索的,而page.date
未被使用。
在配置文件的json schema中实际上包含了一些有用的信息。请参见下面的代码块以获取一些示例。
我已将其设置为/:categories/:title
。这将删除日期和文件扩展名,同时保留类别。
我仍然会使用正确的日期作为文件名,因为您可以在模板中使用该日期。例如,使用{{ page.date }}
来显示文章发布日期。
{
"global-permalink": {
"description": "The global permalink format\nhttps://jekyllrb.com/docs/permalinks/#global",
"type": "string",
"default": "date",
"examples": [
"/:year",
"/:short_year",
"/:month",
"/:i_month",
"/:short_month",
"/:day",
"/:i_day",
"/:y_day",
"/:w_year",
"/:week",
"/:w_day",
"/:short_day",
"/:long_day",
"/:hour",
"/:minute",
"/:second",
"/:title",
"/:slug",
"/:categories",
"/:slugified_categories",
"date",
"pretty",
"ordinal",
"weekdate",
"none",
"/:categories/:year/:month/:day/:title:output_ext",
"/:categories/:year/:month/:day/:title/",
"/:categories/:year/:y_day/:title:output_ext",
"/:categories/:year/:week/:short_day/:title:output_ext",
"/:categories/:title:output_ext"
],
"pattern": "^((/(:(year|short_year|month|i_month|short_month|long_month|day|i_day|y_day|w_year|week|w_day|short_day|long_day|hour|minute|second|title|slug|categories|slugified_categories))+)+|date|pretty|ordinal|weekdate|none)$"
}
}
_config.yml
中使用永久链接配置来绕过文件名模式。例如,如果你只想要文件名作为链接,你可以添加permalink: /:title.html
。 - igneosaur