init.
This commit is contained in:
commit
8f45f69d09
|
@ -0,0 +1,16 @@
|
||||||
|
html, body {
|
||||||
|
margin: 0;
|
||||||
|
min-height: 100vh;
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
background-color: #000;
|
||||||
|
color: #fff;
|
||||||
|
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #0f0;
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 127 B |
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||||
|
|
||||||
|
<link rel="stylesheet" type="text/css" href="/assets/styles/base.css">
|
||||||
|
|
||||||
|
<title>json as html</title>
|
||||||
|
|
||||||
|
<script defer type="module" src="js/main.mjs"></script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body></body>
|
|
@ -0,0 +1,45 @@
|
||||||
|
function use_as_array (val) {
|
||||||
|
return Array.isArray(val) ? val : [val];
|
||||||
|
}
|
||||||
|
|
||||||
|
function json_as_html (json) {
|
||||||
|
if (json instanceof Node) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof json === "string") {
|
||||||
|
return document.createTextNode(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [type, attributes, children] = json;
|
||||||
|
|
||||||
|
const elem = document.createElement(type);
|
||||||
|
|
||||||
|
attributes.forEach(([key, value]) =>
|
||||||
|
elem[key] = value);
|
||||||
|
// elem.setAttribute(key, value));
|
||||||
|
|
||||||
|
const child_nodes = use_as_array(children).map(json_as_html)
|
||||||
|
child_nodes.forEach(child => elem.appendChild(child));
|
||||||
|
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const demo = [
|
||||||
|
"div",
|
||||||
|
[
|
||||||
|
["id", "wrapper"]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
["h1", [], "header :)"],
|
||||||
|
["p", [], "paragraph! meow"],
|
||||||
|
["p", [], "another paragraph!! this is so cool"],
|
||||||
|
["button", [
|
||||||
|
["onclick", _ => alert("see? it works!")]
|
||||||
|
], "it can even do buttons!"],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
document.body.appendChild(json_as_html(demo));
|
Loading…
Reference in New Issue