Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date parsing fails #3

Closed
mehulkar opened this issue May 26, 2017 · 3 comments
Closed

Date parsing fails #3

mehulkar opened this issue May 26, 2017 · 3 comments
Assignees
Labels

Comments

@mehulkar
Copy link

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?

@icco
Copy link

icco commented May 28, 2017

👋 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)

@waiting-for-dev
Copy link
Owner

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)

@mehulkar
Copy link
Author

mehulkar commented Jun 1, 2017

Thanks @waiting-for-dev! and hi @icco! :) let me know when you're on the west coast again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants