Skip to content

feat: Attach export to MDXContent? #14

@domdomegg

Description

@domdomegg

Not sure whether this is in scope for this plugin - feel free to close if so.

I'm hoping to use this plugin with Next.js so that I can insert a header component using the frontmatter content. I think I currently can't access it from _app.tsx, where this is currently rendered. I could access it by adding something to every .mdx file, but this adds overhead to writing pages that I want to avoid.

To fix this, I was hoping this plugin could add it to the component itself, e.g. so rather than:

export const frontmatter = {
  hello: 'frontmatter'
}

export default function MDXContent() {
  return <p>Rest of document</p>
}

It outputs:

export default function MDXContent() {
  return <p>Rest of document</p>
}

MDXContent.frontmatter = {
  hello: 'frontmatter'
}

This is somewhat similar to what's described in https://www.josephrex.me/frontmatter-with-nextjs-and-mdx/ - although the actual component would be in _app.tsx.


This is roughly what I had in mind for my _app.tsx:

import '../styles/globals.css';
import type { AppProps } from 'next/app';

const App: React.FC<AppProps> = ({ Component, pageProps }: AppProps) => {
  if (Component.displayName === 'MDXContent') {
    return (
      <>
        <Header frontmatter={Component.frontmattter} />
        <div className="max-w-sm md:max-w-2xl mx-auto px-8 py-12 md:my-24 prose animate-fade-up">
          <Component {...pageProps} />
        </div>
      </>
    );
  }

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

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions