Skip to content
Run BunYour friendly Bun playground

Astro: examples

Each example is the Markdown you write, on the left, and what it renders, on the right. The Astro guide explains every option; this page shows them. The results use runbun({ fences: "all" }), so plain fences render as runbun’s static blocks, as they would on a site that turns it on (this site keeps Expressive Code for its own fences).

Highlighted at build time. They don’t run, and each has a Copy button.

Markdown
```ts
const file = Bun.file("package.json");
console.log(await file.json());
```
Result
const file = Bun.file("package.json");
console.log(await file.json());
Markdown
```ts title="read.ts"
const text = await Bun.file("notes.txt").text();
console.log(text.split("\n").length, "lines");
```
Result
read.ts
const text = await Bun.file("notes.txt").text();
console.log(text.split("\n").length, "lines");

Without a title, a comment naming a file in the first lines becomes the title and leaves the code.

Markdown
```ts
// hash.ts
const hasher = new Bun.CryptoHasher("sha256");
hasher.update("hello");
console.log(hasher.digest("hex"));
```
Result
hash.ts
const hasher = new Bun.CryptoHasher("sha256");
hasher.update("hello");
console.log(hasher.digest("hex"));
Markdown
```
$ bun --version
1.3.0
```
Result
$ bun --version
1.3.0

{2,4-5} marks lines, the same as mark={2,4-5}.

Markdown
```ts {2,4-5}
const server = Bun.serve({
port: 3000,
fetch(req) {
const url = new URL(req.url);
return new Response(`Hello from ${url.pathname}`);
},
});
```
Result
const server = Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    return new Response(`Hello from ${url.pathname}`);
  },
});
Markdown
```ts ins={2} del={3}
const data = await Bun.file("data.json").json();
await Bun.write("out.json", JSON.stringify(data, null, 2));
await Bun.write("out.json", JSON.stringify(data));
```
Result
const data = await Bun.file("data.json").json();
await Bun.write("out.json", JSON.stringify(data, null, 2));
await Bun.write("out.json", JSON.stringify(data));

A comment that is only (N) at the end of a line, and an ordered list right after the fence with one item per marker.

Markdown
```ts
const server = Bun.serve({ // (1)
fetch: () => new Response("hi"), // (2)
});
```
1. One call starts an HTTP server.
2. Every request gets this handler's response.
Result
const server = Bun.serve({
  fetch: () => new Response("hi"),
});
  1. One call starts an HTTP server.
  2. Every request gets this handler's response.

Consecutive fences with the same group become one block with a tab per fence.

Markdown
```ts title="index.ts" group=greet
import { greet } from "./greet";
console.log(greet("Bun"));
```
```ts title="greet.ts" group=greet
export const greet = (name: string) => `Hello, ${name}`;
```
Result
import { greet } from "./greet";
console.log(greet("Bun"));
export const greet = (name: string) => `Hello, ${name}`;

Each fence keeps its own marks.

Markdown
```ts title="index.ts" group=marks {2}
import { sum } from "./sum";
console.log(sum([1, 2, 3]));
```
```ts title="sum.ts" group=marks ins={2}
export const sum = (xs: number[]) =>
xs.reduce((a, b) => a + b, 0);
```
Result
import { sum } from "./sum";
console.log(sum([1, 2, 3]));
export const sum = (xs: number[]) =>
  xs.reduce((a, b) => a + b, 0);

A fence that a static block can’t show as written stays with the site’s own highlighter, here Expressive Code.

Markdown
```ts runbun=false
console.log("Expressive Code draws this one");
```
Result
console.log("Expressive Code draws this one");

Shell languages keep Expressive Code’s terminal frame.

Markdown
```sh
bun add elysia
```
Result
Terminal window
bun add elysia

A fence marked playground runs when a reader presses Run.

Markdown
```ts playground
const langs = ["ts", "tsx", "js"];
for (const lang of langs) console.log(lang, Bun.version);
```
Result
const langs = ["ts", "tsx", "js"];
for (const lang of langs) console.log(lang, Bun.version);

mode=preview shows what the server answers, path where it starts, and deps installs packages.

Markdown
```ts playground mode=preview height=auto path="/user/123" deps="elysia@rc"
import { Elysia } from "elysia";
new Elysia()
.get("/user/:id", ({ params: { id } }) => `User ${id}`)
.listen(3000);
```
Result
import { Elysia } from "elysia";

new Elysia()
  .get("/user/:id", ({ params: { id } }) => `User ${id}`)
  .listen(3000);

layout=stack puts the preview under the editor (this site’s embeds sit beside it otherwise).

Markdown
```ts playground mode=preview layout=stack height=auto
Bun.serve({
fetch: () => new Response("<h1>Hello from Bun.serve</h1>", { headers: { "Content-Type": "text/html" } }),
});
```
Result
Bun.serve({
  fetch: () => new Response("<h1>Hello from Bun.serve</h1>", { headers: { "Content-Type": "text/html" } }),
});

File-name comments name the files, and hidden keeps the package.json out of the tabs while it pins the packages.

Markdown
```ts playground group=app mode=preview height=auto
// index.ts
import { Elysia } from "elysia";
import { greet } from "./greet";
new Elysia().get("/", () => greet("Bun")).listen(3000);
```
```ts playground group=app
// greet.ts
export const greet = (name: string) => `Hello, ${name}`;
```
```json playground group=app hidden
//package.json
{ "dependencies": { "elysia": "rc" } }
```
Result
import { Elysia } from "elysia";
import { greet } from "./greet";

new Elysia().get("/", () => greet("Bun")).listen(3000);
export const greet = (name: string) => `Hello, ${name}`;

requests gives the preview a tab per request; => adds the answer the reader sees before pressing Run.

Markdown
```ts playground mode=preview height=auto deps="elysia@rc" requests='GET / => Hello Elysia; GET /user/617 => 617; POST /form {"hello":"Elysia"} => {"hello":"Elysia"}'
import { Elysia } from "elysia";
new Elysia()
.get("/", "Hello Elysia")
.get("/user/:id", ({ params: { id } }) => id)
.post("/form", ({ body }) => body)
.listen(3000);
```
Result
import { Elysia } from "elysia";

new Elysia()
  .get("/", "Hello Elysia")
  .get("/user/:id", ({ params: { id } }) => id)
  .post("/form", ({ body }) => body)
  .listen(3000);
Hello Elysia
617
Request body
{"hello":"Elysia"}
Response
{"hello":"Elysia"}

In .mdx and .astro files, <Code> and <Playground> take the same options as props.

MDX
import { Code } from "@runbun/astro/components";
<Code lang="ts" title="greet.ts" mark="1" code={`export const greet = (name: string) => "Hello, " + name;
console.log(greet("Bun"));`} />
Result
greet.ts
export const greet = (name: string) => "Hello, " + name;
console.log(greet("Bun"));
MDX
import { Playground } from "@runbun/astro/components";
<Playground mode="preview" height="320px" files={{ "index.ts": `Bun.serve({ fetch: () => new Response("Hello from <Playground>") });` }}>
<p style="padding: 1rem; margin: 0">A server that answers in the preview. Press Run to start it.</p>
</Playground>
Result

A server that answers in the preview. Press Run to start it.

Was this page helpful?

Sends the rating and this page's address; nothing else unless you write it. Privacy

runbun is an independent project, not affiliated with or endorsed by Oven or Bun.