没有日期的Jekyll文件名

kri*_*lfa 28 jekyll github-pages

我想使用Jekyll和GitHub Pages构建文档站点.问题是Jekyll只接受_posts带有精确模式的文件名YYYY-MM-DD-your-title-is-here.md.

如何在没有此文件名模式的情况下在Jekyll中发布页面?就像是:

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

谢谢你的进步.

Set*_*ton 24

不要使用帖子; 帖子是有约会的东西.听起来你可能想要使用集合; 你获得了帖子的所有力量; 但没有讨厌的日期/命名要求.

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

我使用集合几乎所有不是帖子的东西.这是我自己的网站配置为使用"页面"集合以及我的网站的更具体部分的方式:

我的config.yaml

我的页面集合


Dav*_*uel 21

我想你对帖子网址感到恼火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
---
Run Code Online (Sandbox Code Playgroud)

文件将是anything-you-want/index.html.

网址将是http://domaine.tld/anything-you-want.

  • 实际上,您可以使用`_config.yml`中的永久链接配置绕过文件名模式.例如,如果您只想将文件名作为链接,则可以添加`permalink:/:title.html`. (9认同)

Mar*_*oij 10

我解决这个问题的方法是添加_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
Run Code Online (Sandbox Code Playgroud)

这会覆盖(“猴子补丁”)标准 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
Run Code Online (Sandbox Code Playgroud)

引用的常量为:

DATELESS_FILENAME_MATCHER = %r!^(?:.+/)*(.*)(\.[^.]+)$!.freeze
DATE_FILENAME_MATCHER = %r!^(?>.+/)*?(\d{2,4}-\d{1,2}-\d{1,2})-([^/]*)(\.[^.]+)$!.freeze
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,如require a date ( );DATE_FILENAME_MATCHER中所使用的那样。我把它放在前面。read_posts()(\d{2,4}-\d{1,2}-\d{1,2})date: 2021-07-06

我无法真正让集合工作,这也解决了我遇到的另一个问题,即存储二进制文件(例如图像)在_drafts尝试处理它们时会出错。

可以说有点难看,但效果很好。缺点是它可能会在更新时中断,尽管多年来我一直在修补各种东西并且到目前为止从未真正遇到过任何问题。这是 Jekyll 4.2.0 的情况。


kan*_*ius 5

我在没有“放弃”帖子的情况下所做的(看起来使用集合或页面是更好和更深入的解决方案)是@igneousaur 在评论中所说的加上使用相同日期作为文件名前缀的组合:

  1. 使用permalink: /:title.html_config.yml(在公布的网址中没有日期)。
  2. 使用0001-01-01-name.md文件_posts夹中所有文件的格式(jekyll 对文件名很满意,我对文件的排序很满意)。

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