import { parse } from "./notcl"; /** * Basic unit of information, also an "actor" in the programming system */ type Card = { /** Unique identifier */ id: number; /** Key-value properties on the card */ fields: Record; /** Eventually: a markdown string containing code, but for now, just code */ code: string; }; /** * "Mode" of the environment a script runs in; determines access to mutability features and such. * * "action": response to a UI action; allowed to modify card fields and access time and random numbers. * * "render": deterministic generation of display markup from card and workspace state; can only modify temporary variables. */ type ScriptType = "action" | "render"; /** * State for running a script in. */ type Vm = { /** Mutability status */ mode: ScriptType; /** Markup to render / output */ output: string; }; /** * @param {Vm} state VM state * @param {string} code Script to run * @returns {string} Markup to render / output */ function renderCard(state, code) { const script = parse(code); if (script[0]) { state.output = JSON.stringify(script[1], null, 2); } else { state.output = script[1]; } return state.output; } /** * Global state: a single card * @type {Card} */ let theCard = { id: 100, fields: {}, code: String.raw` h1 "Hello, World!" para [2 + 2] block { This is a paragraph of text, with one [b bold] word. Yes, this means there has to be some magic in text processing... this won't work. } block -red "Beware!" para "All text should be quoted, it's clearer that way. & blockquotes already should contain paragraphs. (maybe normalize nested paragraphs)" block { First block } { Second block Is this markdown-parsed? [button "No we want to render UI" \\{noop}] } { Since we want escapes to work, these blocks [i will] be subject to substitutions. } # A comment para { line endings escaped\ one slash not escaped if \\ two slashes escaped with a slash if \\\ three slashes not escaped with two slashes if \\\\ four slashes escaped with two slashes if \\\\\ five slashes not escaped with three slashes if \\\\\\ six slashes } `, }; const state = document.createElement("pre"); const display = document.createElement("blockquote"); const debugDisplay = document.createElement("pre"); function render() { const vm = { mode: /** @type {ScriptType} */ "render", output: "", }; const html = renderCard(vm, theCard.code); state.textContent = JSON.stringify(theCard, null, 2); display.innerHTML = html; debugDisplay.textContent = html; } render(); document.body.append(state, display, debugDisplay);