-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
166 lines (153 loc) Β· 4.47 KB
/
index.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
const { path, datatypes: { isString }} = require('@vuepress/shared-utils')
module.exports = (options, ctx) => {
const { layoutComponentMap } = ctx
const {
pageEnhancers = [],
postsDir = '_posts',
categoryIndexPageUrl = '/category/',
tagIndexPageUrl = '/tag/',
permalink = '/:year/:month/:day/:slug'
} = options
const isLayoutExists = name => layoutComponentMap[name] !== undefined
const getLayout = (name, fallback) => isLayoutExists(name) ? name : fallback
const isDirectChild = regularPath => path.parse(regularPath).dir === '/'
const enhancers = [
{
when: ({ regularPath }) => regularPath === categoryIndexPageUrl,
frontmatter: { layout: getLayout('Categories', 'Page') }
},
{
when: ({ regularPath }) => regularPath.startsWith('/category/'),
frontmatter: { layout: getLayout('Category', 'Page') }
},
{
when: ({ regularPath }) => regularPath === tagIndexPageUrl,
frontmatter: { layout: getLayout('Tags', 'Page') }
},
{
when: ({ regularPath }) => regularPath.startsWith('/tag/'),
frontmatter: { layout: getLayout('Tag', 'Page') }
},
{
when: ({ regularPath }) => regularPath === '/',
frontmatter: { layout: getLayout('Layout') }
},
{
when: ({ regularPath }) => regularPath.startsWith(`/${postsDir}/`),
frontmatter: {
layout: getLayout('Post', 'Page'),
permalink: permalink
},
data: { type: 'post' }
},
...pageEnhancers,
{
when: ({ regularPath }) => isDirectChild(regularPath),
frontmatter: { layout: getLayout('Page', 'Layout') },
data: { type: 'page' }
}
]
return {
/**
* Modify page's metadata according to the habits of blog users.
*/
extendPageData (pageCtx) {
const { frontmatter: rawFrontmatter } = pageCtx
enhancers.forEach(({
when,
data = {},
frontmatter = {}
}) => {
if (when(pageCtx)) {
Object.keys(frontmatter).forEach(key => {
if (!rawFrontmatter[key]) {
rawFrontmatter[key] = frontmatter[key]
}
})
Object.assign(pageCtx, data)
}
})
},
/**
* Create tag page and category page.
*/
async ready () {
const { pages } = ctx
const tagMap = {}
const categoryMap = {}
const curryHandler = (scope, map) => (key, pageKey) => {
if (key) {
if (!map[key]) {
map[key] = {}
map[key].path = `/${scope}/${key}.html`
map[key].pageKeys = []
}
map[key].pageKeys.push(pageKey)
}
}
const handleTag = curryHandler('tag', tagMap)
const handleCategory = curryHandler('category', categoryMap)
pages.forEach(({
key,
frontmatter: {
tag,
tags,
category,
categories
}
}) => {
if (isString(tag)) {
handleTag(tag, key)
}
if (Array.isArray(tags)) {
tags.forEach(tag => handleTag(tag, key))
}
if (isString(category)) {
handleCategory(category, key)
}
if (Array.isArray(categories)) {
categories.forEach(category => handleCategory(category, key))
}
})
ctx.tagMap = tagMap
ctx.categoryMap = categoryMap
const extraPages = [
{
permalink: tagIndexPageUrl,
frontmatter: { title: `Tags` }
},
{
permalink: categoryIndexPageUrl,
frontmatter: { title: `Categories` }
},
...Object.keys(tagMap).map(tagName => ({
permalink: tagMap[tagName].path,
meta: { tagName },
frontmatter: { title: `${tagName} | Tag` }
})),
...Object.keys(categoryMap).map(categoryName => ({
permalink: categoryMap[categoryName].path,
meta: { categoryName },
frontmatter: { title: `${categoryName} | Category` }
}))
]
await Promise.all(extraPages.map(page => ctx.addPage(page)))
},
/**
* Generate tag and category metadata.
*/
async clientDynamicModules () {
return [
{
name: 'tag.js',
content: `export default ${JSON.stringify(ctx.tagMap, null, 2)}`
},
{
name: 'category.js',
content: `export default ${JSON.stringify(ctx.categoryMap, null, 2)}`
}
]
},
enhanceAppFiles: path.resolve(__dirname, 'enhanceAppFile.js')
}
}