/
page-map.ts
42 lines (37 loc) · 1.04 KB
/
page-map.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
import path from 'node:path'
import { FileMap, MdxPath, MetaJsonPath, PageMapItem } from './types'
import { META_FILENAME } from './constants'
import { normalizeMeta, parseFileName } from './utils'
import filterRouteLocale from './filter-route-locale'
type PageMapProps = {
filePath: string
pageMap: PageMapItem[]
fileMap: FileMap
defaultLocale: string
}
export function getPageMap({
filePath,
pageMap,
fileMap,
defaultLocale
}: PageMapProps): {
title: string
route: string
pageMap: PageMapItem[]
} {
const { locale } = parseFileName(filePath)
const pageItem = fileMap[filePath as MdxPath]
const metaFilename = locale
? META_FILENAME.replace('.', `.${locale}.`)
: META_FILENAME
const metaDir = path.dirname(filePath)
const metaPath = path.join(metaDir, metaFilename) as MetaJsonPath
const pageMeta = fileMap[metaPath].data[pageItem.name]
return {
pageMap: locale
? filterRouteLocale(pageMap, locale, defaultLocale)
: pageMap,
title: normalizeMeta(pageMeta).title,
route: pageItem.route
}
}