【问题标题】:Automate Jekyll page.categories according to directory structure根据目录结构自动化 Jekyll page.categories
【发布时间】:2016-12-26 06:29:55
【问题描述】:

目标

根据目录结构让 Jekyll 帖子有自己的分类。
例如)_post/category1/category2/post-content.md 必须有 ['category1', 'category2'] 的类别(我猜 Jekyll 将类别作为数组处理)。
关键是,1)无需在Front Matter 中指定类别,2)按目录结构自动对每个帖子进行分类。

说明

官方 Jekyll 文档说Categories are derived from the directory structure above the _post directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code'].
https://jekyllrb.com/docs/variables/

  1. 如果可能的话,我会很高兴地使用这个功能。问题是,我不知道如何使_post dir 位于 in 其他目录中。我认为_post 目录必须在根目录中,所以对我来说,将帖子定位在/work/code/_posts/2008-12-24-closures.md 这样的路径中似乎是不可能的。是否可以?还是不可能?

  2. 如果1.不可能,我如何为每个帖子匹配类别和目录结构?我不想为每个帖子手动执行此操作。
    我尝试使用{{ page.path }} 使其自动化,但我遇到了两个问题。 1) 不确定 Liquid(template engine) 是否支持完整的 ruby​​ 功能,例如 File.basename 进行解析。 2) 不确定Front Matter 中是否可以进行解析等代码。例如,我在Front Matter 中尝试了categories: {{ page.path | array_to_sentence_string }},但结果出错了。

这些问题的任何解决方案?还是没有办法?

【问题讨论】:

    标签: ruby jekyll liquid


    【解决方案1】:

    您正确地遵循了指示:您可以使用任一文件夹结构,但首选后者。

    问题是特殊目录是_posts PLURAL,而不是_post 单数。

    【讨论】:

    • 天哪,谢谢!现在我看到 dir 结构有效。无论如何,'后者是首选'是什么意思?任何推荐链接或更详细的解释?
    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 2016-02-19
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多