Date parsing fails
mehulkar opened this issue ยท 3 comments
I have frontmatter like this:
---
title: 'cool title'
date: 2012-01-11
categories: foo
---
This fails with:
FrontMatterParser::Parser.parse_file(str)
#=> Psych::DisallowedClass: Tried to load unspecified class: Date
I'm pretty sure the root cause is ruby/psych#262 and that the parser is attempting to run Date.new('2012-01-11')
, but I haven't nailed it down yet.
I might be able to write a customer parser to overwrite how the frontmatter is being parsed, I guess?
Was wondering if there was a different solution or if anyone had any insight?
๐ You could append your date with a time as a quick fix.
require "front_matter_parser"
prsr = FrontMatterParser::Parser.new(:md)
body = "---\ntitle: 'cool title'\ncategories: foo\ndatetime: 2017-03-16 18:05:42 +0000 UTC\n---\n"
prsr.call(body)
Hey @mehulkar , thanks for reporting.
Indeed, it seems a psych issue.
This library uses YAML
ruby standard library, which uses psych under the hood, to load the front matter. I don't think we should move away from the standard solution.
Also, this library calls YAML.safe_load
instead of YAML.load
. The bug only affects to the former, but I don't want to change and use load
because it is dangerous for user provided content.
As long as the bug is not solved, you could do as @icco says or implement your own loader. For example, if you are parsing from a well known source and you don't need to protect against user content, you could safely use load
instead of safe_load
:
unsafe_loader = ->(string) { YAML.load(string) }
# And then...
# For a file
FrontMatterParser::Parser.parse_file('example.md', loader: unsafe_loader)
# For a string
FrontMatterParser::Parser.new(:md, loader: unsafe_loader).call(string)
Thanks @waiting-for-dev! and hi @icco! :) let me know when you're on the west coast again!