|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import matter from 'gray-matter'; |
| 4 | +import { serialize } from 'next-mdx-remote/serialize'; |
| 5 | +import remarkGfm from 'remark-gfm'; |
| 6 | +import rehypeSlug from 'rehype-slug'; |
| 7 | +import rehypePrism from 'rehype-prism-plus'; |
| 8 | +import rehypeAutolinkHeadings from 'rehype-autolink-headings'; |
| 9 | +import remarkDirective from 'remark-directive'; |
| 10 | +import remarkDirectiveTransformer from './remarkDirectiveTransformer'; |
| 11 | + |
| 12 | +const postsDirectory = path.join(process.cwd(), 'posts'); |
| 13 | + |
| 14 | +type PostFrontmatter = { |
| 15 | + title: string; |
| 16 | + date: string; |
| 17 | + tags?: string[]; |
| 18 | + summary?: string; |
| 19 | + description?: string; |
| 20 | + [key: string]: any; |
| 21 | +}; |
| 22 | + |
| 23 | +type PostData = PostFrontmatter & { |
| 24 | + id: string; |
| 25 | + mdxSource: Awaited<ReturnType<typeof serialize>>; |
| 26 | +}; |
| 27 | + |
| 28 | +export function getAllMdxFiles(dirPath = postsDirectory): string[] { |
| 29 | + let results: string[] = []; |
| 30 | + const list = fs.readdirSync(dirPath); |
| 31 | + list.forEach((file) => { |
| 32 | + const filePath = path.join(dirPath, file); |
| 33 | + const stat = fs.statSync(filePath); |
| 34 | + if (stat && stat.isDirectory()) { |
| 35 | + results = results.concat(getAllMdxFiles(filePath)); |
| 36 | + } else if (file.endsWith('.mdx')) { |
| 37 | + results.push(filePath); |
| 38 | + } |
| 39 | + }); |
| 40 | + return results; |
| 41 | +} |
| 42 | + |
| 43 | +function extractIdFromFilename(fullPath: string): string { |
| 44 | + return path.basename(fullPath, '.mdx'); |
| 45 | +} |
| 46 | + |
| 47 | +export function getAllPostIds(): { params: { id: string } }[] { |
| 48 | + const allFiles = getAllMdxFiles(); |
| 49 | + return allFiles.map((fullPath) => ({ |
| 50 | + params: { |
| 51 | + id: extractIdFromFilename(fullPath), |
| 52 | + }, |
| 53 | + })); |
| 54 | +} |
| 55 | + |
| 56 | +export async function getPostData(id: string): Promise<PostData> { |
| 57 | + const allFiles = getAllMdxFiles(); |
| 58 | + const matchPath = allFiles.find((fp) => path.basename(fp, '.mdx') === id); |
| 59 | + if (!matchPath) throw new Error(`Post not found: ${id}`); |
| 60 | + |
| 61 | + const fileContents = fs.readFileSync(matchPath, 'utf8'); |
| 62 | + const { content, data } = matter(fileContents); |
| 63 | + |
| 64 | + const mdxSource = await serialize(content, { |
| 65 | + mdxOptions: { |
| 66 | + remarkPlugins: [remarkGfm, remarkDirective, remarkDirectiveTransformer], |
| 67 | + rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings, rehypePrism], |
| 68 | + }, |
| 69 | + scope: data, |
| 70 | + }); |
| 71 | + |
| 72 | + return { |
| 73 | + id, |
| 74 | + mdxSource, |
| 75 | + ...(data as PostFrontmatter), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +export async function getSortedPostsDataWithContent(): Promise<PostData[]> { |
| 80 | + const allFiles = getAllMdxFiles(); |
| 81 | + |
| 82 | + const allPostsData: PostData[] = await Promise.all( |
| 83 | + allFiles.map(async (fullPath) => { |
| 84 | + const id = extractIdFromFilename(fullPath); |
| 85 | + const fileContents = fs.readFileSync(fullPath, 'utf8'); |
| 86 | + const { content, data } = matter(fileContents); |
| 87 | + const mdxSource = await serialize(content, { |
| 88 | + mdxOptions: { |
| 89 | + remarkPlugins: [remarkGfm], |
| 90 | + rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings], |
| 91 | + format: 'mdx', |
| 92 | + }, |
| 93 | + scope: data, |
| 94 | + }); |
| 95 | + |
| 96 | + return { |
| 97 | + id, |
| 98 | + mdxSource, |
| 99 | + ...(data as PostFrontmatter), |
| 100 | + }; |
| 101 | + }) |
| 102 | + ); |
| 103 | + |
| 104 | + return allPostsData.sort( |
| 105 | + (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() |
| 106 | + ); |
| 107 | +} |
0 commit comments