From 3c5bb7bd602f429f91fad47e6510e1d29d373ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Felipe=20Gon=C3=A7alves?= Date: Thu, 1 Dec 2022 17:34:24 -0300 Subject: [PATCH] feat: use contentlayer on knowledge page --- src/pages/knowledge/[id].tsx | 56 +++++++++++++----------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/src/pages/knowledge/[id].tsx b/src/pages/knowledge/[id].tsx index 0b865041..65da35d8 100644 --- a/src/pages/knowledge/[id].tsx +++ b/src/pages/knowledge/[id].tsx @@ -2,14 +2,9 @@ import React from 'react' import { GetStaticPaths, GetStaticProps } from 'next' import Head from 'next/head' import Link from 'next/link' +import { useMDXComponent } from 'next-contentlayer/hooks' -import { - getAllKnowledgeIds, - getKnowledgeData -} from '../../lib/knowledgeFunctions' -import { getHashHeaderOfData } from '../../lib/utils' -import { KnowledgeData } from '../../lib/types' -import { unifiedContentProcessor } from '../../lib/unifiedContentProcessor' +import { allPosts, type Post } from 'contentlayer/generated' import { Container } from '../../components/Container' import { Header } from '../../components/Header' @@ -18,23 +13,18 @@ import { ReadProgress } from '../../components/ReadProgress' import { RiHistoryLine } from 'react-icons/ri' -import 'highlight.js/styles/github.css' - -interface KnowledgeDataWithContent extends KnowledgeData { - content: string -} interface Props { - knowledgeData: KnowledgeDataWithContent - knowledgeId: string + post: Post } -const Knowledge: React.FC = ({ knowledgeData, knowledgeId }) => { - const tags = knowledgeData.tags.split(',') +const Knowledge = ({ post }: Props) => { + const tags = post.tags.split(',') + const MDXContent = useMDXComponent(post.body.code) return (
- {knowledgeData.title} + {post.title} @@ -42,25 +32,22 @@ const Knowledge: React.FC = ({ knowledgeData, knowledgeId }) => {
-

{knowledgeData.title}

+

{post.title}

- •{' '} - - {knowledgeData.category} - + •{' '} + {post.category}

- {knowledgeData.lastUpdate && ( + {post.lastUpdate && (

- {' '} - +

)}

@@ -75,7 +62,7 @@ const Knowledge: React.FC = ({ knowledgeData, knowledgeId }) => {

- {unifiedContentProcessor(knowledgeData.content)} +
@@ -86,23 +73,20 @@ const Knowledge: React.FC = ({ knowledgeData, knowledgeId }) => { export default Knowledge export const getStaticPaths: GetStaticPaths = async () => { - const paths = getAllKnowledgeIds() return { - paths, + paths: allPosts.map(post => ({ + params: { + id: post.id + } + })), fallback: false } } export const getStaticProps: GetStaticProps = async ({ params }) => { - const knowledgeData = await getKnowledgeData( - typeof params.id === 'string' ? params.id : '' - ) - const knowledgeId = getHashHeaderOfData(knowledgeData.content) - return { props: { - knowledgeData, - knowledgeId + post: allPosts.find(post => post.id === params?.id) } } }