npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

remark-mdx-frontmatter-nextjs

v1.1.2

Published

remark-mdx plugin to pass frontmatter data to nextjs page props

Downloads

15

Readme

remark-mdx-frontmatter-nextjs

What is it?

Yet another plugin to parse frontmatter data and expose it to the crowd. I know there's a much more popular remark-mdx-frontmatter out there, but i couldn't make it do what I wanted. I'm preparing a PR on the side and hopefully this package will be soon archived.

How to use it?

  1. npm i -S remark-mdx-frontmatter-nextjs

  2. In your next.config.mjs, add:

import withMdx from "@next/mdx";
import remarkFrontmatter from "remark-frontmatter";
import remarkMdxFrontmatter from "remark-mdx-frontmatter-nextjs";

const confMdx = withMdx({
  extension: /\.mdx$/,
  options: {
    remarkPlugins: [
      remarkFrontmatter,
      remarkMdxFrontmatter,
    ],
    rehypePlugins: [],
  },
});

export default confMdx({
  pageExtensions: ["mdx"],
});
  1. By default, the frontmatter metadata will be passed in pageProps, so that you can catch them in _app and use next/head directly there. Here's my own, in case you want some boilerplate
import '../styles/globals.css'

// https://stackoverflow.com/a/67464299/335243
import type { AppProps as NextAppProps } from "next/app";

import Head from 'next/head'

type AppProps<P = any> = {
  pageProps: P;
} & Omit<NextAppProps<P>, "pageProps">;

// from MDX
type PageMeta = {
  title?: string;
  description?: string;
  timestamp?: number;
  tags?: string[];
  layout?: boolean;
};

const AUTHOR = 'Julien Barbay'
const HANDLE = '@y_nk'

export default function App({ Component, router, pageProps }: AppProps<PageMeta>) {
  const { pathname } = router;
  const { title, description, tags, timestamp } = pageProps;

  return (
    <>
      <Head>
        <title>{title}</title>
        <meta name="author" content={AUTHOR} />
        <meta name="description" content={description} />
        {tags && <meta name="keywords" content={tags.join(",")} />}

        <meta name="twitter:creator" content={HANDLE} />
        <meta name="twitter:card" content="summary" />

        <meta property="article:author" content={AUTHOR} />
        <meta property="article:published_time" content={`${timestamp}`} />

        <meta property="og:title" content={title} />
        <meta property="og:type" content="article" />
        <meta property="og:url" content={pathname} />
        <meta property="og:description" content={description} />
      </Head>

      <Component {...pageProps} />
    </>
  );
}

Customisation

The plugin allows ONE option to help inject your frontmatter object into your NextJS application. The default renderer is:

const defaultRenderer = (data, node) => {
  return `
    export const getStaticProps = async () => {
      return { props: ${JSON.stringify(data)} }
    }
  `;
};

As you can guess, that's why the frontmatter data are coming back as pageProps in your _app now.

If you already use getStaticProps in your mdx it is obvious that you'll have a problem of duplicate declaration, but there's a plan B: you can pass another renderer of your choice instead. A suitable example can be:

const customRenderer = (data, node) => {
  return `MDXContent.frontmatterData = ${JSON.stringify(data)}`
};

And then in your app, you'll have to catch your data like this:

import '../styles/globals.css'

export default function App({ Component, pageProps }) {
  const { frontmatterData } = Component

  return (
    <div>
      <Component {...pageProps} />
    </div>
  );
}

More about the package

Why another package?

I just arrived in the NextJS game and I wanted to make a simple blog with mdx support and use frontmatter data to decorate my pages with next/head, yet I didn't want to write an overly verbose header everytime I write a blog post. Layouts are supposed to be for that reason, right?

The existing solutions

  1. There's next-mdx-remote but it doesn't allow imports within your mdx file, which was a no-go for me.

  2. There's next-mdx-enhanced but the repo itself says you shouldn't use it because there's issue at scale (and it's way too opinionated anyway)

  3. There's also mdx-bundler but after reading quick hands on, i got scared - the dependency to esbuild seems a bit overkill to what i wanted to achieve

  4. The only other way to load mdx is to use @next/mdx but there's no built-in support for frontmatter, you gotta use remark plugins for that. Just using the remark-frontmatter will simply strip out the metadata from the page, but the values will be lost. Finally, there's remark-mdx-frontmatter but it does not allow to customise the exports smoothly enough to exploit them in NextJS.

I came to the conclusion that remark-mdx-frontmatter was too narrow to be useful, yet it was a perfect base for me to tinker (thank you).