Issue with frontmatter processing

Hello everybody,

I got stuck with something that I feel must be trivial but can’t find a way to go around it.

Briefly, I have a collection of pages which hold in the YAML frontmatter a string for the hours of an event. The hours vary in format (sometimes it will be 17:00. sometimes it will be 17pm. etc).
So the value should be treated as a string.

BUT this frontmatter is generated via a light CMS (netlifyCMS), which does not add quotes around the hour.

The processing of the frontmatter, when in the format

event: eventname
hour: 17:00

results in this weird minute counting :
hour: 1020.0

How can I pre-process the event markdowns, to add quotes and make it

event: eventname
hour: '17:00'

I tried making a plugin but got stuck

re = /[0-9][0-9]:[0-9][0-9]$/

Jekyll::Hooks.register :site, :pre_render do |site|
      site.collections['events'].each do |event|
          print event['hour']
        event["hour"] = hour

this print the hour but i get this error when trying to modify the value:

method_missing': undefined method `[]=' for #<Jekyll::Document _events/ collection=events> (NoMethodError)
Did you mean?  []

Thanks in advance for the eventual help!! :slight_smile:

In your loop, each event is an instance of Jekyll::Document.
Such objects do not respond to the method []= but only []. The [] is just a shortcut to[key].

Therefore your loop could be refactored into:

# `site.collections[label]` returns a `Jekyll::Collection` instance.
# Call `#docs` on the collection instance to get an array of all documents in the collection.
site.collections['events'].docs.each do |doc|
  # Equivalent to `hour = doc['hour']
  hour =['hour']

  # skip current doc if hour hasn't been set.
  next unless hour

  hour_string = hour.to_s

  # using `puts` to log ensures that each statement is in its own line.
  puts hour if re.match?(hour)['hour'] = hour_string
1 Like

Thanks a lot!

I guess the fact that the shortcut event[‘hour’] was being read didn’t help to understand I had to use the…