|
1 | | -import { getFile, putFile, listFiles } from './github'; |
| 1 | +import { getFile, putFile, listFiles, isFileCached } from './github'; |
2 | 2 | import { parseFrontmatter, stringifyFrontmatter, generateId, transliterate, normalizeText, slugify } from './utils'; |
3 | 3 |
|
4 | 4 | export interface Forum { |
@@ -45,12 +45,22 @@ function matchesSearch(item: { author: string; body: string }, options?: { autho |
45 | 45 | return true; |
46 | 46 | } |
47 | 47 |
|
| 48 | +const cacheFilePaths = async (files: Array<{ name: string; path?: string }>) => { |
| 49 | + await Promise.all( |
| 50 | + files |
| 51 | + .filter(file => file.name.endsWith('.md') && file.path && !isFileCached(file.path)) |
| 52 | + .map(file => getFile(file.path!)) |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
48 | 56 | export async function listForums(): Promise<Forum[]> { |
49 | 57 | const files = await listFiles('forums'); |
50 | 58 | const forums: Forum[] = []; |
51 | 59 |
|
| 60 | + await cacheFilePaths(files); |
| 61 | + |
52 | 62 | for (const file of files) { |
53 | | - if (file.name.endsWith('.md')) { |
| 63 | + if (file.name.endsWith('.md') && file.path) { |
54 | 64 | const fileData = await getFile(file.path); |
55 | 65 | if (fileData) { |
56 | 66 | const { data } = parseFrontmatter<Forum>(fileData.content); |
@@ -88,8 +98,10 @@ export async function listTopics(forumSlug: string, options?: { author?: string; |
88 | 98 | const files = await listFiles('topics'); |
89 | 99 | const topics: Topic[] = []; |
90 | 100 |
|
| 101 | + await cacheFilePaths(files); |
| 102 | + |
91 | 103 | for (const file of files) { |
92 | | - if (file.name.endsWith('.md')) { |
| 104 | + if (file.name.endsWith('.md') && file.path) { |
93 | 105 | const fileData = await getFile(file.path); |
94 | 106 | if (fileData) { |
95 | 107 | const { data, content } = parseFrontmatter<Topic>(fileData.content); |
@@ -148,8 +160,10 @@ export async function listPosts(topicId: string, options?: { author?: string; te |
148 | 160 | const files = await listFiles('posts'); |
149 | 161 | const posts: Post[] = []; |
150 | 162 |
|
| 163 | + await cacheFilePaths(files); |
| 164 | + |
151 | 165 | for (const file of files) { |
152 | | - if (file.name.endsWith('.md')) { |
| 166 | + if (file.name.endsWith('.md') && file.path) { |
153 | 167 | const fileData = await getFile(file.path); |
154 | 168 | if (fileData) { |
155 | 169 | const { data, content } = parseFrontmatter<Post>(fileData.content); |
|
0 commit comments