Disable date parsing? #62

Closed
opened 2018-02-07 06:14:55 +00:00 by KyleAMathews · 14 comments
KyleAMathews commented 2018-02-07 06:14:55 +00:00 (Migrated from github.com)

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?

We're using gray-matter very successfully in Gatsby (thanks!). [@tremby ran into an issue recently](https://github.com/gatsbyjs/gatsby/issues/3885) 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 2018-02-07 14:49:03 +00:00 (Migrated from github.com)

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.

`gray-matter` doesn't do the actual yaml parsing itself. It passes that off to [js-yaml](https://github.com/nodeca/js-yaml) by default. You can specify another [engine](https://github.com/jonschlinkert/gray-matter#optionsengines) if you need to, or pass options through to `js-yaml`. From [this issue](https://github.com/nodeca/js-yaml/issues/161) they point to [these options](https://github.com/nodeca/js-yaml#safeload-string---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](https://github.com/nodeca/js-yaml/issues/161#issuecomment-72714136). 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](https://github.com/nodeca/js-yaml/issues/198#issuecomment-120205625), it looks like the date parsing is in the [YAML spec](http://www.yaml.org/spec/1.2/spec.html#id2761292). 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 commented 2018-02-07 16:49:01 +00:00 (Migrated from github.com)

@doowb thanks for the detailed response!

@doowb thanks for the detailed response!
jonschlinkert commented 2018-02-08 20:37:15 +00:00 (Migrated from github.com)

Looks like this can be closed?

thanks @KyleAMathews @doowb!

Looks like this can be closed? thanks @KyleAMathews @doowb!
KyleAMathews commented 2018-02-08 20:48:56 +00:00 (Migrated from github.com)

Yup! Thanks @doowb!

Yup! Thanks @doowb!
EryouHao commented 2019-01-24 07:53:11 +00:00 (Migrated from github.com)

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

@KyleAMathews How did you solve it in the end? thanks.
jonschlinkert commented 2019-01-24 12:55:53 +00:00 (Migrated from github.com)
@EryouHao https://github.com/jonschlinkert/gray-matter/issues/62#issuecomment-363791733
patarapolw commented 2020-01-16 10:14:53 +00:00 (Migrated from github.com)

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

@doowb [`gray-matter` documentation](https://github.com/jonschlinkert/gray-matter#optionsengines) isn't clear. How do I restrict `js-yaml` to [JSON_SCHEMA](https://github.com/nodeca/js-yaml#safeload-string---options-)?
doowb commented 2020-01-16 16:07:11 +00:00 (Migrated from github.com)

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

@patarapolw from [my comment above](https://github.com/jonschlinkert/gray-matter/issues/62#issuecomment-363791733) 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](https://github.com/jonschlinkert/gray-matter#optionsengines) 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 commented 2020-01-16 22:55:20 +00:00 (Migrated from github.com)

The "engine" can be a no-op.

The "engine" can be a no-op.
patarapolw commented 2020-01-23 10:48:59 +00:00 (Migrated from github.com)

@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,
            }),
          },
        },
    },
@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... ```js 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`. ```js { resolve: 'gatsby-transformer-remark', options: { engines: { yaml: { parse: (s) => yaml.safeLoad(s, { schema: yaml.JSON_SCHEMA, }), }, }, }, ```
ghost commented 2022-04-25 00:36:18 +00:00 (Migrated from github.com)

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

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 commented 2022-08-06 15:05:15 +00:00 (Migrated from github.com)

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.

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: ```shell npm install yaml ``` Then use it as a custom engine: ```js 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 commented 2022-08-06 22:19:52 +00:00 (Migrated from github.com)

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.

> 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](https://www.npmjs.com/package/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 ```ts 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.
ardunster commented 2023-09-14 22:06:21 +00:00 (Migrated from github.com)

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.

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 in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rtlabs-llm-agents/gms-gray-matter#62
No description provided.