Skip to content

Commit 2e48972

Browse files
committed
refactor: unify code style.
1 parent 01fa9af commit 2e48972

File tree

17 files changed

+266
-32
lines changed

17 files changed

+266
-32
lines changed

packages/@vuepress/core/lib/build.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
module.exports = async function build (sourceDir, cliOptions = {}) {
24
process.env.NODE_ENV = 'production'
35

packages/@vuepress/core/lib/dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
module.exports = async function dev (sourceDir, cliOptions = {}) {
24
const path = require('path')
35
const webpack = require('webpack')

packages/@vuepress/core/lib/eject.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const path = require('path')
24
const { chalk, fs, logger } = require('@vuepress/shared-utils')
35

packages/@vuepress/core/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
exports.dev = require('./dev')
24
exports.build = require('./build')
35
exports.eject = require('./eject')

packages/@vuepress/core/lib/prepare/AppContext.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const path = require('path')
28
const createMarkdown = require('../markdown/index')
39
const loadConfig = require('./loadConfig')
@@ -8,17 +14,23 @@ const Page = require('./Page')
814
const I18n = require('./I18n')
915
const PluginAPI = require('../plugin-api/index')
1016

17+
/**
18+
* Expose AppContext.
19+
*/
20+
1121
module.exports = class AppContext {
1222
/**
1323
* Instantiate the app context with a new API
14-
* @param { string } sourceDir
24+
*
25+
* @param {string} sourceDir
1526
* @param {{
1627
* isProd: boolean,
1728
* plugins: pluginsConfig,
1829
* theme: themeNameConfig
1930
* temp: string
2031
* }} options
2132
*/
33+
2234
constructor (sourceDir, cliOptions = {}, isProd) {
2335
this.sourceDir = sourceDir
2436
this.cliOptions = cliOptions
@@ -47,8 +59,11 @@ module.exports = class AppContext {
4759

4860
/**
4961
* Load pages, load plugins, apply plugins / plugin options, etc.
62+
*
5063
* @returns {Promise<void>}
64+
* @api private
5165
*/
66+
5267
async process () {
5368
this.normalizeHeadTagUrls()
5469
this.markdown = createMarkdown(this.siteConfig)
@@ -72,7 +87,10 @@ module.exports = class AppContext {
7287

7388
/**
7489
* Apply internal and user plugins
90+
*
91+
* @api private
7592
*/
93+
7694
resolvePlugins () {
7795
const themeConfig = this.themeConfig
7896
const siteConfig = this.siteConfig
@@ -111,7 +129,10 @@ module.exports = class AppContext {
111129

112130
/**
113131
* normalize head tag urls for base
132+
*
133+
* @api private
114134
*/
135+
115136
normalizeHeadTagUrls () {
116137
if (this.base !== '/' && this.siteConfig.head) {
117138
this.siteConfig.head.forEach(tag => {
@@ -132,7 +153,10 @@ module.exports = class AppContext {
132153

133154
/**
134155
* Make template configurable
156+
*
157+
* @api private
135158
*/
159+
136160
resolveTemplates () {
137161
let { ssrTemplate, devTemplate } = this.siteConfig
138162
const templateDir = path.resolve(this.vuepressDir, 'templates')
@@ -156,8 +180,11 @@ module.exports = class AppContext {
156180

157181
/**
158182
* Find all page source files located in sourceDir
183+
*
159184
* @returns {Promise<void>}
185+
* @api private
160186
*/
187+
161188
async resolvePages () {
162189
// resolve pageFiles
163190
const patterns = ['**/*.md', '!.vuepress', '!node_modules']
@@ -179,8 +206,11 @@ module.exports = class AppContext {
179206

180207
/**
181208
* Add a page
182-
* @returns { Promise<void> }
209+
*
210+
* @returns {Promise<void>}
211+
* @api public
183212
*/
213+
184214
async addPage (options) {
185215
options.permalinkPattern = this.siteConfig.permalink
186216
const page = new Page(options)
@@ -194,15 +224,19 @@ module.exports = class AppContext {
194224

195225
/**
196226
* Resolve theme
197-
* @returns { Promise<void> }
227+
*
228+
* @returns {Promise<void>}
229+
* @api private
198230
*/
231+
199232
async resolveTheme () {
200233
const theme = this.siteConfig.theme || this.cliOptions.theme
201234
Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir)))
202235
}
203236

204237
/**
205238
* Get the data to be delivered to the client.
239+
*
206240
* @returns {{
207241
* title: string,
208242
* description: string,
@@ -211,7 +245,9 @@ module.exports = class AppContext {
211245
* themeConfig: ThemeConfig,
212246
* locales: Locales
213247
* }}
248+
* @api public
214249
*/
250+
215251
getSiteData () {
216252
return {
217253
title: this.siteConfig.title || '',
@@ -233,6 +269,7 @@ module.exports = class AppContext {
233269
* tempPath: string
234270
* }}
235271
*/
272+
236273
function createTemp (tempPath) {
237274
if (!tempPath) {
238275
tempPath = path.resolve(__dirname, '../../.temp')

packages/@vuepress/core/lib/prepare/I18n.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
'use strict'
2+
3+
/**
4+
* Get page data via path (permalink).
5+
*
6+
* @param {array} pages
7+
* @param {string} path
8+
* @returns {object}
9+
*/
10+
111
function findPageForPath (pages, path) {
212
for (let i = 0; i < pages.length; i++) {
313
const page = pages[i]
@@ -11,6 +21,14 @@ function findPageForPath (pages, path) {
1121
}
1222
}
1323

24+
/**
25+
* Expose I18n constructor.Note that this file will
26+
* be run in both server and client side.
27+
*
28+
* @param store
29+
* @returns {{new(*): I18n, prototype: I18n}}
30+
*/
31+
1432
module.exports = (store /* null in server side */) => class I18n {
1533
constructor (dataProvider) {
1634
this.__ssrContext = true

packages/@vuepress/core/lib/prepare/Page.js

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const path = require('path')
28
const slugify = require('../markdown/slugify')
39
const { inferTitle, extractHeaders } = require('../util/index')
410
const { fs, fileToPath, parseFrontmatter, getPermalink } = require('@vuepress/shared-utils')
511

12+
/**
13+
* Expose Page class.
14+
*/
15+
616
module.exports = class Page {
717
/**
8-
* @param { string } title markdown title
9-
* @param { string } content markdown file content
10-
* @param { string } filePath absolute file path of source markdown file.
11-
* @param { string } relative relative file path of source markdown file.
12-
* @param { string } permalink the URL (excluding the domain name) for your pages, posts.
13-
* @param { object } frontmatter
14-
* @param { string } permalinkPattern
18+
* @param {string} path the URL (excluding the domain name) for your page/post.
19+
* @param {string} title markdown title
20+
* @param {string} content markdown file content
21+
* @param {string} filePath absolute file path of source markdown file.
22+
* @param {string} relative relative file path of source markdown file.
23+
* @param {string} permalink same to path, the URL (excluding the domain name) for your page/post.
24+
* @param {object} frontmatter
25+
* @param {string} permalinkPattern
1526
*/
1627
constructor ({
1728
path,
@@ -44,6 +55,16 @@ module.exports = class Page {
4455
this._permalinkPattern = permalinkPattern
4556
}
4657

58+
/**
59+
* Process a page.
60+
*
61+
* 1. If it's a page pointing to a md file, this method will try
62+
* to resolve the page's title / headers from the content.
63+
* 2. If it's a pure route. this method will only enhance it.
64+
*
65+
* @api public
66+
*/
67+
4768
async process ({
4869
i18n,
4970
markdown,
@@ -85,10 +106,57 @@ module.exports = class Page {
85106
this._i18n = i18n
86107
this._localePath = i18n.$localePath
87108

88-
this._enhance(enhancers)
109+
this.enhance(enhancers)
89110
this.buildPermalink()
90111
}
91112

113+
/**
114+
* file name of page's source markdown file, or the last cut of regularPath.
115+
*
116+
* @returns {string}
117+
* @api public
118+
*/
119+
120+
get filename () {
121+
return path.parse(this._filePath || this.regularPath).name
122+
}
123+
124+
/**
125+
* slugified file name.
126+
*
127+
* @returns {string}
128+
* @api public
129+
*/
130+
131+
get slug () {
132+
return slugify(this.filename)
133+
}
134+
135+
/**
136+
* Convert page's metadata to JSON, note that all fields beginning
137+
* with an underscore will not be serialized.
138+
*
139+
* @returns {object}
140+
* @api public
141+
*/
142+
143+
toJson () {
144+
const json = {}
145+
Object.keys(this).reduce((json, key) => {
146+
if (!key.startsWith('_')) {
147+
json[key] = this[key]
148+
}
149+
return json
150+
}, json)
151+
return json
152+
}
153+
154+
/**
155+
* Build permalink via permalink pattern and page's metadata.
156+
*
157+
* @api private
158+
*/
159+
92160
buildPermalink () {
93161
if (!this._permalink) {
94162
this._permalink = getPermalink({
@@ -105,7 +173,16 @@ module.exports = class Page {
105173
}
106174
}
107175

108-
_enhance (enhancers) {
176+
/**
177+
* Execute the page enhancers. A enhancer could do following things:
178+
*
179+
* 1. Modify page's frontmetter.
180+
* 2. Add extra field to the page.
181+
*
182+
* @api private
183+
*/
184+
185+
enhance (enhancers) {
109186
for (const { name: pluginName, value: enhancer } of enhancers) {
110187
try {
111188
enhancer(this)
@@ -115,23 +192,4 @@ module.exports = class Page {
115192
}
116193
}
117194
}
118-
119-
get filename () {
120-
return path.parse(this._filePath || this.regularPath).name
121-
}
122-
123-
get slug () {
124-
return slugify(this.filename)
125-
}
126-
127-
toJson () {
128-
const json = {}
129-
Object.keys(this).reduce((json, key) => {
130-
if (!key.startsWith('_')) {
131-
json[key] = this[key]
132-
}
133-
return json
134-
}, json)
135-
return json
136-
}
137195
}

packages/@vuepress/core/lib/prepare/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const AppContext = require('./AppContext')
28

9+
/**
10+
* Expose prepare.
11+
*/
12+
313
module.exports = async function prepare (sourceDir, cliOptions, isProd) {
414
const appContext = new AppContext(sourceDir, cliOptions, isProd)
515
await appContext.process()

packages/@vuepress/core/lib/prepare/loadConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const { fs } = require('@vuepress/shared-utils')
28
const path = require('path')
39
const yamlParser = require('js-yaml')
410
const tomlParser = require('toml')
511

12+
/**
13+
* Expose loadConfig.
14+
*/
15+
616
module.exports = function loadConfig (vuepressDir, bustCache = true) {
717
const configPath = path.resolve(vuepressDir, 'config.js')
818
const configYmlPath = path.resolve(vuepressDir, 'config.yml')

0 commit comments

Comments
 (0)