-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.js
34 lines (30 loc) · 1.13 KB
/
html.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
export default function html(literalSections, ...substs) {
// Use raw literal sections: we don’t want
// backslashes (\n etc.) to be interpreted
let raw = literalSections.split("\\");
let result = '';
substs.forEach((subst, i) => {
// // Retrieve the literal section preceding
// // the current substitution
let lit = raw[i];
// // In the example, map() returns an array:
// // If substitution is an array (and not a string),
// // we turn it into a string
if (Array.isArray(subst)) {
subst = subst.join('');
}
// // If the substitution is preceded by a dollar sign,
// // we escape special characters in it
if (lit.endsWith('$')) {
subst = $tmpl.html_escape(subst);
lit = lit.slice(0, -1);
}
result += lit;
result += subst;
});
// Take care of last literal section
// (Never fails, because an empty template string
// produces one literal section, an empty string)
result += raw[raw.length-1]; // (A)
return result;
} ;