-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (47 loc) · 1.35 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
import "./index.css";
import { main as Main } from "./dist/Main.js";
import { CODE_QUERY_PARAM } from "./ditto-src/Main.js";
import { classHighlighter } from "@lezer/highlight";
import { themeHighlightStyle } from "./ditto-src/Editor.js";
const DEFAULT_CODE = `module Maybe exports (..)
type Maybe(a) =
| Just(a)
| Nothing
map = fn (mb, f) ->
match mb with
| Just(a) -> Just(f(a))
| Nothing -> Nothing
end
`;
function kebab(str) {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}
function addThemeTokenStyles() {
const style = document.createElement("style");
for (const { tag, class: _, ...styles } of themeHighlightStyle.specs) {
const className = Array.isArray(tag)
? classHighlighter.style(tag)
: classHighlighter.style([tag]);
if (className) {
const rules = Object.entries(styles)
.map(([key, value]) => `${kebab(key)}: ${value}`)
.join(";");
style.innerText += `.${className} { ${rules} }`;
}
}
document.head.appendChild(style);
}
function main() {
addThemeTokenStyles();
let code = DEFAULT_CODE;
const url = new URL(window.location);
const codeParam = url.searchParams.get(CODE_QUERY_PARAM);
if (codeParam) {
try {
code = window.atob(codeParam);
} catch {}
}
const app = document.getElementById("app");
Main(app, code)();
}
main();