-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (136 loc) · 3.85 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const parseAttributes = (tag, theme) => {
const keyPattern = /\w+=/g,
valuePattern = /(["{][^"}]*["}])/g,
keyValPair = /\w+=["{][^"}]+["}]/g,
keyValPairs = tag.match(keyValPair) || null,
keys = [],
values = [];
if (keyValPairs) {
keyValPairs.forEach((pair) => {
keys.push(pair.match(keyPattern));
values.push(pair.match(valuePattern));
tag = tag.replace(pair, "");
});
}
let boolAttrs = [];
if (tag.trim()) {
boolAttrs = tag
.trim()
.split(" ")
.filter((val) => {
return val !== "";
});
}
const spanOpen = (val, theme) => {
let color;
if (val) {
if (val.includes('"')) color = theme.str;
else if (val.includes("{")) color = theme.var;
} else color = theme.attr;
return `<span style="color: ${color}">`;
};
let result = "";
if (keyValPairs) {
keyValPairs.forEach((pair, ind) => {
const key = keys[ind][0];
const val = values[ind][0];
result += `${key.trim()}${spanOpen(val, theme)}${val}</span> `;
});
}
if (boolAttrs) {
boolAttrs.forEach((attr, ind) => {
result += `${attr.trim()}${spanOpen("", theme)}</span>`;
if (ind !== boolAttrs.length - 1) result += " ";
});
}
return result.trim();
};
const parseTag = (tag, name, index, last, theme) => {
let tagName;
let attrNames;
const noAttrsPattern = /(<\w*>)/g;
if (name === "open") {
const noAttrs = noAttrsPattern.test(tag);
tagName = tag.match(/<(\w+)(\.\w+)?/g)[0].replace("<", "");
tag = tag.replace(`<${tagName}`, "").replace(">", "");
if (!noAttrs) {
attrNames = parseAttributes(tag, theme);
} else {
attrNames = [""];
}
} else {
tagName = tag.match(/<(\/\w+)(\.\w+)?/g)[0].replace("<", "");
}
const tSpanOpen = `<span style="color: ${theme.tag}">`,
aSpanOpen = `<span style="color: ${theme.attr}">`,
spanClose = `</span>`,
oSpanClose = `</span>>`,
spanCloseNL = `</span>></br>`;
let result = "";
if (name === "open") {
if (attrNames && attrNames[0] !== "") {
result += `<${tSpanOpen}${tagName}${spanClose} ${aSpanOpen}${attrNames}${oSpanClose}`;
} else {
result += `<${tSpanOpen}${tagName}${spanClose}>`;
}
return result;
} else if (name === "close" && index !== last) {
return `<` + tSpanOpen + tagName + spanCloseNL;
} else {
result += `<${tSpanOpen}${tagName}${spanClose}>`;
return result;
}
};
const colorCode = (str, theme, lang, asReactComponent) => {
const parse = asReactComponent ? require("html-react-parser") : null;
if (lang !== "html") return str;
let result = "";
const lines = str.split("\n");
for (let line of lines) {
const indents = line.match(/(~>)/g);
const index = lines.indexOf(line);
const openingTag = line.match(/(<[^>/]+>)/g) || null;
const closingTag = line.match(/(<\/[^>]+>)/g) || null;
const tagText = line
.replace(/(~>)+/, "")
.replace(openingTag, "")
.replace(closingTag, "")
.trim();
if (indents) {
for (let indent of indents) {
result += indent.replace("~>", " ");
}
}
if (openingTag && !closingTag) {
result += `${parseTag(
openingTag[0],
"open",
index,
lines.length - 1,
theme
)}\n`;
} else if (openingTag && closingTag) {
result += parseTag(openingTag[0], "open", index, lines.length - 1, theme);
result += tagText;
result += parseTag(
closingTag[0],
"close",
index,
lines.length - 1,
theme
);
} else if (!openingTag && closingTag) {
result += tagText;
closingTag &&
(result += parseTag(
closingTag[0],
"close",
index,
lines.length - 1,
theme
));
}
}
return asReactComponent ? parse(result, false) : result;
};
module.exports = { colorCode };