diff --git a/html/dialogue.tsx b/html/dialogue.tsx new file mode 100644 index 0000000..037d83d --- /dev/null +++ b/html/dialogue.tsx @@ -0,0 +1,49 @@ +import { Html, render } from "./html.ts"; +import { Component } from "./jsx/jsx-runtime.ts"; + +type DOption = [Html, Dialogue]; +type Dialogue = Html | [Html, ...DOption[]]; + +function not_empty(arr: T[]): arr is [T, ...T[]] { + return arr.length > 0; +} + +function reify_dialogue(d: Dialogue): Html { + if (!Array.isArray(d)) return d; + + const [r, ...rest] = d; + if (rest.length == 0) { + return r; + } else { + return ( +
+ {r} + {...rest.map(([r, d]) => { + return ( +
+ {r} +
{reify_dialogue(d)}
+
+ ); + })} +
+ ); + } +} + +const rd = reify_dialogue([ + "Hi!", + ["Hello!", "How are you!"], + [ + "Bye", + [ + ":(", + ["Sorry", :D], + ["Not sorry", D:], + ], + ], +]); + +console.log(rd); +console.log(render(rd)); +console.log(render(rd, "mini")); diff --git a/html/html.ts b/html/html.ts index 13069e4..2b935ab 100644 --- a/html/html.ts +++ b/html/html.ts @@ -96,7 +96,7 @@ function render_elem( let string_last = false; for (let i = 0; i < children.length; i++) { let child = children[i]; - const rendered = render(child, mini); + const rendered = render(child, mini ? "mini" : undefined); if (is_string(child)) { mini && string_last && (inner += " "); @@ -124,7 +124,7 @@ function render_elem( // THIS MEANS MINIFICATION IS SEMANTICALLY DIFFERENT // JK I DID IT THE HARD WAY :] -export function render(elem: Html, mini = true): string { +export function render(elem: Html, mini?: "mini"): string { if (is_string(elem)) { return escape(elem); } else if (elem.tag == Fragment) { @@ -140,7 +140,7 @@ export function render(elem: Html, mini = true): string { return render_elem( { tag, attributes, children: expanded.children }, - mini, + mini == "mini", ); } } diff --git a/html/jsx/jsx-runtime.ts b/html/jsx/jsx-runtime.ts index 38c04f6..2755424 100644 --- a/html/jsx/jsx-runtime.ts +++ b/html/jsx/jsx-runtime.ts @@ -15,7 +15,6 @@ export function create_element( type: Component | string, props_?: Props, ): Html { - console.log(arguments); const props: Props = props_ ?? { children: [] }; const children: Html[] = [props.children ?? []].flat(); const attributes: Attributes = {}; @@ -27,8 +26,6 @@ export function create_element( attributes[key] = val; } - console.log(attributes, children); - if (typeof type == "string") { return { tag: type, attributes, children }; } else { diff --git a/html/main.tsx b/html/main.tsx deleted file mode 100644 index 47ae8d6..0000000 --- a/html/main.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import { Html, render } from "./html.ts"; -import { Component } from "./jsx/jsx-runtime.ts"; - -const Homestuck: Component = (attrs, children) => ( -
- {...children} -
-); - -const bla: Html = ( - - - text -
- <> - homestuck - is - cool - - <> - vast error is - also - cool - -
-
-); -console.log(bla); -console.log(render(bla, false)); -console.log(render(bla, true)); - -export {};