-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathresolveOptions.js
194 lines (176 loc) Β· 5.4 KB
/
resolveOptions.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require('fs-extra')
const path = require('path')
const globby = require('globby')
const createMarkdown = require('../markdown')
const loadConfig = require('./loadConfig')
const { encodePath, fileToPath, sort, getGitLastUpdatedTimeStamp } = require('./util')
const {
inferTitle,
extractHeaders,
parseFrontmatter
} = require('../util/index')
module.exports = async function resolveOptions (sourceDir) {
const vuepressDir = path.resolve(sourceDir, '.vuepress')
const siteConfig = loadConfig(vuepressDir)
// normalize head tag urls for base
const base = siteConfig.base || '/'
if (base !== '/' && siteConfig.head) {
siteConfig.head.forEach(tag => {
const attrs = tag[1]
if (attrs) {
for (const name in attrs) {
if (name === 'src' || name === 'href') {
const value = attrs[name]
if (value.charAt(0) === '/') {
attrs[name] = base + value.slice(1)
}
}
}
}
})
}
// resolve outDir
const outDir = siteConfig.dest
? path.resolve(siteConfig.dest)
: path.resolve(sourceDir, '.vuepress/dist')
// resolve theme
const useDefaultTheme = (
!siteConfig.theme &&
!fs.existsSync(path.resolve(vuepressDir, 'theme'))
)
const defaultThemePath = path.resolve(__dirname, '../default-theme')
let themePath = null
let themeLayoutPath = null
let themeNotFoundPath = null
let themeEnhanceAppPath = null
if (useDefaultTheme) {
// use default theme
themePath = defaultThemePath
themeLayoutPath = path.resolve(defaultThemePath, 'Layout.vue')
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue')
} else {
// resolve theme Layout
if (siteConfig.theme) {
// use external theme
try {
themeLayoutPath = require.resolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`, {
paths: [
path.resolve(__dirname, '../../node_modules'),
path.resolve(sourceDir)
]
})
themePath = path.dirname(themeLayoutPath)
} catch (e) {
throw new Error(`[vuepress] Failed to load custom theme "${
siteConfig.theme
}". File vuepress-theme-${siteConfig.theme}/Layout.vue does not exist.`)
}
} else {
// use custom theme
themePath = path.resolve(vuepressDir, 'theme')
themeLayoutPath = path.resolve(themePath, 'Layout.vue')
if (!fs.existsSync(themeLayoutPath)) {
throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`)
}
}
// resolve theme NotFound
themeNotFoundPath = path.resolve(themePath, 'NotFound.vue')
if (!fs.existsSync(themeNotFoundPath)) {
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue')
}
// resolve theme enhanceApp
themeEnhanceAppPath = path.resolve(themePath, 'enhanceApp.js')
if (!fs.existsSync(themeEnhanceAppPath)) {
themeEnhanceAppPath = null
}
}
// resolve theme config
const themeConfig = siteConfig.themeConfig || {}
// resolve algolia
const isAlgoliaSearch = (
themeConfig.algolia ||
Object.keys(siteConfig.locales && themeConfig.locales || {})
.some(base => themeConfig.locales[base].algolia)
)
// resolve markdown
const markdown = createMarkdown(siteConfig)
// resolve pageFiles
const patterns = ['**/*.md', '!.vuepress', '!node_modules']
if (siteConfig.dest) {
// #654 exclude dest folder when dest dir was set in
// sourceDir but not in '.vuepress'
const outDirRelative = path.relative(sourceDir, outDir)
if (!outDirRelative.includes('..')) {
patterns.push('!' + outDirRelative)
}
}
const pageFiles = sort(await globby(patterns, { cwd: sourceDir }))
// resolve lastUpdated
const shouldResolveLastUpdated = (
themeConfig.lastUpdated ||
Object.keys(siteConfig.locales && themeConfig.locales || {})
.some(base => themeConfig.locales[base].lastUpdated)
)
// resolve pagesData
const pagesData = await Promise.all(pageFiles.map(async (file) => {
const filepath = path.resolve(sourceDir, file)
const key = 'v-' + Math.random().toString(16).slice(2)
const data = {
key,
path: encodePath(fileToPath(file))
}
if (shouldResolveLastUpdated) {
data.lastUpdated = getGitLastUpdatedTimeStamp(filepath)
}
// extract yaml frontmatter
const content = await fs.readFile(filepath, 'utf-8')
const frontmatter = parseFrontmatter(content)
// infer title
const title = inferTitle(frontmatter)
if (title) {
data.title = title
}
const headers = extractHeaders(
frontmatter.content,
['h2', 'h3'],
markdown
)
if (headers.length) {
data.headers = headers
}
if (Object.keys(frontmatter.data).length) {
data.frontmatter = frontmatter.data
}
if (frontmatter.excerpt) {
const { html } = markdown.render(frontmatter.excerpt)
data.excerpt = html
}
return data
}))
// resolve site data
const siteData = {
title: siteConfig.title || '',
description: siteConfig.description || '',
base,
pages: pagesData,
themeConfig,
locales: siteConfig.locales
}
const options = {
siteConfig,
siteData,
sourceDir,
outDir,
publicPath: base,
pageFiles,
pagesData,
themePath,
themeLayoutPath,
themeNotFoundPath,
themeEnhanceAppPath,
useDefaultTheme,
isAlgoliaSearch,
markdown
}
return options
}