Skip to content

MDX Integration #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ node_modules
dist
.next
.contentlayer
yarn.lock
.DS_Store
.next

.vercel
12 changes: 12 additions & 0 deletions components/Form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function Form() {
return (
<>
<form className="border-2 border-slate-500 p-2 flex flex-col space-y-5 rounded">
<label>Enter your name:</label>
<input type="text" placeholder="John Doe" />
<label>Enter your age:</label>
<input type="text" placeholder="24" />
</form>
</>
);
}
3 changes: 2 additions & 1 deletion contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { defineDocumentType, makeSource } from 'contentlayer/source-files'

const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `**/*.md`,
filePathPattern: `**/*.mdx`,
contentType: 'mdx',
fields: {
title: {
type: 'string',
Expand Down
4 changes: 3 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { withContentlayer } = require("next-contentlayer");

module.exports = withContentlayer({});
module.exports = withContentlayer({
swcMinify: true,
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"contentlayer": "latest",
"next": "12.1.5",
"next-contentlayer": "latest",
"react": "18.0.0",
"react-dom": "18.0.0"
"react": "^18.1.0",
"react-dom": "^18.1.0"
},
"devDependencies": {
"@types/react": "18.0.4",
"@types/react": "^18.0.8",
"autoprefixer": "^10.4.4",
"postcss": "^8.4.12",
"tailwindcss": "^3.0.24",
Expand Down
18 changes: 14 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import Link from "next/link";
import { compareDesc, format, parseISO } from "date-fns";
import { allPosts, Post } from "contentlayer/generated";
import { useMDXComponent } from 'next-contentlayer/hooks'
import Form from "../components/Form"

// Const that passes custom components to all MDX files in the posts directory
const MDXcomponents = {
Form,
};

export async function getStaticProps() {
const posts: Post[] = allPosts.sort((a, b) => {
Expand All @@ -10,6 +17,9 @@ export async function getStaticProps() {
}

function PostCard(post: Post) {
// Rendering MDX
const MDXComponent = useMDXComponent(post.body.code)
//
return (
<div className="mb-8">
<h2 className="text-xl">
Expand All @@ -20,10 +30,10 @@ function PostCard(post: Post) {
<time dateTime={post.date} className="block text-xs text-gray-600 mb-2">
{format(parseISO(post.date), "LLLL d, yyyy")}
</time>
<div
className="text-sm"
dangerouslySetInnerHTML={{ __html: post.body.html }}
/>
{/* Switch from MD to MDX */}
{/* <div dangerouslySetInnerHTML={{ __html: post.body.html }} /> */}
<MDXComponent components={MDXcomponents}/>
{/* */}
</div>
);
}
Expand Down
17 changes: 16 additions & 1 deletion pages/posts/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import Head from "next/head";
import { format, parseISO } from "date-fns";
import { allPosts, Post } from "contentlayer/generated";
import { useMDXComponent } from 'next-contentlayer/hooks'
import Form from "../../components/Form"

// Const that passes custom components to all MDX files in the posts directory
const MDXcomponents = {
Form,
};

export async function getStaticPaths() {
const paths: string[] = allPosts.map((post) => post.url);
Expand All @@ -22,6 +29,9 @@ export async function getStaticProps({ params }) {
}

const PostLayout = ({ post }: { post: Post }) => {
// Rendering MDX
const MDXComponent = useMDXComponent(post.body.code)
//
return (
<>
<Head>
Expand All @@ -34,7 +44,12 @@ const PostLayout = ({ post }: { post: Post }) => {
</time>
<h1>{post.title}</h1>
</div>
<div dangerouslySetInnerHTML={{ __html: post.body.html }} />

{/* Switch from MD to MDX */}
{/* <div dangerouslySetInnerHTML={{ __html: post.body.html }} /> */}
<MDXComponent components={MDXcomponents}/>
{/* */}

</article>
</>
);
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions posts/react-components.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Using Custom Components with MDX
date: 2022-04-29
---

By adding Components to the `pages/posts/[slug].tsx` file, you can use them across all your MDX files. As an example, we have added a simple Form component to this file.

<Form />
File renamed without changes.
Loading