-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (58 loc) · 1.71 KB
/
index.js
File metadata and controls
68 lines (58 loc) · 1.71 KB
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
function match(node) {
return (
node &&
node.attributes !== undefined &&
(node.attributes['v-if'] !== undefined ||
node.attributes['v-html'] !== undefined ||
node.attributes['v-model'] !== undefined ||
node.attributes['v-for'] !== undefined)
)
}
function transform({ node, parent }) {
if(!match(node)) return;
const attributes = node.attributes;
const ref = parent ? parent.attributes.ref : attributes.source;
if (ref) {
if (attributes['v-if'] !== undefined && ref[attributes['v-if']] === false) {
node.type = false;
delete node.attributes;
delete node.children;
return;
}
if (attributes['v-html'] !== undefined && ref[attributes['v-html']]) {
node.attributes.html = ref[attributes['v-html']]();
}
if (attributes['v-model'] !== undefined && ref[attributes['v-model']]) {
node.attributes.bind = attributes['v-model'];
if (!attributes.source) {
node.attributes.source = ref;
}
}
}
if (attributes['v-for']) {
const props = attributes['v-for'].split(' in ');
const array = JSON.parse(props[1]);
node.children = array.map(a => {
const children = node.children.join('');
let data = '';
if (typeof a === 'object') {
data = children
.replace(props[0] + '.', '')
.split('.')
.reduce((prev, cur) => prev[cur], a);
}
return {
...node,
children: [data || a]
}
});
node.type = 'span';
}
delete node.attributes['v-if'];
delete node.attributes['v-html'];
delete node.attributes['v-model'];
delete node.attributes['v-for'];
}
function load(_context) {
}
export default { transform, load, client: true, server: true };