Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611<script setup lang="ts">import type { TypstState } from "@typbase/engine";
import { type CapturedLog, RECOVERY_CASES, captureConsole, compileForLab, runIndexCheck, typingSimulation, validateReport, type CaseResult, type CheckedSegment, type CompileReport,} from "~/lib/typstLab";
const REPRO_SOURCE = [ "= Welcome to typbase", "", "This is a *Typst* document, and the app reads it as data:", "", '- `#typbase.query("pages")` reads app data as JSON', '- `#typbase.embed("<page-id>")` includes another page', "", "Every page in this workspace:", "", '#let pages = typbase.query("pages")', "", "#for page in pages [", " - #page.title (#page.path)", "]", "", "== Your turn", "", "Create a page from the sidebar, or open today's daily note.",].join("\n");
const source = ref(REPRO_SOURCE);const report = ref<CompileReport>();const running = ref(false);const captured = ref<CapturedLog[]>([]);const error = ref<string>();
const { ensure, workspace, workspaceId, dataRevision, wipeStorage } = useWorkspace();const plugins = usePlugins();const { confirm } = useConfirm();const router = useRouter();const typstState = shallowRef<TypstState>();
function openPluginInstance(instanceId: string): void { if (plugins.surfaceOf(instanceId, "pane")) { void router.push({ query: { view: `plugin:${instanceId}` } }); } else if (plugins.surfaceOf(instanceId, "window")) { plugins.openWindow(instanceId); }}
const store = computed(() => workspace.value);
const pagesCount = computed(() => { void dataRevision.value;
return store.value?.listPages().length ?? 0;});
/* wasm memory meter refreshes on activity. Bump with the lab runs */const wasmMemoryMB = ref(0);onMounted(() => { const tick = () => { if (typstState.value) wasmMemoryMB.value = Math.round(typstState.value.memoryBytes() / 1e6); }; setInterval(tick, 2000);});
const indexCheck = ref<{ ok: boolean; checked: number; mismatches: string[]; segments: CheckedSegment[];}>();const indexCheckRunning = ref(false);
async function runIndexMappingCheck() { indexCheckRunning.value = true; try { await ensure(); typstState.value ??= await useTypst(); indexCheck.value = await runIndexCheck(typstState.value, workspaceId.value, source.value); } catch (reason) { indexCheck.value = { ok: false, checked: 0, mismatches: [reason instanceof Error ? reason.message : String(reason)], segments: [], }; } finally { indexCheckRunning.value = false; }}
const typingReport = ref<{ prefixes: number; crashes: string | null; wasmStartMB: number; wasmEndMB: number;}>();const typingRunning = ref(false);
async function runTypingSimulation() { typingRunning.value = true; try { const storeInstance = await ensure(); if (!storeInstance) throw new Error("No workspace is open."); typstState.value ??= await useTypst(); typingReport.value = await typingSimulation( typstState.value, storeInstance, workspaceId.value, source.value, ); } catch (reason) { typingReport.value = { prefixes: 0, crashes: reason instanceof Error ? reason.message : String(reason), wasmStartMB: 0, wasmEndMB: 0, }; } finally { typingRunning.value = false; }}
const battery = ref<CaseResult[]>([]);const batteryRunning = ref(false);
async function runRecoveryBattery() { batteryRunning.value = true; battery.value = [];
const consoleCapture = captureConsole(); try { const storeInstance = await ensure(); if (!storeInstance) throw new Error("No workspace is open."); typstState.value ??= await useTypst();
for (const testCase of RECOVERY_CASES) { let result: CaseResult; try { const compiled = await compileForLab( typstState.value, storeInstance, workspaceId.value, testCase.source, ); result = { id: testCase.id, label: testCase.label, crash: null, frames: compiled.frames.length, diagnostics: compiled.diagnostics.length, issues: validateReport(compiled, testCase.source), }; } catch (reason) { result = { id: testCase.id, label: testCase.label, crash: reason instanceof Error ? reason.message : String(reason), frames: 0, diagnostics: 0, issues: [], }; } battery.value = [...battery.value, result]; } captured.value = consoleCapture.captured.slice(-160); } finally { consoleCapture.restore(); batteryRunning.value = false; }}
async function run() { if (!source.value.trim()) return;
running.value = true; error.value = undefined; report.value = undefined;
const consoleCapture = captureConsole(); try { const storeInstance = await ensure(); if (!storeInstance) throw new Error("No workspace is open."); typstState.value ??= await useTypst(); report.value = await compileForLab( typstState.value, storeInstance, workspaceId.value, source.value, ); captured.value = consoleCapture.captured.slice(-120); } catch (reason) { error.value = reason instanceof Error ? `${reason.message}\n${reason.stack ?? ""}` : String(reason); } finally { consoleCapture.restore(); running.value = false; }}
async function resetWorkspace() { const reset = await confirm({ title: "Reset workspace?", description: "Delete every workspace from the active storage and reload? This cannot be undone.", confirmLabel: "Reset", }); if (!reset) return;
try { await ensure(); await wipeStorage(); } catch { // Nothing left to wipe. The reload still resets the in-memory state. } window.location.reload();}
definePageMeta({ ssr: false });</script>
<template> <main class="lab"> <header class="lab__header"> <h1>Typst lab</h1> <p class="lab__hint"> Compile arbitrary Typst against the live workspace. Diagnostics, requests, and wasm logs surface here. Dev tooling, not product UI. </p> </header>
<section class="lab__workspace"> <h2>Workspace</h2> <dl class="lab__facts"> <div> <dt>Pages</dt> <dd>{{ pagesCount }}</dd> </div> <div> <dt>Categories</dt> <dd>{{ store?.listCategories().length ?? 0 }}</dd> </div> <div> <dt>Settings</dt> <dd>{{ JSON.stringify(store?.getSettings().name) }}</dd> </div> <div> <dt>System fonts</dt> <dd>{{ systemFontFamilies.length }}</dd> </div> <div> <dt>Wasm memory</dt> <dd>{{ wasmMemoryMB }} MB</dd> </div> </dl> <UiButton class="button--color-danger" @click="resetWorkspace"> Wipe storage and reload </UiButton> </section>
<StorageExplorer />
<section class="lab__plugins"> <h2>Plugins</h2> <PluginManager @open-instance="openPluginInstance" /> <PluginLab /> </section>
<section class="lab__run"> <Label class="lab__field"> <span>Typst source</span> <textarea v-model="source" rows="14" class="lab__input" spellcheck="false" /> </Label> <div class="lab__actions"> <UiButton variant="primary" :disabled="running" @click="run"> {{ running ? "Compiling..." : "Compile" }} </UiButton> <UiButton :disabled="indexCheckRunning" @click="runIndexMappingCheck"> {{ indexCheckRunning ? "Checking..." : "Check index" }} </UiButton> <UiButton :disabled="typingRunning" @click="runTypingSimulation"> {{ typingRunning ? "Typing..." : "Simulate typing" }} </UiButton> </div> </section>
<section v-if="typingReport" class="lab__check" :class="{ 'lab__check--fail': typingReport.crashes }" > <h2>Typing simulation</h2> <p> {{ typingReport.prefixes }} prefixes compiled {{ typingReport.crashes ? `— crashed: ${typingReport.crashes}` : "— no crash" }} · wasm heap {{ typingReport.wasmStartMB }} → {{ typingReport.wasmEndMB }} MB </p> </section>
<section v-if="indexCheck" class="lab__check" :class="{ 'lab__check--fail': !indexCheck.ok }"> <h2>Index mapping check</h2> <p> {{ indexCheck.ok ? "OK" : "FAILED" }} — {{ indexCheck.checked }} positions per direction </p> <ul v-if="indexCheck.mismatches.length"> <li v-for="mismatch in indexCheck.mismatches.slice(0, 20)" :key="mismatch"> <code>{{ mismatch }}</code> </li> </ul> <UiCollapsible v-if="indexCheck.segments.length" class="lab__segments" :label="`${indexCheck.segments.length} segments`" > <table class="lab__table"> <thead> <tr> <th>#</th> <th>kind</th> <th>from</th> <th>to</th> </tr> </thead> <tbody> <tr v-for="(segment, index) in indexCheck.segments" :key="index"> <td>{{ index }}</td> <td> <code>{{ segment.generated ? `${segment.kind} (generated)` : segment.kind }}</code> </td> <td> <code>{{ segment.from }}..{{ segment.from_end }}</code> </td> <td> <code>{{ segment.to }}..{{ segment.to_end }}</code> </td> </tr> </tbody> </table> </UiCollapsible> </section>
<section class="lab__battery"> <div class="lab__actions"> <h2>Recovery battery</h2> <UiButton :disabled="batteryRunning" @click="runRecoveryBattery"> {{ batteryRunning ? "Running..." : `Run ${RECOVERY_CASES.length} cases` }} </UiButton> </div> <table v-if="battery.length" class="lab__table"> <thead> <tr> <th>Case</th> <th>Frames</th> <th>Diags</th> <th>Issues</th> </tr> </thead> <tbody> <tr v-for="result in battery" :key="result.id" :class="{ 'lab__table-row--fail': result.crash || result.issues.length, }" > <td>{{ result.label }}</td> <td>{{ result.crash ? "crash" : result.frames }}</td> <td>{{ result.diagnostics }}</td> <td> <span v-if="result.crash" class="lab__crash">{{ result.crash }}</span> <span v-else-if="result.issues.length">{{ result.issues.join("; ") }}</span> <span v-else>—</span> </td> </tr> </tbody> </table> </section>
<p v-if="error" class="lab__error">{{ error }}</p>
<template v-if="report"> <section class="lab__report"> <h2>Result</h2> <p> {{ report.frames.length }} frames · {{ report.diagnostics.length }} diagnostics · {{ report.durationMs.toFixed(0) }}ms · {{ report.resolvedRequests.length }} requests resolved <span v-if="report.requests.length">({{ report.requests.length }} unresolved)</span> </p>
<h3>Diagnostics</h3> <ul class="lab__diags"> <li v-for="(diagnostic, index) in report.diagnostics" :key="index"> <span class="lab__sev" :class="`lab__sev--${diagnostic.severity.toLowerCase()}`"> {{ diagnostic.severity }} </span> {{ diagnostic.range.start }}..{{ diagnostic.range.end }} — {{ diagnostic.message }} <ul v-if="diagnostic.hints.length"> <li v-for="hint in diagnostic.hints" :key="hint" class="lab__hint"> {{ hint }} </li> </ul> </li> <li v-if="report.diagnostics.length === 0">none</li> </ul>
<h3>Resolved requests</h3> <ul class="lab__requests"> <li v-for="request in report.resolvedRequests" :key="JSON.stringify(request)"> {{ request.type }}: {{ JSON.stringify(request.value ?? request) }} </li> <li v-if="report.resolvedRequests.length === 0">none</li> </ul> </section> </template>
<section v-if="captured.length" class="lab__console"> <h2>Wasm console (last {{ captured.length }})</h2> <pre class="lab__pre">{{ captured.map((entry) => `[${entry.level}] ${entry.line}`).join("\n") || "nothing captured" }}</pre> </section> </main></template>
<style>.lab { max-width: 900px; margin: 0 auto; padding: var(--space-6); display: flex; flex-direction: column; gap: var(--space-5); min-height: 100vh;}
.lab__header h1 { margin: 0 0 var(--space-1); font-size: var(--text-3xl);}
.lab__hint { margin: 0; color: var(--color-text-secondary); font-size: var(--text-md);}
.lab__workspace,.lab__run,.lab__report,.lab__console { border: 1px solid var(--color-border); border-radius: var(--radius-md); padding: var(--space-4); background: var(--color-surface);}
.lab__facts { display: flex; gap: var(--space-6); margin: 0 0 var(--space-3);}
.lab__facts dt { font-size: var(--text-xs); color: var(--color-text-secondary);}
.lab__facts dd { font-weight: 600;}
.button--color-danger { color: var(--color-danger); border-color: var(--color-danger);}
.lab__field { display: flex; flex-direction: column; gap: var(--space-1-5); margin-bottom: var(--space-3);}
.lab__field span { font-size: var(--text-sm); color: var(--color-text-secondary);}
.lab__input { font-family: var(--font-mono); font-size: var(--text-md); padding: var(--space-2-5); border: 1px solid var(--color-border); border-radius: var(--radius-sm); resize: vertical;}
.lab__actions { display: flex; align-items: center; gap: var(--space-2-5);}
.lab__actions h2 { margin-right: auto;}
.lab__check { border: 1px solid var(--color-border); border-radius: var(--radius-md); padding: var(--space-4); background: var(--color-surface);}
.lab__check--fail { border-color: var(--color-danger);}
.lab__check ul { font-size: var(--text-sm); color: var(--color-danger);}
.lab__battery { border: 1px solid var(--color-border); border-radius: var(--radius-md); padding: var(--space-4); background: var(--color-surface);}
.lab__table { width: 100%; border-collapse: collapse; font-size: var(--text-md); margin-top: var(--space-3);}
.lab__table th,.lab__table td { text-align: left; padding: var(--space-1-5) var(--space-2); border-bottom: 1px solid var(--color-border); vertical-align: top;}
.lab__table-row--fail td { color: var(--color-danger);}
.lab__crash { font-family: var(--font-mono); font-size: var(--text-xs);}
.lab__error { color: var(--color-danger); white-space: pre-wrap; font-family: var(--font-mono); font-size: var(--text-sm);}
.lab__diags,.lab__requests { padding-left: var(--space-5); font-size: var(--text-md); display: flex; flex-direction: column; gap: var(--space-1);}
.lab__sev { font-weight: 700; text-transform: uppercase;}
.lab__sev--error { color: var(--color-danger);}
.lab__hint { color: var(--color-text-secondary);}
.lab__pre { max-height: 22rem; overflow: auto; background: #111; color: #ddd; padding: var(--space-3); border-radius: var(--radius-sm); font-size: var(--text-xs); white-space: pre-wrap;}
.lab__plugins { display: grid; gap: var(--space-3);}</style>