-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
navigation.ts
56 lines (50 loc) · 1.73 KB
/
navigation.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
55
56
import { cachedEventHandler } from 'nitropack/dist/runtime/cache'
import { cacheStorage, serverQueryContent } from '../storage'
import { createNav } from '../navigation'
import type { ParsedContent, ParsedContentMeta } from '../../types'
import { getContentQuery } from '../../utils/query'
import { isPreview } from '../preview'
export default cachedEventHandler(async (event) => {
const query = getContentQuery(event)
// Read from cache if not preview and there is no query
if (!isPreview(event) && Object.keys(query).length === 0) {
const cache = await cacheStorage.getItem('content-navigation.json')
if (cache) {
return cache
}
}
const contents = await serverQueryContent(event, query)
.where({
/**
* Partial contents are not included in the navigation
* A partial content is a content that has `_` prefix in its path
*/
_partial: false,
/**
* Exclude any pages which have opted out of navigation via frontmatter.
*/
navigation: {
$ne: false
}
})
.find()
const dirConfigs = await serverQueryContent(event)
.where({ _path: /\/_dir$/i, _partial: true })
.find()
const configs = (dirConfigs?.result || dirConfigs).reduce((configs, conf: ParsedContent) => {
if (conf.title?.toLowerCase() === 'dir') {
conf.title = undefined
}
const key = conf._path!.split('/').slice(0, -1).join('/') || '/'
configs[key] = {
...conf,
// Extract meta from body. (non MD files)
...conf.body
}
return configs
}, {} as Record<string, ParsedContentMeta>)
return createNav((contents?.result || contents) as ParsedContentMeta[], configs)
}, {
maxAge: 31536000,
shouldBypassCache: () => !!import.meta.dev
})