question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gray-matter options?

See original GitHub issue
  • mdx-bundler version: 5.1.2
  • node version: 14.17.3
  • npm version: 6.14.13

Relevant code or config

---
title: Test Post
published: 2021-02-13
description: Shows how a post is published via MDX
---

# Test Post

This is a test post

What you did: I’m using mdx-bundler to build a nextjs static site. My frontmatter contains some date-looking things. I pass the frontmatter returned by bundleMDX as props for my component.

What happened: Since its a static site, Nextjs tries to serialize the props as part of the component and fails because it isn’t JSON serializable:

Error: Error serializing `.frontmatter.published` returned from `getStaticProps` in "/posts/[slug]".
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.

Problem description: There’s no way to configure the default gray-matter engine behavior.

Suggested solution: I guess the most obvious thing is to provide the ability to pass options to gray-matter.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
barrymaycommented, Nov 1, 2021

FYI - since I ran into the same issue and am a little new to gray-matter/MDX processing, after jumping thru the google-sphere to get answers I felt dropping an example usage of graymatterOptions that solves the original question would be helpful to others:

This returns published as a string, not a date, which is typical for most JSON-parsed results anyway.

import yaml from "js-yaml";

// ---- 
  await bundleMDX(source, {
    grayMatterOptions: (options) => {
      // set other gray-matter options, like excerpt
      options.engines = {
        yaml: (s) => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object,
      };
      return options;
    },
  } 
// ---- 

If you’re using matter elsewhere, use the same thing (hence the whole point of grayMatterOptions):

import yaml from "js-yaml";

// ---- 
      const { data } = matter(source, {
        engines: {
          yaml: (s) => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object,
        },
      });
// --- 

Thanks to @Arcath and @jpaas for the solution and original question 😃

0reactions
josephbalescommented, Feb 6, 2022

@barrymay You are a legend. Thanks for this tidbit! Got me going again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parse front-matter from a string or file. Fast, reliable ... - GitHub
Instead of defining the language on the options, gray-matter will automatically detect the language defined after the first delimiter and select the correct ......
Read more >
gray-matter - npm
Parses YAML front matter by default, but also has support for YAML, JSON, TOML or Coffee Front-Matter, with options to set custom delimiters ......
Read more >
Strategic Partners | GrayMatter
100 Global View Drive Warrendale, PA 15086. Call: 877-741-2410. eFax: 412-202-5053. © 2022 GrayMatter | All Rights Reserved. Privacy policy.
Read more >
WHO WE ARE | GrayMatter
We focus on co-innovation and curating the best process and technology to drive industrial companies and utilities through digital transformation. Who does ...
Read more >
GrayMatter: Home
GrayMatter is dedicated to transforming operations and empowering people. We help your people and your industrial assets become smarter and more visible.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found