-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPedroDown.js
73 lines (65 loc) · 2.36 KB
/
PedroDown.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
import { parse, Render, buildDom } from "../lib/imports.js"
import DOM from "./DomBuilder.js";
export default function renderFromString(str) {
return new PedrothRender().render(parse(str));
}
class PedrothRender extends Render {
renderBlockCode(blockCode, context) {
const { code, language } = blockCode;
const split = language.split("*");
if (split.length === 1)
return super.renderBlockCode(blockCode, context);
const container = super.renderBlockCode({ code, language: split[0] }, context);
const evaluator = () => {
let evaluation;
try {
evaluation = eval(code);
} catch (e) {
evaluation = e.message;
}
return JSON.stringify(evaluation, null, 2);
}
const uid = generateUniqueID(10);
const resultBlockBuilder = () => super
.renderBlockCode({
code: evaluator(),
language: "js"
}, context)
const result = buildDom("div")
.appendChild(
buildDom("div")
.attr("style", "padding: 0 1.5em")
.attr("id", uid)
.appendChild(resultBlockBuilder()),
)
if (code.includes("Math.random()")) {
result.appendChild(
buildDom("button")
.attr("class", "button")
.attr("style", "position: absolute; bottom: 0; right: 1.5em;")
.inner("replay")
.event("click", async () => {
DOM.ofId(uid)
.removeChildren()
.append(
resultBlockBuilder()
.build()
)
})
);
}
container.appendChild(result);
return container;
}
}
function generateUniqueID(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let randomID = '';
const randomBytes = new Uint8Array(length);
window.crypto.getRandomValues(randomBytes);
for (let i = 0; i < length; i++) {
randomID += characters[randomBytes[i] % charactersLength];
}
return randomID;
}