Skip to content

Guide ​

Installation ​

Astro ​

Install the package:

bash
npm install @ugoku/astro
bash
pnpm add @ugoku/astro
bash
yarn add @ugoku/astro

Add the integration — it wires up both the Shiki transformer and the page script in one step:

ts
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { ugoku } from '@ugoku/astro';

export default defineConfig({
  integrations: [ugoku()],
});

VitePress ​

Install the package:

bash
npm install @ugoku/vitepress
bash
pnpm add @ugoku/vitepress
bash
yarn add @ugoku/vitepress

Register the transformer in your VitePress config:

ts
// .vitepress/config.ts
import { defineConfig } from 'vitepress';
import { ugokuTransformer } from '@ugoku/vitepress';

export default defineConfig({
  markdown: {
    codeTransformers: [ugokuTransformer()],
  },
});

Hook the mount into your theme, so ugoku re-scans on VitePress's SPA route changes, not just on first load:

ts
// .vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme';
import { setupVitePressMount } from '@ugoku/vitepress/client';

export default {
  extends: DefaultTheme,
  enhanceApp({ router }) {
    setupVitePressMount(router);
  },
};

Other unified.js sites (Next.js MDX, Nextra, Docusaurus) ​

There's no dedicated @ugoku/* package for these yet, but you likely don't need one. If your site highlights code with @shikijs/rehype — the common path for Next.js MDX, Nextra, and Docusaurus — it already accepts a transformers array passed straight through to Shiki, and @ugoku/shiki's transformer is a plain ShikiTransformer. Add it to your existing list:

ts
import rehypeShiki from '@shikijs/rehype';
import { ugokuTransformer } from '@ugoku/shiki';

// wherever you currently configure rehypeShiki
.use(rehypeShiki, {
  themes: { light: 'github-light', dark: 'github-dark' },
  transformers: [
    // ...your existing transformers
    ugokuTransformer(),
  ],
})

This only covers the build-time half — turning run/run edit fences into marked-up <pre> elements. Unlike @ugoku/vitepress and @ugoku/astro, nothing here auto-wires the reader-facing half for you. Install @ugoku/core and call mount() yourself once the page's DOM is ready, and unmount()/mount() again on client-side route changes if your framework does them (e.g. Next.js App Router navigations):

ts
import { mount } from '@ugoku/core';

mount(document.body);

This doesn't cover Eleventy — its default markdown engine is markdown-it, not rehype, so a ShikiTransformer passed to @shikijs/rehype doesn't apply there.

Writing runnable snippets ​

Add run to a fence's info string for a run-only block, or run edit to also let readers modify the code before running it:

md
```js run
console.log('hello');
```

```ts run edit
function add(a: number, b: number): number {
  return a + b;
}
console.log(add(2, 3));
```

run edit blocks persist reader edits to localStorage, keyed by the block's original source. If the author changes the snippet, the key changes too and any stale edit is discarded automatically.

TypeScript blocks (ts) have their type annotations stripped in the browser via Sucrase before execution — this isn't type-checking, just enough to make the code valid JavaScript.

Fetching data ​

fetch() works inside the worker like anywhere else in the browser, which makes it a natural fit for examples that call an API. One gotcha: the worker runs from a blob: URL, and a relative path like fetch('/data.json') fails to parse against that — build an absolute URL from self.origin instead:

js
const res = await fetch(new URL('/example-data.json', self.origin));
const data = await res.json();
console.log(data);

This is a same-origin request, so it carries the reader's cookies the same as any other fetch call on your site — see Security for what that means for run edit blocks.

Styling ​

ugoku ships markup with stable class names and CSS variables, but leaves the actual look to you — there's no default visual design to fight.

Buttons ​

The run controls have no default styles at all. Target these classes directly in your own CSS:

ClassElement
.ugoku-controlsWrapper around the buttons
.ugoku-run-btnRun button
.ugoku-stop-btnStop button (shown while code is running)
.ugoku-reset-btnReset button (run edit blocks only)

Output panel ​

The console output panel comes with sensible default colors, each overridable via a CSS variable so you don't need to override the class itself:

ClassCSS variableDefault
.ugoku-line-run-error, .ugoku-run-error-banner--ugoku-color-error#e5534b
.ugoku-string--ugoku-color-string#22863a
.ugoku-number--ugoku-color-number#005cc5
.ugoku-boolean, .ugoku-null--ugoku-color-boolean#d73a49
.ugoku-key--ugoku-color-keyinherit
.ugoku-class-name--ugoku-color-class#6f42c1
.ugoku-special, .ugoku-punctuation, .ugoku-toggle, .ugoku-error-stack--ugoku-color-special / --ugoku-color-punctuation#6a737d

Each console line also gets .ugoku-line-log, .ugoku-line-warn, .ugoku-line-error, .ugoku-line-info, or .ugoku-line-debug depending on which console.* method produced it, alongside the shared .ugoku-line class.

Edit overlay ​

run edit blocks render a transparent textarea over the highlighted code. Its font and padding read from your host's own CSS variables where available (e.g. VitePress's --vp-code-*), falling back to --ugoku-font-edit-* and --ugoku-padding-edit-* variables you can set yourself.

Custom grammars and themes ​

If your site's own Shiki config uses a grammar or theme that isn't in Shiki's bundle, the initial page render works automatically — but the lazy re-highlighter run edit uses needs to be told about it separately, since it builds its own independent highlighter instance in the reader's browser. Register the same loader you already pass to your Shiki/VitePress config:

ts
import { registerLanguageLoader, registerThemeLoader } from '@ugoku/core';

registerLanguageLoader('my-lang', () => import('./my-lang.tmLanguage.json'));
registerThemeLoader('my-theme', () => import('./my-theme.json'));

Call these once, at site init — alongside setupVitePressMount/your Astro integration setup, not inside a component that re-runs on every navigation. Without registration, an unbundled grammar/theme still works for editing and running — the block just falls back to plain-text (no live re-highlighting) with a one-time warning in its output panel.

Security ​

Code runs in a module Web Worker, which has no DOM access — but it's not a sandbox against your own origin. fetch/XMLHttpRequest calls made from inside it carry the same cookies as the page, so a block can call same-origin authenticated APIs. This applies equally to run and run edit blocks: editability doesn't grant new capabilities, it only changes who chooses the code that runs — the doc author for run, the reader for run edit. Since the reader is running their own code in their own session, this is closer to self-XSS than an attack on your site.

run edit persistence is scoped to localStorage, keyed by the block's source hash, and only ever written from the reader's own edits — there's no URL parameter or query string that seeds editor content. If you build anything on top of ugoku that shares or links to an edited snippet (e.g. a "share this" URL), threat-model it first — that's exactly the kind of feature that would let a third party's code land in someone else's session.

The console output panel renders every value via textContent, never innerHTML, so crafted output like console.log('<img onerror=...>') can't execute in the page.

If your site has a Content-Security-Policy, module workers created from a Blob: URL need worker-src blob: (or child-src blob: as a fallback). Without it, ugoku fails closed — the Run button just won't work — rather than failing open.

What's next ​