Giter Site home page Giter Site logo

Comments (6)

joe-bell avatar joe-bell commented on May 3, 2024 3

Apologies if this is the wrong issue to raise this, but it felt somewhat related – I'd love to be able to access these attributes (or frontmatter) within the layout wrapping my MDX routes

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 content was available too, I could even create placeholders for all MDX images on the server-side)

from remix.

ryanflorence avatar ryanflorence commented on May 3, 2024 2

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?

from remix.

lachlanjc avatar lachlanjc commented on May 3, 2024 1

Big fan of calling this frontmatter/adding support for this!

from remix.

ryanflorence avatar ryanflorence commented on May 3, 2024

been thinking about this too, have you done this elsewhere? what does SlimLayout look like?

from remix.

kentcdodds avatar kentcdodds commented on May 3, 2024

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}

from remix.

kentcdodds avatar kentcdodds commented on May 3, 2024

I like that!

Could we call it frontmatter instead of attributes? That's consistent with the common term for the "YAML at the top of markdown" I think.

from remix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.