Disable date parsing? #62
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
We're using gray-matter very successfully in Gatsby (thanks!). @tremby ran into an issue recently where he needs to access the original form of a date field in the frontmatter but since it's converted into a date object, he doesn't have access to it anymore. Since Gatsby handles date conversion automatically, would it be possible to turn off date parsing in gray-matter?
gray-matterdoesn't do the actual yaml parsing itself. It passes that off to js-yaml by default. You can specify another engine if you need to, or pass options through tojs-yaml.From this issue they point to these options which show that you can use a different schema. By default
js-yamluses a default schema that when it finds an unquoted string that looks like a date, it will parse it as a date. Using the JSON or CORE schemas seems to work.Unfortunately,
js-yamldoesn't take a string to specify another schema so it looks like you would need to use one exported on thejs-yamlobject itself, or create a new one using theirSchemaconstructor.Also, according to this issue, it looks like the date parsing is in the YAML spec.
I'm going to leave this open and see if I can come up with a working example of specifying another schema. In the meantime, I recommend quoting the strings that might look like dates or timestamps.
@doowb thanks for the detailed response!
Looks like this can be closed?
thanks @KyleAMathews @doowb!
Yup! Thanks @doowb!
@KyleAMathews How did you solve it in the end? thanks.
@EryouHao https://github.com/jonschlinkert/gray-matter/issues/62#issuecomment-363791733
@doowb
gray-matterdocumentation isn't clear. How do I restrictjs-yamlto JSON_SCHEMA?@patarapolw from my comment above and the
JSON_SCHEMAlink you mentioned, you can pass aschemaon the options (gray-matterpasses options through tojs-yaml). I don't know what theschemaproperty expects, but if you can experiment and figure it out, I'm interested in seeing an example.What isn't clear? That documentation shows how you can specify engines for parsing and stringifying front-matter. It was mentioned in the comment above as a way to specify your own
yamlengine to parse yaml in any way you would like sincegray-matterjust callsjs-yamlusing the default options.The "engine" can be a no-op.
@doowb Although, I did find out that this is possible, it isn't clear that I can specify
yamlin engines, because your example is toml.Anyways, I'll post the solution for someone less smart...
enginescan also be specified ingatsby-transformer-remarkbecause it inheritsgray-matter.For those who came here, iit also can be disabled by '!!str 2021-02-22' syntax. By this you don't need to install js-yaml.
example
https://github.com/ulwlu/ulwlu.github.io/blob/master/posts/got-myname-on-mysql-releasenotes.md
If anyone is still having this issue, I was able to fix it using the https://github.com/eemeli/yaml package.
First, install the package:
Then use it as a custom engine:
This fix might not be needed if #147 got merged.
Looks interesting, and it can already be easily installed by NPM-github feature. (though Git has to be installed separately)
I am not exactly sure about the package size or the solidness of the package, but I might consider yaml package as well, as it has TypeScript types built-in.
Also, I am not sure about the value of
gray-matteritself, but the parsing function is probably as simple asAbout the tag, parsing to date can be forced as well, with
!!timestamp 2022-08-07syntax.Found this issue after significant frustration trying to format a YYYY-MM-DD input through dayjs in a Next.js app and finally discovered that
matterwas deciding for itself what time I really meant instead of just retrieving the string. However, I was also able to easily fix it by just enclosing the YYYY-MM-DD inside the markdown file in quotes, ie:instead of:
This may not be the right solution for every situation but it works fine for me, hopefully this is helpful to someone else.