-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (35 loc) · 999 Bytes
/
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
var sep = require('path').sep
var toString = require('mdast-util-to-string')
module.exports = checkTitle
function checkTitle (opts) {
var title = (opts || {}).title
return function checkTitleTransformer (root, file) {
var filePath = title || (file.dirname === '.' ? file.cwd : file.dirname) || process.cwd();
var dirnames = filePath.split(sep)
var folder = dirnames[dirnames.length - 1]
var children = root.children
var node = children[0]
var replacement = {
type: 'heading',
depth: 1,
children: [
{ type: 'text', value: folder }
]
}
// Replace heading if it is incorrect
// Do nothing if it is fine
if (node && node.type === 'heading') {
var text = toString(node)
.toLowerCase()
.replace(/\s+/g, ' ')
.trim()
if (text !== folder) {
children[0] = replacement
}
// If it doesn't, splice it in
} else {
children.unshift(replacement)
}
return
}
}