Skip to content

Commit 75565f9

Browse files
authored
use contentlayer instead of next-mdx-remote (railwayapp#215)
* use contentlayer instead of next-mdx-remote * pesky package removal * yarn.lock again
1 parent a36e7e0 commit 75565f9

10 files changed

Lines changed: 1110 additions & 90 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ yarn-error.log*
3939
# dynamically generated files
4040
public/sitemap*.xml
4141
public/robots.txt
42+
.contentlayer

contentlayer.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineDocumentType, makeSource } from "contentlayer/source-files";
2+
import remarkAutoLinkHeadings from "remark-autolink-headings";
3+
import remarkGfm from "remark-gfm";
4+
import remarkSlug from "remark-slug";
5+
6+
const Page = defineDocumentType(() => ({
7+
name: "Page",
8+
filePathPattern: `**/*.md`,
9+
contentType: "mdx",
10+
fields: {
11+
title: {
12+
type: "string",
13+
description: "The title of the page",
14+
required: true,
15+
},
16+
},
17+
computedFields: {
18+
url: {
19+
type: "string",
20+
resolve: doc => `/${doc._raw.flattenedPath}`,
21+
},
22+
},
23+
}));
24+
25+
export default makeSource({
26+
contentDirPath: "src/docs",
27+
documentTypes: [Page],
28+
mdx: { remarkPlugins: [remarkSlug, remarkAutoLinkHeadings, remarkGfm] },
29+
});

next.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
const { withContentlayer } = require("next-contentlayer");
2+
13
/** @type {import('next').NextConfig} */
2-
const nextConfig = {
4+
const nextConfig = withContentlayer({
35
reactStrictMode: true,
46
images: {
57
domains: [
@@ -18,6 +20,6 @@ const nextConfig = {
1820
},
1921
];
2022
},
21-
};
23+
});
2224

2325
module.exports = nextConfig;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
"@prisma/client": "4.10.1",
2121
"@radix-ui/react-scroll-area": "^1.0.2",
2222
"@tailwindcss/typography": "^0.5.9",
23+
"contentlayer": "^0.3.0",
2324
"copy-to-clipboard": "^3.3.1",
2425
"dayjs": "^1.10.4",
2526
"fathom-client": "^3.1.0",
2627
"fuse.js": "^6.4.6",
27-
"gray-matter": "^4.0.3",
2828
"nanostores": "^0.7.4",
2929
"next": "13.2.1",
30-
"next-mdx-remote": "^4.3.0",
30+
"next-contentlayer": "^0.3.0",
3131
"next-seo": "^5.15.0",
3232
"next-themes": "^0.2.1",
3333
"prismjs": "^1.29.0",

src/components/Sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const SidebarContent: React.FC = () => {
4949
} = useRouter();
5050

5151
const prefixedSlug = useMemo(
52-
() => (slug ? `/${(slug as string[])?.join("/")}` : undefined),
52+
() => (slug ? `/${(slug as string[] | undefined)?.join("/")}` : undefined),
5353
[slug],
5454
);
5555

src/layouts/DocsLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const DocsLayout: React.FC<PropsWithChildren<Props>> = ({
2828
} = useRouter();
2929

3030
const prefixedSlug = useMemo(
31-
() => `/${(slug as string[]).join("/")}`,
31+
() => `/${(slug as string[] | undefined)?.join("/")}`,
3232
[slug],
3333
);
3434
const gitHubFileLink = useMemo(

src/pages/[...slug].tsx

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import { Banner, PriorityBoardingBanner } from "@/components/Banner";
22
import { CodeBlock } from "@/components/CodeBlock";
33
import Layout from "@/mdxLayouts/index";
4-
import { FrontMatter } from "@/types";
5-
import { postFilePaths, POSTS_PATH } from "@/utils/mdxUtils";
6-
import fs from "fs";
7-
import matter from "gray-matter";
8-
import { GetStaticProps } from "next";
9-
import { MDXRemote, MDXRemoteProps } from "next-mdx-remote";
10-
import { serialize } from "next-mdx-remote/serialize";
4+
import { allPages, Page } from "contentlayer/generated";
5+
import { GetStaticPaths, GetStaticProps } from "next";
6+
import { useMDXComponent } from "next-contentlayer/hooks";
117
import { default as NextImage, ImageProps } from "next/legacy/image";
128
import Link from "next/link";
13-
import path from "path";
14-
import remarkAutolinkHeadings from "remark-autolink-headings";
15-
import remarkGfm from "remark-gfm";
16-
import remarkSlug from "remark-slug";
179

1810
const Image = (props: ImageProps) => (
1911
<a
@@ -34,46 +26,38 @@ const components = {
3426
PriorityBoardingBanner,
3527
};
3628

37-
export default function PostPage({
38-
source,
39-
frontMatter,
40-
}: {
41-
source: Omit<MDXRemoteProps, "components">;
42-
frontMatter: FrontMatter;
43-
}) {
29+
export default function PostPage({ page }: { page: Page }) {
30+
const MDXContent = useMDXComponent(page.body.code);
31+
4432
return (
45-
<Layout frontMatter={frontMatter}>
46-
<MDXRemote {...source} components={components} />
33+
<Layout
34+
frontMatter={{
35+
title: page.title,
36+
}}
37+
>
38+
<MDXContent components={components} />
4739
</Layout>
4840
);
4941
}
5042

5143
export const getStaticProps: GetStaticProps = async ({ params }) => {
52-
const postFilePath = path.join(
53-
POSTS_PATH,
54-
`${(params?.slug as string[]).join("/")}.md`,
44+
const page = allPages.find(
45+
page =>
46+
page._raw.flattenedPath ===
47+
(params?.slug as string[] | undefined)?.join("/"),
5548
);
56-
const source = fs.readFileSync(postFilePath);
57-
58-
const { content, data } = matter(source);
5949

60-
const mdxSource = await serialize(content, {
61-
// Optionally pass remark/rehype plugins
62-
mdxOptions: {
63-
remarkPlugins: [remarkAutolinkHeadings, remarkSlug, remarkGfm],
64-
rehypePlugins: [],
65-
},
66-
scope: data,
67-
});
6850
return {
6951
props: {
70-
source: mdxSource,
71-
frontMatter: data,
52+
page,
7253
},
7354
};
7455
};
7556

76-
export const getStaticPaths = async () => {
77-
const paths = postFilePaths.map(path => path.replace(/\.mdx?$/, ""));
78-
return { paths, fallback: false };
57+
export const getStaticPaths: GetStaticPaths = async () => {
58+
const paths = allPages.map(page => page.url);
59+
return {
60+
paths,
61+
fallback: false,
62+
};
7963
};

src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export interface FrontMatter {
22
title: string;
3-
id: string;
4-
wordCount: number;
53
}
64

75
export type ISidebarContent = ISidebarSection[];

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"incremental": true,
1717
"baseUrl": ".",
1818
"paths": {
19-
"@/*": ["./src/*"]
19+
"@/*": ["./src/*"],
20+
"contentlayer/generated": ["./.contentlayer/generated"]
2021
}
2122
},
2223
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],

0 commit comments

Comments
 (0)