|
1 | 1 | import jsonData from '@md/index.json'; |
2 | 2 | import { join } from 'node:path'; |
| 3 | +import { existsSync, writeFileSync } from 'node:fs'; |
3 | 4 |
|
4 | 5 | import { symbolsTime, symbolsCount } from '@/lib/count'; |
5 | 6 | import { getFirstGitCommitTime, getLastGitUpdateTime } from '@/lib/git'; |
@@ -28,58 +29,83 @@ export type PostJsonType = { |
28 | 29 | coverImage: string; |
29 | 30 | summary?: string; |
30 | 31 | }; |
| 32 | + |
31 | 33 | export type PostMap = Record<string, PostItem>; |
32 | 34 |
|
33 | 35 | export function importMarkdownFile(path: string) { |
34 | | - // 使用 require.context 获取所有 markdown 文件 |
35 | 36 | const markdownContext = (require as any).context('../../markdown', true, /\.md$/); |
36 | 37 |
|
37 | 38 | return markdownContext(path); |
38 | 39 | } |
39 | 40 |
|
| 41 | +export function importJsonFile(path: string) { |
| 42 | + const jsonContext = (require as any).context('../../markdown', true, /\.json$/); |
| 43 | + |
| 44 | + return jsonContext(path); |
| 45 | +} |
| 46 | + |
| 47 | +export function writeOutputFile(data: { postDataMap: PostMap; postDataList: PostItem[] }) { |
| 48 | + const outputContent = { |
| 49 | + postDataMap: data.postDataMap, |
| 50 | + postDataList: data.postDataList, |
| 51 | + }; |
| 52 | + |
| 53 | + const outputPath = join(process.cwd(), 'markdown/output.json'); |
| 54 | + writeFileSync(outputPath, JSON.stringify(outputContent, null, 2)); |
| 55 | + |
| 56 | + console.log(`Data written to ${outputPath}`); |
| 57 | +} |
| 58 | + |
40 | 59 | export function buildPostData() { |
41 | 60 | const postDataMap: PostMap = {}; |
42 | 61 | const postDataList: PostItem[] = []; |
43 | 62 |
|
44 | | - function processPostItem(item: PostJsonType) { |
45 | | - const itemInfo = {} as PostItem; |
46 | | - const file = importMarkdownFile(`./${item.path}`); |
47 | | - itemInfo.authors = item.authors; |
48 | | - itemInfo.title = item.title; |
49 | | - itemInfo.tag = item.tag; |
50 | | - itemInfo.path = item.path.replace('.md', ''); |
51 | | - itemInfo.rawFilePath = `./${item.path}`; |
52 | | - itemInfo.summary = item.summary; |
53 | | - itemInfo.coverImage = item.coverImage.startsWith('http') |
54 | | - ? item.coverImage |
55 | | - : `/postCoverImage/${item.coverImage}`; |
56 | | - |
57 | | - itemInfo.text = file; |
58 | | - itemInfo.count = symbolsCount(file); |
59 | | - itemInfo.readingTime = symbolsTime(file, 0, 200); |
60 | | - itemInfo.updatedAt = getLastGitUpdateTime(join('markdown/', item.path)); |
61 | | - itemInfo.createdAt = getFirstGitCommitTime(join('markdown/', item.path)); |
62 | | - |
63 | | - itemInfo.modified = |
64 | | - itemInfo.updatedAt && itemInfo.createdAt |
65 | | - ? itemInfo.updatedAt.getTime() !== itemInfo.createdAt.getTime() |
66 | | - : false; |
67 | | - |
68 | | - postDataMap[itemInfo.path] = itemInfo; |
69 | | - postDataList.push(itemInfo); |
| 63 | + function processPostItem({ authors, title, tag, path, coverImage, summary }: PostJsonType) { |
| 64 | + const file = importMarkdownFile(`./${path}`); |
| 65 | + const createdAt = getFirstGitCommitTime(join('markdown/', path)); |
| 66 | + const updatedAt = getLastGitUpdateTime(join('markdown/', path)); |
| 67 | + |
| 68 | + const postItem: PostItem = { |
| 69 | + authors, |
| 70 | + title, |
| 71 | + tag, |
| 72 | + path: path.replace('.md', ''), |
| 73 | + rawFilePath: `./${path}`, |
| 74 | + summary, |
| 75 | + coverImage: coverImage.startsWith('http') ? coverImage : `/postCoverImage/${coverImage}`, |
| 76 | + text: file, |
| 77 | + count: symbolsCount(file), |
| 78 | + readingTime: symbolsTime(file, 0, 200), |
| 79 | + createdAt, |
| 80 | + updatedAt, |
| 81 | + modified: updatedAt && createdAt ? updatedAt.getTime() !== createdAt.getTime() : false, |
| 82 | + }; |
| 83 | + |
| 84 | + postDataMap[postItem.path] = postItem; |
| 85 | + postDataList.push(postItem); |
70 | 86 | } |
71 | 87 |
|
72 | | - jsonData.forEach((postJsonItem: PostJsonType) => { |
73 | | - processPostItem(postJsonItem); |
74 | | - }); |
| 88 | + jsonData.forEach((postJsonItem) => processPostItem(postJsonItem)); |
| 89 | + |
| 90 | + postDataList.sort((a, b) => (b.createdAt?.getTime() || 0) - (a.createdAt?.getTime() || 0)); |
| 91 | + |
| 92 | + const result = { postDataMap, postDataList }; |
| 93 | + writeOutputFile(result); |
75 | 94 |
|
76 | | - postDataList.sort((a, b) => { |
77 | | - if (a.createdAt && b.createdAt) { |
78 | | - return b.createdAt.getTime() - a.createdAt.getTime(); |
79 | | - } |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +export async function getPostData(): Promise<{ postDataMap: PostMap; postDataList: PostItem[] }> { |
| 99 | + const outputPath = join(process.cwd(), 'markdown/output.json'); |
80 | 100 |
|
81 | | - return 0; |
82 | | - }); |
| 101 | + if (existsSync(outputPath)) { |
| 102 | + const { postDataMap, postDataList } = importJsonFile('./output.json'); |
| 103 | + console.log('Using cached post data.'); |
83 | 104 |
|
84 | | - return { postDataMap, postDataList }; |
| 105 | + return { postDataMap, postDataList }; |
| 106 | + } else { |
| 107 | + console.log('Output file not found, generating post data...'); |
| 108 | + |
| 109 | + return buildPostData(); |
| 110 | + } |
85 | 111 | } |
0 commit comments