我想使用 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 *.md, so it won't process binary
# files from e.g. drafts.
def read_posts(dir)
read_publishable(dir, "_posts", /.*\.md$/)
end
def read_drafts(dir)
read_publishable(dir, "_drafts", /.*\.md$/)
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
如您所见,
DATE_FILENAME_MATCHER
中使用的read_posts()
需要日期((\d{2,4}-\d{1,2}-\d{1,2})
);我把 date: 2021-07-06
放在前面。
我无法真正让集合工作,这也解决了我遇到的另一个问题,即在
_drafts
中存储图像等二进制文件在尝试处理它们时会出错。
可以说有点难看,但效果很好。缺点是它可能会在更新时中断,尽管多年来我一直在修补各种东西并且到目前为止从未真正遇到过任何问题。这是 Jekyll 4.2.0 的情况。
我在没有“放弃”帖子的情况下所做的(看起来使用集合或页面是更好、更深入的解决方案)是@igneousaur在评论中所说的内容加上使用相同日期作为文件名前缀的组合:
permalink: /:title.html
中使用 _config.yml
(已发布的 URL 中没有日期)。0001-01-01-name.md
文件夹中的所有文件使用格式 _posts
(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 - 那么您可以在您的
page.last_modified_at
以及您正在运行的任何文件中使用_layouts/post.html
{% for post in site.posts %}
中。
现在,日期是从上次 git 提交日期(不是作者日期)检索的,并且
page.date
未使用。
在配置文件的json schema中实际上有一些有用的信息。请参阅下面的代码块以获取一些示例。
我已将其设置为
/:categories/:title
。这会删除日期和文件扩展名,同时保留类别。
我仍然使用正确的日期作为文件名,因为您可以在模板中使用该日期。 IE。使用
{{ 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)$"
}
}