-
-
Notifications
You must be signed in to change notification settings - Fork 605
/
content-index.ts
54 lines (44 loc) · 1.99 KB
/
content-index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { H3Event } from 'h3'
import type { ParsedContent } from '../types'
import type { ContentQueryBuilder } from '../types/query'
import { isPreview } from './preview'
import { cacheStorage, chunksFromArray, getContent, getContentsList } from './storage'
import { useRuntimeConfig } from '#imports'
export async function getContentIndex (event: H3Event) {
const defaultLocale = useRuntimeConfig().content.defaultLocale
let contentIndex = await cacheStorage.getItem('content-index.json') as Record<string, string[]>
if (!contentIndex) {
// Fetch all contents
const data = await getContentsList(event)
contentIndex = data.reduce((acc: Record<string, string[]>, item: ParsedContent) => {
acc[item._path!] = acc[item._path!] || []
if (item._locale === defaultLocale) {
acc[item._path!].unshift(item._id)
} else {
acc[item._path!].push(item._id)
}
return acc
}, {} as Record<string, string[]>)
await cacheStorage.setItem('content-index.json', contentIndex)
}
return contentIndex
}
export async function getIndexedContentsList<T = ParsedContent> (event: H3Event, query: ContentQueryBuilder<T>): Promise<T[]> {
const params = query.params()
const path = params?.where?.find(wh => wh._path)?._path
// Read from Index is not preview and path is string or RegExp
if (!isPreview(event) && !params.surround && !params.dirConfig && (typeof path === 'string' || path instanceof RegExp)) {
const index = await getContentIndex(event)
const keys = Object.keys(index)
.filter(key => (path as any).test ? (path as any).test(key) : key === String(path))
.flatMap(key => index[key])
const keyChunks = [...chunksFromArray(keys, 10)]
const contents = []
for await (const chunk of keyChunks) {
const result = await Promise.all(chunk.map(key => getContent(event, key)))
contents.push(...result)
}
return contents as unknown as Promise<T[]>
}
return getContentsList(event) as unknown as Promise<T[]>
}