Repository navigation
feat(docs): runnable client-side Dang playground on front page - #84
Merged
Merged
Conversation
Evaluating pure Dang (no GraphQL imports) needs nothing platform-specific, but process_unix.go referenced syscall.SysProcAttr.Setpgid, which does not exist on js/wasm and broke the build. Narrow that file to !windows && !js and add no-op process-group stubs for js, so the package compiles for the browser playground while leaving native service management unchanged. Signed-off-by: Alex Suraci <[email protected]>
Add cmd/dang-playground, a js/wasm module that exposes a single dangEval(source) function to JavaScript. It runs the same parse, type-check, and evaluate pipeline as the REPL with fresh standard-library scopes and no GraphQL backend, returning a result object that distinguishes parse, type, and runtime errors. This backs the runnable code snippets on the documentation site. Signed-off-by: Alex Suraci <[email protected]>
Add a \dang-playground{{{ ... }}} macro that turns a verbatim Dang snippet into
an editable widget. Without JavaScript it degrades to a plain code block; with
it, playground.js enhances the block into a textarea plus Run button, lazily
fetching the wasm module (and wasm_exec.js) on first run so other pages stay
light. build.sh now compiles cmd/dang-playground to docs/js and stages Go's
wasm shim; both are build artifacts and are gitignored. The front page seeds
the widget with a small type-and-map example.
Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
04b12ab
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://834a5459.dang-3kk.pages.dev |
| Branch Preview URL: | https://front-page-wasm-playground.dang-3kk.pages.dev |
Add the assets the playground editor needs to syntax-highlight in the browser: the web-tree-sitter runtime (tree-sitter.js, tree-sitter.wasm), the grammar compiled to wasm from treesitter/ (tree-sitter-dang.wasm), and a copy of the shared highlights.scm query. These are committed rather than built at deploy time because the docs build environment only has Go, not the tree-sitter CLI, and the first wasi-sdk download is large. regen-highlight-assets.sh rebuilds them from the in-repo grammar when it changes, the same way src/parser.c is a committed generated artifact. Signed-off-by: Alex Suraci <[email protected]>
Layer a transparent textarea over a highlighted <pre> so the playground shows colored code while staying editable. Highlighting reuses the in-repo grammar and highlights.scm query via web-tree-sitter, mapping capture names to the same chroma palette the static code blocks use, and re-highlights on each keystroke. The grammar loads eagerly (~140KB gzipped) and independently of the heavy eval module, so editing feels instant; if it fails to load the editor degrades to plain text. Asset URLs resolve relative to playground.js so they work under any base path. Signed-off-by: Alex Suraci <[email protected]>
Owner
Author
|
Added syntax highlighting to the playground editor (2 new commits). Approach — reuse the in-repo tree-sitter grammar +
Note on the committed assets: the 4 highlight assets are committed rather |
Instead of committing the web-tree-sitter runtime, grammar wasm, and highlight query, build.sh now generates them on the fly via build-highlight-assets.sh: it builds the grammar with the tree-sitter CLI, copies highlights.scm from the editors/zed submodule, and fetches the pinned web-tree-sitter runtime with curl (no npm). The four assets are gitignored alongside the eval wasm. Generation is best-effort: if the tooling is unavailable the docs still build and the playground editor falls back to plain, uncolored text. Signed-off-by: Alex Suraci <[email protected]>
Cloudflare Pages' build image has neither the tree-sitter CLI nor initialized submodules, so build-highlight-assets.sh now self-provisions: it downloads a pinned prebuilt tree-sitter binary when none is on PATH, and falls back to fetching highlights.scm from the editors/zed submodule's pinned revision on GitHub when the submodule isn't checked out. Both paths were verified to produce byte-identical assets to a local build. Signed-off-by: Alex Suraci <[email protected]>
The feedback script runs before the playground script and appended a "feedback" link to every <pre>, including the playground's fallback block. The playground then seeded its editor from that block's textContent, so the word "feedback" leaked into the editable source. Skip playground elements in the feedback system, and defensively read the seed source with any injected controls stripped. Signed-off-by: Alex Suraci <[email protected]>
Every prebuilt tree-sitter that auto-downloads wasi-sdk for `build --wasm` (0.26.x) is built against glibc 2.39, which is newer than the Cloudflare Pages build image; older binaries instead require emcc/docker. So the build image can't produce the grammar wasm at all. Commit it as a generated artifact (like treesitter/src/parser.c) and have build.sh fetch only the query and web-tree-sitter runtime, which are pure curl and work anywhere. build-highlight-assets.sh gains a --runtime-only mode for that build-time fetch; running it with no arguments still rebuilds the committed grammar wasm when the grammar changes. Signed-off-by: Alex Suraci <[email protected]>
It's a served asset, not an executable. Signed-off-by: Alex Suraci <[email protected]>
The committed docs/js/tree-sitter-dang.wasm is generated from the grammar, so it should track grammar changes like the other generated sources. hack/generate now rebuilds it (via build-highlight-assets.sh --grammar-only) right after the tree-sitter parser is regenerated. The script gained --grammar-only alongside --runtime-only, and normalizes the wasm's file mode so regeneration produces no spurious diff. Signed-off-by: Alex Suraci <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makes the docs front page run Dang client-side in the browser, à la Go's
runnable examples. The marquee snippet is editable, and Run evaluates it via a
WebAssembly build of the real interpreter — no server round-trip.
How it works
pkg/dangnow compiles tojs/wasm. The only blocker wasprocess_unix.gousingsyscall.SysProcAttr.Setpgid(absent on wasm); it'snarrowed to
!windows && !jswith no-op stubs in a newprocess_js.go. Thenon-CGO PEG parser path was already there behind build tags.
cmd/dang-playgroundis a wasm module exposingdangEval(source)to JS.It runs the same
ParseWithRecovery → InferFormsWithPhases → EvalNodepipeline as the REPL, with fresh stdlib scopes and no GraphQL/Dagger
backend. Returns
{ok, value, output, error, stage}(stage =parse/type/eval).
\dang-playground{{{ … }}}Booklit macro (triple-brace verbatim,so braces/quotes/backticks pass through).
playground.jsprogressivelyenhances each block into a textarea + Run button + output panel, lazily
fetching the wasm (and
wasm_exec.js) on first Run so other pages stay light.Without JS it degrades to a plain code block.
build.shcompiles the wasm andstages Go's shim into
docs/js/; both are gitignored build artifacts.Verification
Ran the real wasm through Node + a DOM shim simulating a Run click:
[1,2,3,4].filter { x => x % 2 == 0 }.map { x => x * 10 }→=> [20, 40]1 + "oops"→Type error: cannot use String! as Int![Hello, world!, Hello, Dang!, Hello, you!]Native
go build ./...,go vet, thejs/wasmbuild, and the fullpkg/dangtest suite all pass.verified headlessly but the visual layout is not — worth an eyeball in the
preview. The wasm is ~11MB uncompressed (~2-3MB gzipped), fetched only on first
Run.
Possible follow-ups
🤖 Generated with Claude Code