Skip to content

Commit

Permalink
feat(contents): set list meta cache as static import
Browse files Browse the repository at this point in the history
  • Loading branch information
chengpeiquan committed Oct 20, 2024
1 parent 1086167 commit 88f0d04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/contents/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import { extname, join } from 'node:path'
import { readdirSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { z } from 'zod'
import { type Locale, locales } from '@/config/locale-config'
import {
type ContentFolder,
type ContentItem,
type GetListResponse,
type ListFolder,
type MetaCacheItem,
contentFolders,
contentItemSchema,
Expand All @@ -18,6 +18,7 @@ import {
isValidContentItem,
} from '@/config/content-config'
import { type ParseOptions, parse } from './parser'
import { metaCacheMap } from './meta-cache'

const contentRootPath = join(process.cwd(), 'src', contentRootFolder)

Expand All @@ -34,17 +35,11 @@ export const getMetaCachePath = async (
}

export const getMetaCache = async (
folder: ContentFolder,
folder: ListFolder,
locale: Locale,
): Promise<MetaCacheItem[]> => {
try {
const { filePath } = await getMetaCachePath(folder, locale)
const raw = await readFile(filePath, 'utf-8')
if (!raw) return []
return JSON.parse(raw)
} catch (e) {
return []
}
const cache = metaCacheMap.get(`${folder}_${locale}`)
return cache || []
}

const getMarkdownFiles = (dir: string) => {
Expand Down
13 changes: 13 additions & 0 deletions src/contents/meta-cache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type Locale } from '@/config/locale-config'
import { type ListFolder, type MetaCacheItem } from '@/config/content-config'
import article_en from './article-en.json'
import article_zh from './article-zh.json'
import cookbook_zh from './cookbook-zh.json'

type MetaCacheMapKey = `${ListFolder}_${Locale}`

export const metaCacheMap = new Map<MetaCacheMapKey, MetaCacheItem[]>([
['article_en', article_en as MetaCacheItem[]],
['article_zh', article_zh as MetaCacheItem[]],
['cookbook_zh', cookbook_zh as MetaCacheItem[]],
])

0 comments on commit 88f0d04

Please sign in to comment.