forked from vuejs/vuepress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (111 loc) · 3.03 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
const parseEmojis = str => {
const emojiData = require('markdown-it-emoji/lib/data/full.json')
return str.replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder)
}
exports.normalizeHeadTag = tag => {
if (typeof tag === 'string') {
tag = [tag]
}
const tagName = tag[0]
return {
tagName,
attributes: tag[1] || {},
innerHTML: tag[2] || '',
closeTag: !(tagName === 'meta' || tagName === 'link')
}
}
exports.applyUserWebpackConfig = function (userConfig, config, isServer) {
const merge = require('webpack-merge')
if (typeof userConfig === 'object') {
return merge(config, userConfig)
}
if (typeof userConfig === 'function') {
const res = userConfig(config, isServer)
if (res && typeof res === 'object') {
return merge(config, res)
}
}
return config
}
exports.inferTitle = function (frontmatter) {
if (frontmatter.data.home) {
return 'Home'
}
if (frontmatter.data.title) {
return parseEmojis(frontmatter.data.title)
}
const match = frontmatter.content.trim().match(/^#+\s+(.*)/)
if (match) {
return parseEmojis(match[1])
}
}
exports.parseFrontmatter = content => {
const matter = require('gray-matter')
const toml = require('toml')
return matter(content, {
excerpt_separator: '<!-- more -->',
engines: {
toml: toml.parse.bind(toml),
excerpt: false
}
})
}
const LRU = require('lru-cache')
const cache = LRU({ max: 1000 })
exports.extractHeaders = (content, include = [], md) => {
const key = content + include.join(',')
const hit = cache.get(key)
if (hit) {
return hit
}
const tokens = md.parse(content, {})
const res = []
tokens.forEach((t, i) => {
if (t.type === 'heading_open' && include.includes(t.tag)) {
const title = parseEmojis(tokens[i + 1].content)
const slug = t.attrs.find(([name]) => name === 'id')[1]
res.push({
level: parseInt(t.tag.slice(1), 10),
title,
slug: slug || md.slugify(title)
})
}
})
cache.set(key, res)
return res
}
exports.loadPlugin = (pluginPath, sourceDir) => {
const fs = require('fs-extra')
const path = require('path')
const vuepressDir = path.resolve(sourceDir, '.vuepress')
let plugin
try {
if (fs.existsSync(path.resolve(vuepressDir, pluginPath))) {
plugin = require(path.resolve(vuepressDir, pluginPath))
} else {
plugin = require(pluginPath)
}
} catch (e) {
throw e
}
return plugin
}
exports.cloneOptions = (options) => {
const optionClone = {}
const readonlyProperties = ['sourceDir', 'outDir', 'publicPath', 'markdown', 'themePath', 'useDefaultTheme', 'notFoundPath']
const writableProperties = ['siteConfig', 'pageFiles', 'siteData']
readonlyProperties.forEach(prop => {
Object.defineProperty(optionClone, prop, {
value: options[prop],
enumerable: true
})
})
writableProperties.forEach(prop => {
Object.defineProperty(optionClone, prop, {
value: options[prop],
writable: true,
enumerable: true
})
})
return optionClone
}