jonschlinkert/gray-matter

Disable date parsing?

KyleAMathews opened this issue · 14 comments

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 commented

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.

@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.

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

doowb commented

@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.

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 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,
            }),
          },
        },
    },

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:

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.

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.

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.