-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
100 lines (99 loc) · 3.9 KB
/
index.mjs
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
90
91
92
93
94
95
96
97
98
99
100
const plugin = (options = {}) => (tree) => {
options.tag = options.tag || "outlook";
const attributes = ["only", "not", "lt", "lte", "gt", "gte"];
const versions = {
2e3: 9,
2002: 10,
2003: 11,
2007: 12,
2010: 14,
2013: 15,
2016: 16,
2019: 16
};
const process = (node) => {
if (node.tag === `not-${options.tag}`) {
node.tag = false;
node.content = `<!--[if !mso]><!-->${tree.render(node.content)}<!--<![endif]-->`;
return node;
}
if (node.tag !== options.tag) {
return node;
}
node.tag = false;
if (Array.isArray(node.content)) {
for (const line of node.content) {
if (line.tag && [options.tag, `not-${options.tag}`].includes(line.tag)) {
line.tag = false;
}
}
}
if (!hasAttributes(node.attrs, attributes)) {
node.content = `<!--[if mso]>${tree.render(node.content)}<![endif]-->`;
return node;
}
const foundAttrs = intersectAttributes(node.attrs, attributes);
const firstAttr = foundAttrs[0];
const secondAttr = foundAttrs[1] || false;
if (["only", "not"].includes(firstAttr)) {
const items = node.attrs[firstAttr].split(",");
const negation = firstAttr === "not" ? "!" : "";
if (items.length > 1) {
let conditions = [];
for (const item of items) {
if (versions[item]) {
conditions.push(`(mso ${versions[item]})`);
}
}
conditions = [...new Set(conditions)];
conditions = conditions.length > 1 ? conditions.join("|") : conditions[0].replace(/[()]/g, "");
if (firstAttr === "not") {
node.content = `<!--[if ${negation}(${conditions})]>${tree.render(node.content)}<![endif]-->`;
} else {
node.content = `<!--[if ${conditions}]>${tree.render(node.content)}<![endif]-->`;
}
} else if (items.length === 1 && versions[items[0]]) {
node.content = `<!--[if ${negation}mso ${versions[items[0]]}]>${tree.render(node.content)}<![endif]-->`;
}
return node;
}
if (arraysMatch(foundAttrs, ["gt", "lt"]) || arraysMatch(foundAttrs, ["gt", "lte"]) || arraysMatch(foundAttrs, ["gte", "lte"]) || arraysMatch(foundAttrs, ["gte", "lt"]) || arraysMatch(foundAttrs, ["lt", "gt"]) || arraysMatch(foundAttrs, ["lt", "gte"]) || arraysMatch(foundAttrs, ["lte", "gte"]) || arraysMatch(foundAttrs, ["lte", "gt"])) {
if (versions[node.attrs[firstAttr]] && versions[node.attrs[secondAttr]]) {
node.content = `<!--[if (${firstAttr} mso ${versions[node.attrs[firstAttr]]})&(${secondAttr} mso ${versions[node.attrs[secondAttr]]})]>${tree.render(node.content)}<![endif]-->`;
}
return node;
}
if (firstAttr === "lt" && versions[node.attrs.lt]) {
node.content = `<!--[if lt mso ${versions[node.attrs.lt]}]>${tree.render(node.content)}<![endif]-->`;
return node;
}
if (firstAttr === "lte" && versions[node.attrs.lte]) {
node.content = `<!--[if lte mso ${versions[node.attrs.lte]}]>${tree.render(node.content)}<![endif]-->`;
return node;
}
if (firstAttr === "gt" && versions[node.attrs.gt]) {
node.content = `<!--[if gt mso ${versions[node.attrs.gt]}]>${tree.render(node.content)}<![endif]-->`;
return node;
}
if (firstAttr === "gte" && versions[node.attrs.gte]) {
node.content = `<!--[if gte mso ${versions[node.attrs.gte]}]>${tree.render(node.content)}<![endif]-->`;
return node;
}
return node;
};
return tree.walk(process);
};
const hasAttributes = (o, a) => o ? Object.keys(o).some((r) => a.includes(r)) : false;
const intersectAttributes = (o, a) => o ? Object.keys(o).filter((r) => a.includes(r)) : false;
const arraysMatch = (first, second) => {
if (first.length !== second.length) {
return false;
}
for (const [i, element] of first.entries()) {
if (element !== second[i]) {
return false;
}
}
return true;
};
export { plugin as default };