-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.js
More file actions
31 lines (26 loc) · 768 Bytes
/
util.js
File metadata and controls
31 lines (26 loc) · 768 Bytes
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
const _ = require('lodash')
const extractText = children => {
return children
.map(child => {
if (!_.isEmpty(child.value)) {
return child.value
} else if (child.children && child.children.length > 0) {
return extractText(child.children)
} else {
return ''
}
})
.join(' ')
}
const getDefaultId = children => {
return formatDefaultId(extractText(children))
}
const formatDefaultId = value => {
return _.kebabCase(value.replace(/\\s+/g, ' ').trim())
}
const setNodeId = (node, id) => {
if (!node.data) node.data = {}
if (!node.data.hProperties) node.data.hProperties = {}
node.data.id = node.data.hProperties.id = id
}
module.exports = { extractText, getDefaultId, formatDefaultId, setNodeId }