-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
69 lines (61 loc) · 1.44 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
'use strict'
/**
* Normalize head tag config.
*
* @param {string|array} tag
* @returns {object}
*/
exports.normalizeHeadTag = function (tag) {
if (typeof tag === 'string') {
tag = [tag]
}
const tagName = tag[0]
return {
tagName,
attributes: tag[1] || {},
innerHTML: tag[2] || '',
closeTag: !(tagName === 'meta' || tagName === 'link')
}
}
/**
* Use webpack-merge to merge user's config into default config.
*
* @param {object} userConfig
* @param {object} config
* @param {boolean} isServer
* @returns {object}
*/
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
}
/**
* Infer date.
*
* @param {object} frontmatter
* @param {string} filename
* @returns {null|string}
*/
const DATE_RE = /(\d{4}-\d{1,2}(-\d{1,2})?)-(.*)/
exports.DATE_RE = DATE_RE
exports.inferDate = function (frontmatter = {}, filename, dirname) {
let matches
if (frontmatter.date) {
return frontmatter.date
} else if (filename && (matches = filename.match(DATE_RE))) {
return matches[1]
} else if (dirname && (matches = dirname.match(DATE_RE))) {
return matches[1]
} else {
return null
}
}