Skip to content

Commit

Permalink
swap out @next/mdx for next-mdx-remote
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahongsf committed Oct 22, 2024
1 parent 2f18db8 commit 3f580eb
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 1,220 deletions.
38 changes: 38 additions & 0 deletions app/(stories)/stories/[storySlug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { compileMDX } from 'next-mdx-remote/rsc';
import { promises as fs } from 'fs';
import path from 'path';
import { Container } from '@/components/Container';
import { Heading } from '@/components/Typography';

// Generate static parameters (slugs) for each story
export async function generateStaticParams() {
const filenames = await fs.readdir(path.join(process.cwd(), '/stories'));
return filenames.map((filename) => ({
storySlug: filename.replace('.mdx', ''),
}));
}

export default async function ProjectPage({ params }: { params: { storySlug: string } }) {
const content = await fs.readFile(path.join(process.cwd(), '/stories', `${params.storySlug}.mdx`), 'utf-8');

interface Frontmatter {
title: string;
}

const data = await compileMDX<Frontmatter>({
source: content,
options: {
parseFrontmatter: true,
},
components: {
// Add components here
Heading,
},
});

return (
<Container className="mt-10">
{data.content}
</Container>
);
}
54 changes: 54 additions & 0 deletions app/(stories)/stories/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { promises as fs } from 'fs';
import path from 'path';
import Link from 'next/link';
import { compileMDX } from 'next-mdx-remote/rsc';
import { Container } from '@/components/Container';
import { useId } from 'react';

export const metadata = {
title: 'Stories',
description: 'Lorem ipsum pellentesque ut neque. In consectetuer turpis ut velit. Aenean massa.',
};

export default async function generateStaticParams() {
const filenames = await fs.readdir(path.join(process.cwd(), '/stories'));
const id = useId;

interface Frontmatter {
title: string;
}

const stories = await Promise.all(filenames.map(async (filename) => {
const content = await fs.readFile(path.join(process.cwd(), '/stories', filename), 'utf-8');
const { frontmatter } = await compileMDX<Frontmatter>({
source: content,
options: {
parseFrontmatter: true,
},
});
return {
filename,
slug: filename.replace('.mdx', ''),
...frontmatter,
};
}));

console.log('stories', stories);

return (
<Container>
<h1 className="text-4xl sm:text-5xl md:text-6xl font-bold mb-6 sm:mb-10 md:mb-16">
Impact Stories
</h1>
<ul>
{stories.map(({ filename, slug }) => {
return (
<li key={id}>
<Link href={`/stories/${slug}`}>{ filename }</Link>
</li>
);
})}
</ul>
</Container>
);
}
11 changes: 0 additions & 11 deletions app/MDXProvider.tsx

This file was deleted.

5 changes: 1 addition & 4 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import './globals.css';
import localFont from 'next/font/local';
import { Source_Sans_3, DM_Sans } from 'next/font/google';
import { cnb } from 'cnbuilder';
import CustomMDXProvider from './MDXProvider';

const dm_sans = DM_Sans({
subsets: ['latin'],
Expand Down Expand Up @@ -38,9 +37,7 @@ export default function RootLayout({
stanford.variable,
)}
>
<CustomMDXProvider>
{children}
</CustomMDXProvider>
{children}
</body>
</html>
);
Expand Down
19 changes: 0 additions & 19 deletions app/pages/index.mdx

This file was deleted.

7 changes: 0 additions & 7 deletions mdx-components.ts

This file was deleted.

13 changes: 2 additions & 11 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import remarkGfm from 'remark-gfm';
import createMDX from '@next/mdx';

/** @type {import('next').NextConfig} */

const nextConfig = {
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
basePath: '/soe-centennial-nextjs',
output: 'export',
reactStrictMode: true,
};

const withMDX = createMDX({
options: {
remarkPlugins: [remarkGfm],
rehypePlugins: [],
},
});

export default withMDX(nextConfig);
export default nextConfig;

Loading

0 comments on commit 3f580eb

Please sign in to comment.