-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
367 lines (336 loc) · 10.9 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const path = require('path')
const setFrontmatter = require('./node_utils/setFrontmatter')
const getSidebarData = require('./node_utils/getSidebarData')
const { createPage, deletePage } = require('./node_utils/handlePage')
const chalk = require('chalk') // 命令行打印美化
const yaml = require('js-yaml') // yaml转js
const log = console.log
// md容器名
const CARD_LIST = 'cardList'
const CARD_IMG_LIST = 'cardImgList'
// siteConfig base 配置
let base = ''
// Theme API.
module.exports = (options, ctx) => {
const { sourceDir, themeConfig, siteConfig } = ctx
// base路径
base = siteConfig.base || ''
// 自动设置front matter
setFrontmatter(sourceDir, themeConfig)
// 自动生成结构化侧边栏
const sidebar = themeConfig.sidebar
if (
sidebar === 'structuring' ||
(sidebar && sidebar.mode === 'structuring')
) {
const collapsable =
themeConfig.sidebar.collapsable === false ? false : true
const sidebarData = getSidebarData(sourceDir, collapsable)
if (sidebarData) {
themeConfig.sidebar = sidebarData
log(
chalk.blue('tip ') +
chalk.green('add sidebar data. 成功生成侧边栏数据。')
)
} else {
themeConfig.sidebar = 'auto'
log(
chalk.yellow(
'warning: fail to add sidebar data, switch to "auto". 未能添加侧边栏数据,将切换为“auto”。'
)
)
}
}
// 分类页
if (themeConfig.category !== false) {
createPage(sourceDir, 'categoriesPage')
} else {
deletePage(sourceDir, 'categoriesPage')
}
// 标签页
if (themeConfig.tag !== false) {
createPage(sourceDir, 'tagsPage')
} else {
deletePage(sourceDir, 'tagsPage')
}
// 归档页
if (themeConfig.archive !== false) {
createPage(sourceDir, 'archivesPage')
} else {
deletePage(sourceDir, 'archivesPage')
}
// resolve algolia
const isAlgoliaSearch =
themeConfig.algolia ||
Object.keys((siteConfig.locales && themeConfig.locales) || {}).some(
(base) => themeConfig.locales[base].algolia
)
const enableSmoothScroll = themeConfig.smoothScroll === true
return {
alias() {
return {
'@AlgoliaSearchBox': isAlgoliaSearch
? path.resolve(__dirname, 'components/AlgoliaSearchBox.vue')
: path.resolve(__dirname, 'noopModule.js')
}
},
plugins: [
['@vuepress/active-header-links', options.activeHeaderLinks],
'@vuepress/search',
'@vuepress/plugin-nprogress',
['smooth-scroll', enableSmoothScroll],
[
'container',
{
type: 'note',
defaultTitle: {
'/': '笔记',
'/en/': 'NOTE'
}
}
],
[
'container',
{
type: 'tip',
defaultTitle: {
'/': '提示',
'/en/': 'TIP'
}
}
],
[
'container',
{
type: 'warning',
defaultTitle: {
'/': '注意',
'/en/': 'WARNING'
}
}
],
[
'container',
{
type: 'danger',
defaultTitle: {
'/': '警告',
'/en/': 'WARNING'
}
}
],
[
'container',
{
type: 'right',
defaultTitle: ''
}
],
[
'container',
{
type: 'theorem',
before: (info) =>
`<div class="custom-block theorem"><p class="title">${info}</p>`,
after: '</div>'
}
],
[
'container',
{
type: 'details',
before: (info) =>
`<details class="custom-block details">${
info ? `<summary>${info}</summary>` : ''
}\n`,
after: () => '</details>\n',
defaultTitle: {
'/': '点击查看',
'/en/': 'DETAILS'
}
}
],
// 内容居中容器
[
'container',
{
type: 'center',
before: (info) => `<div class="center-container">`,
after: () => '</div>'
}
],
// 卡片列表
[
'container',
{
type: CARD_LIST,
render: (tokens, idx) => {
// tokens 是整个md文件的虚拟dom结构数组
// idx 是tokens中':::' 所在的索引,而且是当前指定type的':::',分别有开始和结束两次的idx
// if (tokens[idx].nesting === 1) { // 开头的 ':::' 标记
// } else { // 结束的 ':::' 标记
// }
// 注意:修改这里面的代码后需要在md文件保存一下才会重新执行渲染
return renderCardList(tokens, idx, CARD_LIST)
}
}
],
// 图文卡片列表
[
'container',
{
type: CARD_IMG_LIST,
render: (tokens, idx) => {
return renderCardList(tokens, idx, CARD_IMG_LIST)
}
}
]
]
}
}
// 渲染md容器的卡片列表
function renderCardList(tokens, idx, type) {
const END_TYPE = `container_${type}_close`,
_tokens$idx = tokens[idx],
nesting = _tokens$idx.nesting,
info = _tokens$idx.info
if (nesting === 1) {
// 渲染开头的 ':::' 标记
let yamlStr = ''
for (let i = idx; i < tokens.length; i++) {
let _tokens$i = tokens[i],
type = _tokens$i.type,
content = _tokens$i.content,
_info = _tokens$i.info
if (type === END_TYPE) break // 遇到结束的 ':::' 时
if (!content) continue
if (type === 'fence' && _info === 'yaml') {
// 是代码块类型,并且是yaml代码
yamlStr = content
}
}
if (yamlStr) {
// 正确解析出yaml字符串后
const dataObj = yaml.safeLoad(yamlStr) // 将yaml字符串解析成js对象
let dataList = []
let config = {}
if (dataObj) {
// 正确解析出数据对象
if (Array.isArray(dataObj)) {
dataList = dataObj
} else {
config = dataObj.config
dataList = dataObj.data
}
}
if (dataList && dataList.length) {
// 有列表数据
// 每行显示几个
let row = Number(info.split(' ').pop())
if (!row || row > 4 || row < 1) {
row = 3 // 默认 3
}
let listDOM = ''
if (type === CARD_LIST) {
// 普通卡片列表
listDOM = getCardListDOM(dataList, row, config)
} else if (type === CARD_IMG_LIST) {
// 卡片图片列表
listDOM = getCardImgListDOM(dataList, row, config)
}
return `<div class="${type}Container"><div class="card-list">${listDOM}</div>`
}
}
} else {
// 渲染':::' 结尾
return '</div>'
}
}
// 将数据解析成DOM结构 - 普通卡片列表
function getCardListDOM(dataList, row, config) {
const { target = '_blank' } = config
let listDOM = ''
dataList.forEach((item) => {
listDOM += `
<${
item.link
? 'a href="' + withBase(item.link) + '" target="' + target + '"'
: 'span'
} class="card-item ${row ? 'row-' + row : ''}"
style="${
item.bgColor
? 'background-color:' +
item.bgColor +
';--randomColor:' +
item.bgColor +
';'
: '--randomColor: var(--bodyBg);'
}${item.textColor ? 'color:' + item.textColor + ';' : ''}"
>
${
item.avatar
? '<img src="' + withBase(item.avatar) + '" class="no-zoom">'
: ''
}
<div>
<p class="name">${item.name}</p>
<p class="desc">${item.desc}</p>
</div>
</${item.link ? 'a' : 'span'}>
`
})
return listDOM
}
// 将数据解析成DOM结构 - 图文卡片列表
function getCardImgListDOM(dataList, row, config) {
const {
imgHeight = 'auto',
objectFit = 'cover',
lineClamp = 1,
target = '_blank'
} = config
let listDOM = ''
dataList.forEach((item) => {
listDOM += `
<div class="card-item ${row ? 'row-' + row : ''}" >
<a href="${withBase(item.link)}" target="${target}">
<div class="box-img" style="height: ${imgHeight}">
<img src="${withBase(
item.img
)}" class="no-zoom" style="object-fit: ${objectFit}">
</div>
<div class="box-info">
<p class="name">${item.name}</p>
${
item.desc
? `<p class="desc" style="-webkit-line-clamp: ${lineClamp}">${item.desc}</p>`
: ''
}
</div>
${
item.avatar || item.author
? `<div class="box-footer">
${
item.avatar
? `<img src="${withBase(item.avatar)}" class="no-zoom">`
: ''
}
${item.author ? `<span>${item.author}</span>` : ''}
</div>`
: ''
}
</a>
</div>
`
})
return listDOM
}
// 添加base路径
function withBase(path) {
if (!path) return ''
if (base && path.charAt(0) === '/') {
return base + path.slice(1)
} else {
return path
}
}