-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
34 lines (33 loc) · 1.08 KB
/
utils.mjs
File metadata and controls
34 lines (33 loc) · 1.08 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
globalThis.html = (raw, ...ars) => {
return raw.reduce((reduce, string, index) => {
let va = ars[index - 1] ?? "";
if (va instanceof Array) va = va.join("");
if (va instanceof Function)
va = `<script>
(${va.toString()})(document.currentScript.parentElement);
document.currentScript.remove();
</script>`;
if (
/<\s*$/.test(reduce) &&
/^\s*\/>/.test(string) &&
(va === "" || /^\s*<[\S\s]+\/[\S\s]*>\s*$/.test(va))
) {
reduce = reduce.replace(/<\s*$/, "");
string = string.replace(/^\s*\/>/, "");
}
return reduce + va + string;
});
};
globalThis.css = (raw, ...ars) => {
const id = "i" + Math.random().toString().slice(2, 6);
const str = String.raw({ raw }, ...ars).replaceAll("&", "." + id);
return `<script>
document.currentScript.parentElement.classList.add("${id}");
document.currentScript.remove();
document.querySelector("style[${id}]") ||
document.head.insertAdjacentHTML(
"beforeend",
\`<style ${id}>${str}</style>\`
);
</script>`;
};