Hook/plugin to inject front matter header

Is it possible to inject/add front matter key/values using a hook for newly created markdown files (posts or custom collections)?

I am aware of the ability to add default header values using defaults in _config.yml file. However, this does not add those values to the actual front matter header and only allows access in liquid on the page variable.

The reason for this is because we have our pages structured like _pages/category/sub-category/file.md and we use cloud cannon for non technical users to edit/create content.

I’ve got a proof of concept working using File.write() within a hook but this triggers an infinite regenerate loop when running jekyll serve (I believe its because the file is written and the hook is triggered creating a loop).

I am using Jekyll::Hooks.register :site, :post_read to achieve this. Can someone give me advice on how to accomplish this? Ultimately, we want a hook to run for newly created files and do some logic to add the yaml front matter headers for the file.

Jekyll doesn’t have this feature… Out of the box. But since you are using cloud cannon you can take advantage of their collection defaults

Yeah we’re already using cloud cannon defaults feature but the issue is when we have nested folders within a collection. Cloud cannon only allows one defaults.md for a collection so you cannot nest it like so: _pages/category/sub-category/_defaults.md, it will only work when you have it within root of the collection folder _pages/category/_defaults.md.

We’re looking for a way to categorize “pages” similar to how posts is done out of the box in Jekyll but without the need to make the filename in the date-title.md format. We’re also using posts but its for blogs/news related articles that require date and other blog related attributes.

Hmm did you try using the Array defaults? But instead of using _pages just create a collection instead?

Thanks! Managed to figure this out using a custom plugin that adds category and sub category to the front matter header based on the directory path.

@jabowa could I take a look at that custom plugin?

I’m facing a similar issue where I want to dynamically inject an array of category tags to a large number of pages.

@plllmg https://gist.github.com/bsubedi26/eb41c3c359129dd965780592128f4eb9

Make sure you specify which collections to dynamically add category/sub category to their front matter headers in _data/categorize_collections.yml or it defaults to pages collection.

@jabowa Thanks so much!