Subcategory for Jekyll Github pages
Now this blog has subcategories. Check math category page and its subcategory math/number-theory category page. The depth of subcategories can be greater than 2.
I don’t like the way I implemented this feature, so I don’t want to explain it in detail. Check the difference to see the changes.
In brief,
- I defined the category structures like
math/number-theory
. Thecategory
key of each post can be given likecategory: math
orcategory: math/number-theory
, etc. - I added the directories to resemble the category structure and corresponding
.md
files in_categories/
. For example, the category structure for now isblog math └number-theory miscellaneous
and the corresponding
_categories/
structure is_categories/ └math/ └number-theory.md └blog.md └math.md └miscellaneous.md
(Note that the slash symbol at the end of
(directory_name)/
means that it is a directory, not a file.)Each file in
_categories/
has new keyparent_category
whose value is all of its ancestors (from top level to lower level). For example,_categories/math/number-theory.md
file has--- catname: "math/number-theory" parent_category: ["math"] layout: "category" permalink: "category/math/number-theory" ---
- I modified
_layouts/category.html
,category.md
files.- The
_layouts/category.html
file shows the list of post under the category. Now,- it makes the hyperlinks to all of its ancestors category in heading, and
- it shows the list of its descendant categories (if it has any).
- The
category.md
file shows the list of all categories of the blog. Now,- it shows the list of all categories with tree-like structure with indentation.
- The
Possible issues on this implementation:
- I think there are no
startswith
-like command for strings in Jekyll. I usedcontains
instead; this means that I cannot make subcategory with another category name. For example,math/history-of-mathematics
category will cause some problems. _layouts/category.html
file uses both of the string for category, for examplemath/number-theory
, and the list of its ancestors, for example["math"]
. This is absurd, and the structure can be simplified.- (2022.1.20.) Now the variable
parent_category
of category page is not used.
- (2022.1.20.) Now the variable
Minor update on 8th April 2024: Some urls have been updated since the blog url has been changed.