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|
        hour=event['hour'].to_s
        if
          re.match?(hour)
        then
          print event['hour']
        end
        event["hour"] = hour
      end
end

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

method_missing': undefined method `[]=' for #<Jekyll::Document _events/2017-03-09-will.md collection=events> (NoMethodError)
Did you mean?  []

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

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 doc.data[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 = doc.data['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)

  doc.data['hour'] = hour_string
end
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 document.data…

m