Unit testing to check if all markdown variables are set

I’m looking for a way to do unit testing on a jekyll site. I want to check if the users are filling in all the variables in the markdown. For example title, category etc. If they have not included all info needed in the markdown I want to block the possibility to merge the content into production. Anyone who has done something similar or if there are any built in functionality that I can use?

2 Likes

Jekyll already supports Liquid options to prevent invalid Liquid code that might otherwise be quietly deployed

liquid:
  error_mode: strict
  strict_variables: true
  strict_filters: true
2 Likes

Thanks for sharing @MichaelCurrin I didn’t know about those options, and I actually found some issues on my sites.

@giraffe You could also pass the flag --strict_front_matter when building your site:

bundle exec jekyll build --strict_front_matter --trace

From the docs:

--strict_front_matter Fail if errors are present in front matter

More info:

bundle exec jekyll help build
1 Like

Thanks. I’ve added to my notes and set strict_front_matter on one of my sites now.

@MichaelCurrin I just tried strict_variables: true for the first time and discovered it does not know about the default filter. For example, it erroneously flags this as an error:

{% assign headerImage = layout.headerImage | default: page.headerImage | default: "/assets/images/arrowHead.webp" %}

Also, strict_filters: true erroneously reports the Jekyll slugize filter as missing when it is used properly.

Yeah it is too strict.

If you use default then you can do without strict variable for those sections.
Or you need to actually set the value even empty.

E.g. use defaults in config to set

headerImage: nil

Or maybe ''. Then it is set as a variable but then default should see it as empty and replace it.

Do you mean slugify not slugize ? That sounds like strict filter is doing its job to alert you.

See bottom of string filters list

Yes, slugize is a valid Jekyll filter that is not well known. Seems there are various categories of filters, and the Jekyll filters don’t get as much publicity/documentation. See How do I shuffle the order of an array in Jekyll?

Can you share where you found slugize and what it does?

It appears zero times on a code search in Jekyll repo but slugify is well covered

I also tried slugize locally on Jekyll 3.9 and it did nothing.

It does appear on some stackoverflow questions so maybe it’s deprecated

The page I linked to says this:

Jekyll slugize

Liquid, Jekyll uses the Liquid templating language to process templates. Generally in Liquid you output content using two curly braces e.g. {{ variable }} and perform Liquid Filters. All of the standard Liquid filters are supported (see below)… To make common tasks easier, Jekyll even adds a few handy filters of its own, all of which you can find on this page.

Liquid Filters, To make common tasks easier, Jekyll even adds a few handy filters of its own, all of which you can find on this page. You can also create your own filters using Liquid. Jekyll uses the Liquid templating language to process templates… Generally in Liquid you output content using two curly braces e.g. {{ variable }} and perform logic statements by surrounding them in a curly brace percentage sign e.g. {% if statement %}.

Jekyll, show category for a single post, already loop over the array: {% for category in page.categories %} {{ category }} {% endfor %}. Jekyll 4.0 started allowing themes to bundle a _config.yml to simplify theme-onboarding for new users. In the unfortunate situation that importing a bundled theme configuration messes up the merged site-configuration, the user can configure Jekyll to not import the theme-config entirely. ignore_theme_config: BOOL. Exclude"

Please check your comment after posting.

{{ category }}

The snippet you posted hides the crucial slugize bit as the forum formatted it.

Please use code ticks

```
Copied HTML code
```

This is what you meant I think

Jekyll, show category for a single post, already loop over the array: 
{% for category in page.categories %}
 <a href="/​blog/{{ category | slugize }}/">
 {{ category }} </a>

Again, I think that article may be outdated or a mistake for reasons in my previous comment. Slugize does nothing for me in standard Jekyll so rejecting it with strict rule sounds fair.

Please just refer the article I linked to. Maybe someone else will have additional information. That is all I have at present.

The article only uses slugize as in the snippet I copied from the article. It doesn’t explain it further besides using Slugize in a heading. Maybe they have it as a plugin they never mentioned.

So I would not trust the source. Trust the Liquid docs and Jekyll docs.

Can you use slugize locally or on a site to display that it does indeed work for you.

e.g. with a screenshot of output or link to a GH pages site where you use it

Otherwise just use slugify because that does work by default and is covered in the docs.

This page shows Jekyll-specific tags, including Slugify: