| 
									
										
										
										
											2024-08-12 13:53:28 +08:00
										 |  |  | import { PostFilesDirectory } from "@/consts/consts"; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | import { TFrontmatter } from "@/types/frontmatter.type"; | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  | import { TPostListItem, TPostsByTag } from "@/types/post-list"; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | import fs from "fs"; | 
					
						
							|  |  |  | import { serialize } from "next-mdx-remote/serialize"; | 
					
						
							|  |  |  | import path from "path"; | 
					
						
							| 
									
										
										
										
											2024-08-12 13:55:30 +08:00
										 |  |  | import { titleCase } from "title-case"; | 
					
						
							| 
									
										
										
										
											2024-01-06 20:20:05 +08:00
										 |  |  | import { isEmptyString, nullifyEmptyArray, nullifyEmptyString } from "./utils"; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  | async function extractFrontmatters(filepath: string): Promise<TFrontmatter> { | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |   const source = fs.readFileSync(filepath, "utf-8"); | 
					
						
							|  |  |  |   const mdxSource = await serialize(source, { | 
					
						
							|  |  |  |     parseFrontmatter: true, | 
					
						
							|  |  |  |     mdxOptions: { format: "md" }, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   const frontmatter = mdxSource.frontmatter as TFrontmatter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const normalizedTags = frontmatter.tags | 
					
						
							|  |  |  |     ?.filter((tagname) => !isEmptyString(tagname)) | 
					
						
							|  |  |  |     .map((tagname) => tagname.toUpperCase()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const normalizedResult: TFrontmatter = { | 
					
						
							| 
									
										
										
										
											2024-08-12 13:55:30 +08:00
										 |  |  |     title: titleCase(frontmatter.title), | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |     subtitle: nullifyEmptyString(frontmatter.subtitle), | 
					
						
							|  |  |  |     coverURL: nullifyEmptyString(frontmatter.coverURL), | 
					
						
							|  |  |  |     tags: nullifyEmptyArray(normalizedTags), | 
					
						
							|  |  |  |     summary: nullifyEmptyString(frontmatter.summary), | 
					
						
							|  |  |  |     time: frontmatter.time, | 
					
						
							|  |  |  |     pin: frontmatter.pin ?? false, | 
					
						
							|  |  |  |     noPrompt: frontmatter.noPrompt ?? false, | 
					
						
							|  |  |  |     allowShare: frontmatter.allowShare ?? true, | 
					
						
							|  |  |  |     closed: frontmatter.closed ?? false, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return normalizedResult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function readPostsDirectory(): string[] { | 
					
						
							|  |  |  |   const result: string[] = []; | 
					
						
							| 
									
										
										
										
											2024-08-12 13:53:28 +08:00
										 |  |  |   fs.readdirSync(PostFilesDirectory).forEach((fileName) => { | 
					
						
							|  |  |  |     const filePath = path.join(PostFilesDirectory, fileName); | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |     const fileStat = fs.statSync(filePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fileStat.isFile() && fileName.endsWith(".md")) { | 
					
						
							|  |  |  |       result.push(filePath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getPostFileContent = (postId: string): string | null => { | 
					
						
							| 
									
										
										
										
											2024-08-12 13:53:28 +08:00
										 |  |  |   const filePath = path.join(PostFilesDirectory, `${postId}.md`); | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |   if (!fs.existsSync(filePath)) return null; | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   const content = fs.readFileSync(filePath, "utf-8"); | 
					
						
							|  |  |  |   return content; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  | const sortOutPosts = async (): Promise<{ | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |   allPostList: TPostListItem[]; | 
					
						
							|  |  |  |   pinnedPostList: TPostListItem[]; | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   postsByTag: TPostsByTag; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | }> => { | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   const allPostList: TPostListItem[] = []; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |   const pinnedPostList: TPostListItem[] = []; | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   const postsByTag: TPostsByTag = {}; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   const postFilePaths: string[] = readPostsDirectory(); | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (let i = 0; i < postFilePaths.length; i++) { | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |     const frontmatter = await extractFrontmatters(postFilePaths[i]); | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |     const postId = path.parse(postFilePaths[i]).name; | 
					
						
							| 
									
										
										
										
											2024-01-06 20:20:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |     const currentPostListItem: TPostListItem = { | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |       id: postId, | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |       frontMatter: frontmatter, | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |     if (!currentPostListItem.frontMatter.closed) { | 
					
						
							|  |  |  |       allPostList.push(currentPostListItem); | 
					
						
							|  |  |  |       if (currentPostListItem.frontMatter.pin) { | 
					
						
							|  |  |  |         pinnedPostList.push(currentPostListItem); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   pinnedPostList.sort((a, b) => { | 
					
						
							|  |  |  |     return a.frontMatter.time > b.frontMatter.time ? -1 : 1; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   allPostList.sort((a, b) => { | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |     return a.frontMatter.time > b.frontMatter.time ? -1 : 1; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   allPostList.forEach((item) => { | 
					
						
							|  |  |  |     item.frontMatter.tags?.forEach((tagName) => { | 
					
						
							|  |  |  |       if (postsByTag[tagName] == null) { | 
					
						
							|  |  |  |         postsByTag[tagName] = []; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |       postsByTag[tagName].push(item); | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  |   return { allPostList: allPostList, postsByTag: postsByTag, pinnedPostList: pinnedPostList }; | 
					
						
							| 
									
										
										
										
											2023-12-25 17:21:39 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 22:08:27 +08:00
										 |  |  | export const sortedPosts = await sortOutPosts(); |