GithubHelp home page GithubHelp logo

Comments (5)

shadcn avatar shadcn commented on May 15, 2024

Can you give an example of a post and snippet frontMatter so we can determine the relationship?

Thank you

from next-mdx.

tyteen4a03 avatar tyteen4a03 commented on May 15, 2024

post.md:

---
title: Post
---

snippet.md:

---
title: Snippet
belongsToPost: post
---

from next-mdx.

shadcn avatar shadcn commented on May 15, 2024

OK. Let me see. I'll try to put together a quick example.

from next-mdx.

shadcn avatar shadcn commented on May 15, 2024

OK I created an example here:

Demo: https://next-mdx-post-snippet.vercel.app
Repo: https://github.com/arshad/next-mdx-post-snippet

Here are some key parts:

Define your MDX types

https://github.com/arshad/next-mdx-post-snippet/blob/25aed7eefcb2616fd7da311189eff2071fa6fa20/next-mdx.json#L1

{
  "post": {
    "contentPath": "content/posts",
    "basePath": "/blog"
  },
  "snippet": {
    "contentPath": "content/snippets"
  }
}

Set the post in the snippet

---
title: Hello World
post: <------- This must match the key we used above in next-mdx.json
  - first-post <-----------------
---

```jsx
export function HelloWorld() {
  return <p>Hello World</p>
}

Get the snippets for a post

export async function getStaticProps(context) {
  const post = await getMdxNode("post", context)

  if (!post) {
    return {
      notFound: true,
    }
  }

  const snippets = await getAllMdxNodes("snippet")

  return {
    props: {
      post,
      snippets: snippets.filter((snippet) =>
        snippet.relationships.post.some(({ slug }) => slug === post.slug)
      ),
    },
  }
}

https://github.com/arshad/next-mdx-post-snippet/blob/25aed7eefcb2616fd7da311189eff2071fa6fa20/pages/blog/%5B...slug%5D.tsx#L46

Hope this helps

from next-mdx.

tyteen4a03 avatar tyteen4a03 commented on May 15, 2024

OK so as I suspected I would need to do the filtering myself. Thanks either way :)

from next-mdx.

Related Issues (9)

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.