Files
lixiyu-net/pages/blog/[id].tsx

229 lines
8.4 KiB
TypeScript
Raw Normal View History

2023-12-25 17:21:39 +08:00
import { MDXComponentsSet } from "@/components/mdx";
2024-08-16 10:41:36 +08:00
import { DrawerTOC } from "@/components/reader-page/DrawerTOC";
import { PostComments } from "@/components/reader-page/PostComments";
import { PostCover } from "@/components/reader-page/PostCover";
import { ShareButtons } from "@/components/reader-page/ShareButtons";
import { Separator } from "@/components/ui/separator";
2023-12-25 17:21:39 +08:00
import { Toaster } from "@/components/ui/toaster";
import { Footer } from "@/components/utils/Footer";
2024-08-16 14:12:30 +08:00
import { ContentContainer, Page } from "@/components/utils/Layout";
2023-12-25 17:21:39 +08:00
import { NavBar } from "@/components/utils/NavBar";
import { SEO } from "@/components/utils/SEO";
import { Config } from "@/data/config";
import { normalizeDate } from "@/lib/date";
import { getPostFileContent, sortedPosts } from "@/lib/post-process";
2024-04-03 22:08:27 +08:00
import { makeTOCTree } from "@/lib/toc";
2024-09-26 16:48:47 +08:00
import type { TPostFrontmatter } from "@/types/frontmatter.type";
import type { TPostListItem, TTOCItem } from "@/types/docs.type";
2023-12-25 17:21:39 +08:00
import { nanoid } from "nanoid";
import type { GetStaticPaths, GetStaticProps } from "next";
import { MDXRemote, type MDXRemoteSerializeResult } from "next-mdx-remote";
2023-12-25 17:21:39 +08:00
import { serialize } from "next-mdx-remote/serialize";
import Link from "next/link";
import { renderToString } from "react-dom/server";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
2024-04-03 22:08:27 +08:00
import rehypeHighlight from "rehype-highlight";
2023-12-25 17:21:39 +08:00
import rehypeKatex from "rehype-katex";
import rehypePresetMinify from "rehype-preset-minify";
import rehypeRaw from "rehype-raw";
2023-12-25 17:21:39 +08:00
import rehypeSlug from "rehype-slug";
import externalLinks from "remark-external-links";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
2024-08-16 17:56:55 +08:00
import { titleCase } from "title-case";
2023-12-25 17:21:39 +08:00
type ReaderPageProps = {
compiledSource: MDXRemoteSerializeResult;
2023-12-25 17:21:39 +08:00
tocList: TTOCItem[];
2024-09-26 16:48:47 +08:00
frontMatter: TPostFrontmatter;
2023-12-25 17:21:39 +08:00
postId: string;
nextPostListItem: TPostListItem | null;
prevPostListItem: TPostListItem | null;
};
const ReaderPage = (props: ReaderPageProps) => {
const compiledSource = props.compiledSource;
2024-08-16 17:56:55 +08:00
// const setIsTOCOpen = useDrawerTOCState((state) => state.changeDrawerTOCOpen);
// Only the TOC length reaches 3 can be displayed.
// In order to avoid large blank spaces that ruin the visual perception
2024-04-03 22:08:27 +08:00
const isTOCLongEnough = props.tocList.length > 2;
2024-08-16 17:56:55 +08:00
// const handleLeftSwipe = useSwipeable({
// onSwipedLeft: () => isTOCLongEnough && setIsTOCOpen(true),
// delta: 150,
// });
2023-12-25 17:21:39 +08:00
return (
<Page>
<SEO
2024-08-16 17:56:55 +08:00
coverURL={props.frontMatter.coverURL}
2024-04-03 22:08:27 +08:00
description={props.frontMatter.summary}
2024-08-16 17:56:55 +08:00
title={`${titleCase(props.frontMatter.title)} - ${Config.SiteTitle}`}
2023-12-25 17:21:39 +08:00
/>
<Toaster />
<NavBar />
2023-12-25 17:21:39 +08:00
<ContentContainer>
2024-08-16 17:56:55 +08:00
<div className="flex justify-center py-5">
<div className="typesetting" style={{ width: "min(50rem,100%)" }}>
{props.frontMatter.coverURL && <PostCover coverURL={props.frontMatter.coverURL} />}
<div className="border-black border-b-2 pb-1 dark:border-gray-300">
<div
className={
"caption-font my-2 flex justify-center whitespace-normal break-words font-bold text-3xl text-black dark:text-white"
}
>
{props.frontMatter?.title}
</div>
{props.frontMatter?.subtitle && (
<div className={"my-1 flex justify-center font-bold text-xl content-font"}>
{props.frontMatter.subtitle}
</div>
)}
<div className="my-1 flex justify-center text-sm italic">{normalizeDate(props.frontMatter?.time)}</div>
{props.frontMatter?.summary && (
<p
className={
2024-08-16 17:56:55 +08:00
"my-4 border-gray-400 border-l-4 bg-gray-100 py-5 pr-2 pl-5 text-[16px] text-gray-800 content-font dark:border-gray-600 dark:bg-gray-900 dark:text-gray-300"
}
>
2024-08-16 17:56:55 +08:00
{props.frontMatter?.summary}
</p>
)}
{props.frontMatter.tags && (
<div className={"flex flex-wrap border-black border-t-2 pt-1 dark:border-gray-300"}>
{props.frontMatter.tags.map((tagName) => (
<Link
className="not-prose my-1 mr-2 border-2 border-black px-2 py-1 font-bold text-gray-700 text-xs hover:text-black dark:border-gray-300 dark:text-gray-300 dark:hover:text-gray-200"
href={`/tags/${tagName}`}
key={`tags-${nanoid()}`}
>
{tagName}
</Link>
))}
2023-12-25 17:21:39 +08:00
</div>
2024-08-16 17:56:55 +08:00
)}
</div>
<div
className={`text-wrap border-gray-500 content-font ${!props.frontMatter.allowShare ? "select-none" : ""}`}
// {...handleLeftSwipe}
>
{compiledSource && (
<MDXRemote
compiledSource={compiledSource.compiledSource}
// @ts-ignore
components={MDXComponentsSet}
frontmatter={compiledSource.frontmatter}
scope={compiledSource.scope}
/>
)}
2023-12-25 17:21:39 +08:00
</div>
<Separator />
2024-08-16 17:56:55 +08:00
{/* <BottomCard /> */}
2024-04-03 22:08:27 +08:00
<Separator />
2023-12-25 17:21:39 +08:00
<ShareButtons
2024-04-03 22:08:27 +08:00
allowShare={props.frontMatter.allowShare}
postId={props.postId}
quote={props.frontMatter.summary}
2023-12-25 17:21:39 +08:00
subtitle={props.frontMatter.subtitle}
title={props.frontMatter.title}
/>
<Separator />
<ul className="my-5 flex list-disc flex-col justify-center px-5">
2023-12-25 17:21:39 +08:00
{props.prevPostListItem && (
<li className="my-1">
<Link
className=" hover:text-sky-600 dark:hover:text-sky-500"
href={`/blog/${props.prevPostListItem?.id}`}
>
{props.prevPostListItem?.frontMatter.title}
</Link>
</li>
)}
{props.nextPostListItem && (
<li className="my-1">
<Link
className=" hover:text-sky-600 dark:hover:text-sky-500"
href={`/blog/${props.nextPostListItem?.id}`}
>
{props.nextPostListItem?.frontMatter.title}
</Link>
</li>
)}
</ul>
2024-05-03 22:09:58 +08:00
{Config.Giscus?.enabled && <PostComments postId={props.postId} />}
2023-12-25 17:21:39 +08:00
</div>
</div>
2024-08-16 17:56:55 +08:00
{isTOCLongEnough && <DrawerTOC data={props.tocList} />}
2023-12-25 17:21:39 +08:00
</ContentContainer>
<Footer />
</Page>
);
};
export const getStaticPaths: GetStaticPaths<{ id: string }> = async () => {
const allPaths = sortedPosts.allPostList.map((item) => ({
params: { id: item.id },
}));
return {
paths: allPaths,
fallback: false,
};
};
export const getStaticProps: GetStaticProps<ReaderPageProps> = async (context) => {
const postId = context.params?.id;
if (postId == null || Array.isArray(postId)) {
return { notFound: true };
}
const source = getPostFileContent(postId);
if (source == null) {
return { notFound: true };
}
const mdxSource = await serialize(source, {
parseFrontmatter: true,
mdxOptions: {
2024-04-03 22:08:27 +08:00
remarkPlugins: [externalLinks, remarkMath, remarkGfm],
rehypePlugins: [
rehypeRaw,
2024-08-16 17:56:55 +08:00
2024-04-03 22:08:27 +08:00
rehypeKatex as any,
rehypeAutolinkHeadings,
rehypeSlug,
rehypePresetMinify.plugins,
() => rehypeHighlight({ detect: true }),
],
2023-12-25 17:21:39 +08:00
format: "md",
},
});
2024-04-03 22:08:27 +08:00
const tocList = makeTOCTree(renderToString(<MDXRemote {...mdxSource} />));
2023-12-25 17:21:39 +08:00
const postIndexInAllPosts = sortedPosts.allPostList.findIndex((item) => item.id === postId);
2024-09-26 16:48:47 +08:00
const frontMatter: TPostFrontmatter = sortedPosts.allPostList[postIndexInAllPosts].frontMatter;
2023-12-25 17:21:39 +08:00
const nextPostListItem =
postIndexInAllPosts !== sortedPosts.allPostList.length - 1
? sortedPosts.allPostList[postIndexInAllPosts + 1]
: null;
const prevPostListItem = postIndexInAllPosts !== 0 ? sortedPosts.allPostList[postIndexInAllPosts - 1] : null;
return {
props: {
compiledSource: mdxSource,
2023-12-25 17:21:39 +08:00
tocList: tocList,
frontMatter: frontMatter,
postId: postId,
nextPostListItem: nextPostListItem,
prevPostListItem: prevPostListItem,
},
};
};
export default ReaderPage;