1
+ 'use strict'
2
+
3
+ /**
4
+ * Module dependencies.
5
+ */
6
+
1
7
const path = require ( 'path' )
2
8
const createMarkdown = require ( '../markdown/index' )
3
9
const loadConfig = require ( './loadConfig' )
@@ -8,17 +14,23 @@ const Page = require('./Page')
8
14
const I18n = require ( './I18n' )
9
15
const PluginAPI = require ( '../plugin-api/index' )
10
16
17
+ /**
18
+ * Expose AppContext.
19
+ */
20
+
11
21
module . exports = class AppContext {
12
22
/**
13
23
* Instantiate the app context with a new API
14
- * @param { string } sourceDir
24
+ *
25
+ * @param {string } sourceDir
15
26
* @param {{
16
27
* isProd: boolean,
17
28
* plugins: pluginsConfig,
18
29
* theme: themeNameConfig
19
30
* temp: string
20
31
* }} options
21
32
*/
33
+
22
34
constructor ( sourceDir , cliOptions = { } , isProd ) {
23
35
this . sourceDir = sourceDir
24
36
this . cliOptions = cliOptions
@@ -47,8 +59,11 @@ module.exports = class AppContext {
47
59
48
60
/**
49
61
* Load pages, load plugins, apply plugins / plugin options, etc.
62
+ *
50
63
* @returns {Promise<void> }
64
+ * @api private
51
65
*/
66
+
52
67
async process ( ) {
53
68
this . normalizeHeadTagUrls ( )
54
69
this . markdown = createMarkdown ( this . siteConfig )
@@ -72,7 +87,10 @@ module.exports = class AppContext {
72
87
73
88
/**
74
89
* Apply internal and user plugins
90
+ *
91
+ * @api private
75
92
*/
93
+
76
94
resolvePlugins ( ) {
77
95
const themeConfig = this . themeConfig
78
96
const siteConfig = this . siteConfig
@@ -111,7 +129,10 @@ module.exports = class AppContext {
111
129
112
130
/**
113
131
* normalize head tag urls for base
132
+ *
133
+ * @api private
114
134
*/
135
+
115
136
normalizeHeadTagUrls ( ) {
116
137
if ( this . base !== '/' && this . siteConfig . head ) {
117
138
this . siteConfig . head . forEach ( tag => {
@@ -132,7 +153,10 @@ module.exports = class AppContext {
132
153
133
154
/**
134
155
* Make template configurable
156
+ *
157
+ * @api private
135
158
*/
159
+
136
160
resolveTemplates ( ) {
137
161
let { ssrTemplate, devTemplate } = this . siteConfig
138
162
const templateDir = path . resolve ( this . vuepressDir , 'templates' )
@@ -156,8 +180,11 @@ module.exports = class AppContext {
156
180
157
181
/**
158
182
* Find all page source files located in sourceDir
183
+ *
159
184
* @returns {Promise<void> }
185
+ * @api private
160
186
*/
187
+
161
188
async resolvePages ( ) {
162
189
// resolve pageFiles
163
190
const patterns = [ '**/*.md' , '!.vuepress' , '!node_modules' ]
@@ -179,8 +206,11 @@ module.exports = class AppContext {
179
206
180
207
/**
181
208
* Add a page
182
- * @returns { Promise<void> }
209
+ *
210
+ * @returns {Promise<void> }
211
+ * @api public
183
212
*/
213
+
184
214
async addPage ( options ) {
185
215
options . permalinkPattern = this . siteConfig . permalink
186
216
const page = new Page ( options )
@@ -194,15 +224,19 @@ module.exports = class AppContext {
194
224
195
225
/**
196
226
* Resolve theme
197
- * @returns { Promise<void> }
227
+ *
228
+ * @returns {Promise<void> }
229
+ * @api private
198
230
*/
231
+
199
232
async resolveTheme ( ) {
200
233
const theme = this . siteConfig . theme || this . cliOptions . theme
201
234
Object . assign ( this , ( await loadTheme ( theme , this . sourceDir , this . vuepressDir ) ) )
202
235
}
203
236
204
237
/**
205
238
* Get the data to be delivered to the client.
239
+ *
206
240
* @returns {{
207
241
* title: string,
208
242
* description: string,
@@ -211,7 +245,9 @@ module.exports = class AppContext {
211
245
* themeConfig: ThemeConfig,
212
246
* locales: Locales
213
247
* }}
248
+ * @api public
214
249
*/
250
+
215
251
getSiteData ( ) {
216
252
return {
217
253
title : this . siteConfig . title || '' ,
@@ -233,6 +269,7 @@ module.exports = class AppContext {
233
269
* tempPath: string
234
270
* }}
235
271
*/
272
+
236
273
function createTemp ( tempPath ) {
237
274
if ( ! tempPath ) {
238
275
tempPath = path . resolve ( __dirname , '../../.temp' )
0 commit comments