-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
41 lines (34 loc) · 1.91 KB
/
runner.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
import { evaluate } from '@eddmann/santa-lang-wasm';
/**
* Replace code snippet copy functionality with runner
*/
const style = document.createElement('style');
style.innerHTML = `
.md-clipboard:after {
-webkit-mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="48" viewBox="0 96 960 960" width="48"><path d="m383 746 267-170-267-170v340Zm97 230q-82 0-155-31.5t-127.5-86Q143 804 111.5 731T80 576q0-83 31.5-156t86-127Q252 239 325 207.5T480 176q83 0 156 31.5T763 293q54 54 85.5 127T880 576q0 82-31.5 155T763 858.5q-54 54.5-127 86T480 976Zm0-60q142 0 241-99.5T820 576q0-142-99-241t-241-99q-141 0-240.5 99T140 576q0 141 99.5 240.5T480 916Zm0-340Z"/></svg>');
mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="48" viewBox="0 96 960 960" width="48"><path d="m383 746 267-170-267-170v340Zm97 230q-82 0-155-31.5t-127.5-86Q143 804 111.5 731T80 576q0-83 31.5-156t86-127Q252 239 325 207.5T480 176q83 0 156 31.5T763 293q54 54 85.5 127T880 576q0 82-31.5 155T763 858.5q-54 54.5-127 86T480 976Zm0-60q142 0 241-99.5T820 576q0-142-99-241t-241-99q-141 0-240.5 99T140 576q0 141 99.5 240.5T480 916Zm0-340Z"/></svg>');
}
`;
document.head.appendChild(style);
[].forEach.call(document.querySelectorAll('.md-clipboard.md-icon'), (el) => {
const source = document.querySelector(el.dataset.clipboardTarget);
if (!source.classList.contains('language-santa')) {
el.remove();
return;
}
source.addEventListener('dblclick', () => {
source.contentEditable = true;
});
el.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
source.parentNode.nextSibling?.remove();
const result = document.createElement('pre');
source.parentNode.after(result);
try {
result.innerHTML = `<code>${evaluate(source.innerText)}</code>`;
} catch (error) {
result.innerHTML = `<code>${error.message}</code>`;
}
});
});