Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit ecad5db

Browse files
committed
chore: add faq contentlayer
1 parent 96401e2 commit ecad5db

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

contentlayer.config.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,30 @@ const Doc = defineDocumentType(() => ({
9393
},
9494
}));
9595

96+
const FAQ = defineDocumentType(() => ({
97+
name: 'FAQ',
98+
filePathPattern: 'faqs/*.mdx',
99+
bodyType: 'mdx',
100+
fields: {
101+
title: { type: 'string', required: true },
102+
description: { type: 'string', required: true },
103+
},
104+
computedFields: {
105+
...computedFields,
106+
frontMatter: {
107+
type: 'json',
108+
resolve: (doc) => ({
109+
title: doc.title,
110+
description: doc.description,
111+
slug: `/${doc._raw.flattenedPath}`,
112+
}),
113+
},
114+
},
115+
}));
116+
96117
const contentLayerConfig = makeSource({
97118
contentDirPath: 'pages',
98-
documentTypes: [Blog, Doc, Guides],
119+
documentTypes: [Blog, Doc, Guides, FAQ],
99120
mdx: {
100121
rehypePlugins: [rehypeMdxCodeMeta],
101122
remarkPlugins: [remarkSlug, remarkGfm, remarkEmoji],

pages/faq.tsx

-34
This file was deleted.

pages/faqs/[[...slug]].tsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { allFAQs } from '.contentlayer/data';
2+
import type { FAQ } from '.contentlayer/types';
3+
import { MDXComponents } from 'components/mdx-components';
4+
import { GetStaticPaths, GetStaticProps } from 'next';
5+
import { useMDXComponent } from 'next-contentlayer/hooks';
6+
import Layout from 'layouts';
7+
8+
export default function Page({ faq }: { faq: FAQ }) {
9+
const Component = useMDXComponent(faq.body.code);
10+
return (
11+
<Layout frontMatter={faq.frontMatter}>
12+
<Component components={MDXComponents} />
13+
</Layout>
14+
);
15+
}
16+
17+
export const getStaticPaths: GetStaticPaths = async () => {
18+
const faqs = allFAQs
19+
.map((t) =>
20+
t._id.replace('faqs/', '').replace('.mdx', '').replace('index', ''),
21+
)
22+
.map((id) => ({ params: { slug: id.split('/') } }));
23+
return { paths: faqs, fallback: false };
24+
};
25+
26+
export const getStaticProps: GetStaticProps = async (ctx) => {
27+
const params = Array.isArray(ctx.params.slug)
28+
? ctx.params.slug
29+
: [ctx.params.slug];
30+
31+
const faq = allFAQs.find((faq) => faq._id.includes(params.join('/')));
32+
return { props: { faq } };
33+
};
File renamed without changes.

0 commit comments

Comments
 (0)