/
createPage.ts
152 lines (131 loc) · 3.71 KB
/
createPage.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { App, Page, PageOptions } from '../types/index.js'
import { inferPagePath } from './inferPagePath.js'
import { renderPageContent } from './renderPageContent.js'
import { resolvePageComponentInfo } from './resolvePageComponentInfo.js'
import { resolvePageDataInfo } from './resolvePageDataInfo.js'
import { resolvePageDate } from './resolvePageDate.js'
import { resolvePageFileContent } from './resolvePageFileContent.js'
import { resolvePageFilePath } from './resolvePageFilePath.js'
import { resolvePageHtmlInfo } from './resolvePageHtmlInfo.js'
import { resolvePageKey } from './resolvePageKey.js'
import { resolvePageLang } from './resolvePageLang.js'
import { resolvePagePath } from './resolvePagePath.js'
import { resolvePagePermalink } from './resolvePagePermalink.js'
import { resolvePageRouteMeta } from './resolvePageRouteMeta.js'
import { resolvePageSlug } from './resolvePageSlug.js'
export const createPage = async (
app: App,
options: PageOptions
): Promise<Page> => {
// plugin hook: extendsPageOptions
await app.pluginApi.hooks.extendsPageOptions.process(options, app)
// resolve page file absolute path and relative path
const { filePath, filePathRelative } = resolvePageFilePath({
app,
options,
})
// read the raw file content according to the absolute file path
const content = await resolvePageFileContent({ filePath, options })
// render page content and extract information
const {
contentRendered,
deps,
frontmatter,
headers,
links,
markdownEnv,
sfcBlocks,
title,
} = renderPageContent({
app,
content,
filePath,
filePathRelative,
options,
})
// resolve route meta from frontmatter
const routeMeta = resolvePageRouteMeta({ frontmatter })
// resolve slug from file path
const slug = resolvePageSlug({ filePathRelative })
// resolve date from file path
const date = resolvePageDate({ frontmatter, filePathRelative })
// infer page path according to file path
const { pathInferred, pathLocale } = inferPagePath({ app, filePathRelative })
// resolve language from frontmatter and site options
const lang = resolvePageLang({ app, frontmatter, pathLocale })
// resolve page permalink
const permalink = resolvePagePermalink({
app,
frontmatter,
slug,
date,
pathInferred,
pathLocale,
})
// resolve page path
const path = resolvePagePath({ permalink, pathInferred, options })
// resolve page key
const key = resolvePageKey({ path })
// resolve page rendered html file path
const { htmlFilePath, htmlFilePathRelative } = resolvePageHtmlInfo({
app,
path,
})
// resolve page component and extract headers & links
const {
componentFilePath,
componentFilePathRelative,
componentFileChunkName,
} = await resolvePageComponentInfo({
app,
htmlFilePathRelative,
key,
})
const { dataFilePath, dataFilePathRelative, dataFileChunkName } =
resolvePageDataInfo({ app, htmlFilePathRelative, key })
const page: Page = {
// page data
data: {
key,
path,
title,
lang,
frontmatter,
headers,
},
// base fields
key,
path,
title,
lang,
frontmatter,
headers,
// extra fields
content,
contentRendered,
date,
deps,
links,
markdownEnv,
pathInferred,
pathLocale,
permalink,
routeMeta,
sfcBlocks,
slug,
// file info
filePath,
filePathRelative,
componentFilePath,
componentFilePathRelative,
componentFileChunkName,
dataFilePath,
dataFilePathRelative,
dataFileChunkName,
htmlFilePath,
htmlFilePathRelative,
}
// plugin hook: extendsPage
await app.pluginApi.hooks.extendsPage.process(page, app)
return page
}