Mike Slinn

Identify Problematic Front Matter

Published 2024-07-23.
Time to read: 1 minutes.

This page is part of the jekyll collection.

Jekyll websites use YAML to identify pages that should be interpreted with the Liquid templating language. The YAML is placed at the top of the page, and is referred to as front matter.

Sometimes while I am writing a Jekyll page a bizarre error message suddenly appears. If the message mentions the Psych module, then the cause is malformed YAML.

Here is one such error message:

/home/mslinn/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/psych-5.1.2/lib/psych/parser.rb:62:in `_native_parse': (<unknown>): did not find expected key while parsing a block mapping at line 2 column 1 (Psych::SyntaxError)

The problem might be one or more missing colons. For example, instead of this:

Front Matter YAML missing colons
---
title One Fine Day
---

A colon is required to set off the key from the value:

Front Matter YAML with colons
---
title: One Fine Day
---

Here is another error message:

Psych::SyntaxError on line 1 of /home/mslinn/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/psych-5.1.2/lib/psych/parser.rb by outline: (): mapping values are not allowed in this context at line 2 column 7

The problem is likely a quoting problem in the front matter of a page. For example:

Front Matter YAML needing quotes
---
title: It's a Fine Day
---

Quotes are required encapsulate the value:

Front Matter YAML with quotes
---
title: "It's a Fine Day"
---

The problem might be leading whitespace before a key. For example:

Front Matter YAML with whitespace before a key
---
    title: Space before a key is not allowed
---

Identifying Problematic Front Matter

If you are unsure which page might be problematic, following is a short Ruby program that reads every HTML and markdown file, extracts the front matter, and checks the YAML in the front matter for validity.

If the program runs to completion, all your source files have well-formed front matter.

identify_problem_front_matter
#!/usr/bin/env ruby

require 'yaml'

class String
  def front_matter
    self[/(---\n.*?)---\n/m, 1]
  end
end

entries = Dir['collections/**/*.(html,md)']
  .reject { |fn| File.directory?(fn) }

entries.each do |entry|
  puts "Reading #{entry}"
  yaml = File.read entry

  puts "Reading #{entry} front matter"
  front_matter = yaml.front_matter
  puts "Front matter for #{entry} is:\n#{front_matter}"

  puts "Converting front matter in #{entry} to YAML"
  YAML.unsafe_load front_matter
end

identify_problem_front_matter needs to be made executable:

Shell
$ chmod a+x identify_problem_front_matter

The program can be verbose, so you might want to view the output by piping it through a pager, such as less:

Shell
$ identify_problem_front_matter | less
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.