-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (78 loc) · 2.71 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as cheerio from 'cheerio';
import _ from 'lodash';
function compile(html, data) {
const $ = cheerio.load(html);
$('[x-for]').each(function () {
const key = $(this).attr('x-for').split(' in ')[1].slice(5); // Remove 'data.' from the start
const items = _.get(data, key);
const template = $(this).html();
let result = '';
items.forEach((item) => {
let itemHtml = cheerio.load(template);
for (const prop in item) {
const regex = new RegExp(`item.${prop}`, 'g');
itemHtml('[x-html]').each(function () {
if ($(this).attr('x-html') === `item.${prop}`) {
$(this).html(item[prop]);
}
});
itemHtml('[x-text]').each(function () {
if ($(this).attr('x-text') === `item.${prop}`) {
$(this).text(item[prop]);
}
});
itemHtml('[:src]').each(function () {
if ($(this).attr(':src') === `item.${prop}`) {
$(this).attr('src', item[prop]);
}
});
itemHtml('[:id]').each(function () {
if ($(this).attr(':id') === `item.${prop}`) {
$(this).attr('id', item[prop]);
}
});
}
result += itemHtml.html();
});
$(this).replaceWith(result);
});
$('[x-html]').each(function () {
const key = $(this).attr('x-html').slice(5); // Remove 'data.' from the start
const value = _.get(data, key);
if (value) {
$(this).html(value);
}
});
$('[x-text]').each(function () {
const key = $(this).attr('x-text').slice(5); // Remove 'data.' from the start
const value = _.get(data, key);
if (value) {
$(this).text(value);
}
});
$('[:src]').each(function () {
const key = $(this).attr(':src').slice(5); // Remove 'data.' from the start
const value = _.get(data, key);
if (value) {
$(this).attr('src', value);
}
});
$('[:id]').each(function () {
const key = $(this).attr(':id').slice(5); // Remove 'data.' from the start
const value = _.get(data, key);
if (value) {
$(this).attr('id', value);
}
});
/*
$('[x-if]').each(function() {
const condition = $(this).attr('x-if').slice(5); // Remove 'data.' from the start
const value = _.get(data, condition);
if (!value) {
$(this).remove();
}
});
*/
return $.html();
}
export { compile };