user6119787
user6119787

Reputation: 75

Automate Jekyll page.categories according to directory structure

Goal

Make Jekyll post have their own category according to directory structure.
e.g.) _post/category1/category2/post-content.md must have categories of ['category1', 'category2'](I guess Jekyll deals with categories as an array).
The point is, making 1) no need to specify category in Front Matter and 2) auto categorize every posts by directory structure .

Description

Official Jekyll document says 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. If it is possible, I would joyfully use this function. The problem is, I have no idea how to make _post dir to be located in other directories. I thought _post dir have to be in root dir, so for me positioning post in path like /work/code/_posts/2008-12-24-closures.md seems impossible. Is it possible? or not possible?

  2. If 1. is impossible, how can I match categories and dir-struct for every post? I don't want to do it manually for every posts.
    I tried to use {{ page.path }} to make it automated, but 2 problem I faced. 1) not sure Liquid(template engine) support full ruby functionality like File.basename for parsing. 2) not sure if codes like parsing can be done in Front Matter. For instance, I tried categories: {{ page.path | array_to_sentence_string }} in Front Matter but it ended up in an error.

any solution for these issues? Or there is no way?

Upvotes: 1

Views: 464

Answers (1)

Derek
Derek

Reputation: 4751

You're following the directions properly: you can use either folder structure, though the latter is preferred.

The problem is that the special directory is _posts PLURAL, not _post singular.

Upvotes: 1

Related Questions