-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (23 loc) · 707 Bytes
/
index.js
File metadata and controls
26 lines (23 loc) · 707 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
var has = require('hast-util-has-property')
var url = require('url')
var opt = require('stdopt')
var visit = require('unist-util-visit')
module.exports = function transform (options) {
if (typeof options === 'function') {
options = { transform: options }
}
options.transform = options.transform || function () {}
return function transformer (tree) {
visit(tree, 'element', function (node) {
modify(node, 'href')
modify(node, 'src')
})
}
function modify (node, prop) {
if (has(node, prop)) {
var obj = url.parse(node.properties[prop])
var res = opt(options.transform(obj, node)).or(obj).value()
node.properties[prop] = url.format(res)
}
}
}