-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (37 loc) · 967 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
41
42
43
44
45
let config = {};
let configLength = 0;
function match(node) {
return (
node &&
node.attributes !== undefined &&
(Object.values(config.types).filter(type => (
node.type === type
)).length > 0 ||
Object.values(config.attributes).filter(attr => (
node.attributes[attr] !== undefined
)).length > 0)
)
}
function transform({ node }) {
if(configLength === 0 || !match(node)) return;
if (Object.values(config.types).includes(node.type)) {
const type = Object.keys(config.types).find(key => (
config.types[key] === node.type
));
node.type = type;
}
const attributes = node.attributes;
for (let attr in config.attributes) {
node.attributes[attr] = attributes[config.attributes[attr]];
}
}
function configPlugin(userConfig) {
config = userConfig || {};
configLength = Object.keys(config).length;
return {
transform,
client: true,
server: true
}
}
export default configPlugin;