Could the MDX default export get the frontmatter as props? #2851
Replies: 7 comments 1 reply
-
been thinking about this too, have you done this elsewhere? what does SlimLayout look like? |
Beta Was this translation helpful? Give feedback.
-
It's not much right now. But I'd like to have access to frontmatter. import * as React from 'react'
import {Link} from 'react-router-dom'
function SlimLayout({
children,
...props
}: React.PropsWithChildren<{}>): React.ReactNode {
console.log(props) // <-- I want the frontmatter in there
return (
<div>
<nav>
<ul>
<li>
<Link to="/">kentcdodds.com</Link>
</li>
</ul>
</nav>
<main>{children}</main>
<footer>Join the newsletter!</footer>
</div>
)
}
export {SlimLayout} |
Beta Was this translation helpful? Give feedback.
-
I think I could get behind this. This code is no different, but feels less magic when you explicitly write it out like this: ---
meta:
title: Remix Rocks
description: A solid description of this document.
---
This is the content for the blog post
export default ({ attributes, body }) => (
<div>
<h1>{attributes.meta.title}</h1>
<article>{body}</article>
</div>
) So by default, the default export is the body of the markdown as a component. If you export default, then that's the component we'll use, and Remix passes the attributes and body to you. To complete the picture but in reverse, mdx components export the attributes: import MdxPage, { attributes } from "../mdx/some-page";
export default function Page() {
return (
<div>
<h1>{attributes.title}</h1>
<MdxPage />
</div>
);
} WDYT? |
Beta Was this translation helpful? Give feedback.
-
I like that! Could we call it |
Beta Was this translation helpful? Give feedback.
-
Big fan of calling this |
Beta Was this translation helpful? Give feedback.
-
Apologies if this is the wrong issue to raise this, but it felt somewhat related – I'd love to be able to access these Something like… ---
title: "Do Not Disturb"
date: "2019-01-03"
description: "Modern mindfulness tips for connected life"
---
Some markdown content nested within `app/routes/blog/` // app/routes/blog.tsx
import { Outlet, useLoaderData } from "remix";
import type { LoaderFunction } from "remix";
export const loader: LoaderFunction = async ({ attributes }) => {
return { attributes };
};
export default function BlogPost() {
const { attributes } = useLoaderData();
return (
<article>
<header>
<h1>{attributes.title}</h1>
<p>{attributes.description}</p>
</header>
<Outlet />
</article>
);
} This feels a bit more akin to Remix layouts, and would also open up the door to more server-side logic in MDX posts (e.g. creating an image placeholder on the server-side for a hero image. Heck! If |
Beta Was this translation helpful? Give feedback.
-
I've been stumbling into this too. The lack of support for frontmatter with the same functionality as we can expect from Jekyll etc, feels like quite a limit to adoption as a blog engine, but I can appreciate you have higher goals than that. Still, having frontmatter accessible to useMatches() for example would be really useful. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like it if the
SlimLayout
component could access themeta
(and everything else in the frontmatter) so it can display thetitle
,description
, and theheaders['og:image']
.Beta Was this translation helpful? Give feedback.
All reactions