Jekyll无日期文件名

46

我想使用Jekyll和GitHub Pages构建文档网站。问题在于,Jekyll只接受_posts目录下文件名的确切模式,如YYYY-MM-DD-your-title-is-here.md

如何在不使用这种文件名模式的情况下在Jekyll中发布页面?类似于:

  • awesome-title.md
  • yet-another-title.md
  • etc.md

谢谢您提前的帮助。

7个回答

40

不要使用文章;文章是有日期的东西。看起来你可能想使用集合;你可以获得所有文章的功能,但没有烦人的日期/命名要求。

https://jekyllrb.com/docs/collections/

对于几乎所有不是文章的内容,我都使用集合。这是我自己网站如何配置使用集合来处理“页面”以及更特定的站点部分的方法:

My config.yaml

My Pages collection


29
我猜你对文章的URL链接 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


好的,帖子需要特殊处理,但是我的网站是用Jekyll构建的,没有使用任何帖子。也就是说,在您的路径中拥有正常的.html、.md或.textile文件非常容易。我的文件名就像OP提供的示例一样。 - Rudy Velthuis
1
Rudy,你说得完全正确!另一个选择是使用页面而不是文章。 - David Jacquel
18
你实际上可以通过在 _config.yml 中使用永久链接配置来绕过文件名模式。例如,如果你只想要文件名作为链接,你可以添加 permalink: /:title.html - igneosaur

9
我解决这个问题的方法是添加_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 下进行的。


不确定为什么这个被踩得那么多,但它是有效的。文章提供了更多的功能,比如post_url方法、分类和标签,而集合没有。如果你根本不打算使用时间戳,你必须将永久链接选项设置为省略日期。 - givemelight
是的,这就是真正的答案。 - MJ Studio

8

在不“放弃”文章的情况下,我所做的是结合了@igneousaur在评论中提到的方法并使用相同的日期作为文件名的前缀(看起来使用集合或页面是更好且更深入的解决方案):

  1. _config.yml中使用permalink: /:title.html(发布的URL中没有日期)。
  2. _posts文件夹中使用格式为0001-01-01-name.md的命名方式(jekyll对文件名很满意,而我对文件的排序很满意)。

当然,我们可以在名称中包含任何“额外信息”,例如一些增量id或任何有助于我们组织文件的内容,例如:0001-01-01-001-name.md


1

无法评论,但想对@martin-tournoij的答案进行补充。它可行,但你需要将/.*\.markdown$/ 更改为 /.*\.md$/。除此之外,这就是正确的答案。


0

我想使用文章,但不想在日期中包含文件名。我最接近的方法是使用任意的“日期”命名帖子,例如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未被使用。


0

在配置文件的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)$"
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接