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

Disable date parsing? #62

Closed
KyleAMathews opened this issue Feb 7, 2018 · 14 comments
Closed

Disable date parsing? #62

KyleAMathews opened this issue Feb 7, 2018 · 14 comments

Comments

@KyleAMathews
Copy link

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?

@doowb
Copy link
Collaborator

doowb commented Feb 7, 2018

gray-matter doesn'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 to js-yaml.

From this issue they point to these options which show that you can use a different schema. By default js-yaml uses 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-yaml doesn't take a string to specify another schema so it looks like you would need to use one exported on the js-yaml object itself, or create a new one using their Schema constructor.

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.

@KyleAMathews
Copy link
Author

@doowb thanks for the detailed response!

@jonschlinkert
Copy link
Owner

Looks like this can be closed?

thanks @KyleAMathews @doowb!

@KyleAMathews
Copy link
Author

Yup! Thanks @doowb!

@EryouHao
Copy link

@KyleAMathews How did you solve it in the end? thanks.

@jonschlinkert
Copy link
Owner

@EryouHao #62 (comment)

@patarapolw
Copy link

@doowb gray-matter documentation isn't clear. How do I restrict js-yaml to JSON_SCHEMA?

@doowb
Copy link
Collaborator

doowb commented Jan 16, 2020

@patarapolw from my comment above and the JSON_SCHEMA link you mentioned, you can pass a schema on the options (gray-matter passes options through to js-yaml). I don't know what the schema property expects, but if you can experiment and figure it out, I'm interested in seeing an example.

gray-matter documentation isn't clear.

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 yaml engine to parse yaml in any way you would like since gray-matter just calls js-yaml using the default options.

@jonschlinkert
Copy link
Owner

The "engine" can be a no-op.

@patarapolw
Copy link

patarapolw commented Jan 23, 2020

@doowb Although, I did find out that this is possible, it isn't clear that I can specify yaml in engines, because your example is toml.

Anyways, I'll post the solution for someone less smart...

import matter from 'gray-matter'
import yaml from 'js-yaml'

matter(STRING, {
  engines: {
    yaml: s => yaml.safeLoad(s, { schema: yaml.JSON_SCHEMA })
  }
})

engines can also be specified in gatsby-transformer-remark because it inherits gray-matter.

    {
      resolve: 'gatsby-transformer-remark',
      options: {
        engines: {
          yaml: {
            parse: (s) => yaml.safeLoad(s, {
              schema: yaml.JSON_SCHEMA,
            }),
          },
        },
    },

@ghost
Copy link

ghost commented Apr 25, 2022

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

@dralshehri
Copy link

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:

npm install yaml

Then use it as a custom engine:

import matter from 'gray-matter'
import { parse, stringify } from 'yaml'

const { data, content } = matter(source, {
    engines: {
      yaml: {
        parse,
        stringify,
      },
    },
  })

This fix might not be needed if #147 got merged.

@patarapolw
Copy link

patarapolw commented Aug 6, 2022

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)

npm install sakulstra/gray-matter#feat/yaml

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-matter itself, but the parsing function is probably as simple as

function matter(s) {
  const [,fm0] = s.split(/^---\r?\n/)
  if (!fm0) {
    return { content: s, data: {} }
  }

  const [fm, content = ''] = fm0.split(/\r?\n---(\r?\n)?/)
  return { content, data: yaml.parse(fm) }
}

About the tag, parsing to date can be forced as well, with !!timestamp 2022-08-07 syntax.

khuedoan added a commit to khuedoan/blog that referenced this issue Mar 26, 2023
@ardunster
Copy link

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 matter was 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:

date: '2023-09-14'

instead of:

date: 2023-09-14

This may not be the right solution for every situation but it works fine for me, hopefully this is helpful to someone else.

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

No branches or pull requests

7 participants