Code Blocks, Line by Line

Your Name(updated September 3, 2026)2 min read5 chapters
Code Blocks, Line by Line cover

Code blocks are rendered by Expressive Code. They use the github-light theme in light mode and Tokyo Night in dark mode, so the code matches the rest of the page in both. Every block has a copy button and shows its language in the corner.

Titles and frames

Add a title to show a file name in an editor tab:

```ts title="src/lib/slugify.ts"
export const slugify = (s: string) => s.toLowerCase().replace(/\s+/g, '-');
```
src/lib/slugify.ts
export const slugify = (s: string) => s.toLowerCase().replace(/\s+/g, '-');

Shell languages get a terminal frame:

Install dependencies
pnpm install
pnpm build

Use shellsession when you want to show a command together with its output. Line numbers are turned off for it:

Terminal window
$ node --version
v22.23.5
$ pnpm --version
12.22.0

Highlighting lines

Mark lines by number with {…}, or show what was added and removed with ins and del:

import { getCollection } from 'astro:content';
export async function getPosts() {
const posts = await getCollection('blog');
return posts
.sort((a, b) => a.data.pubDate - b.data.pubDate);
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.filter((post) => !post.data.isDraft)
.map((post) => ({ ...post, url: `/post/${post.id}/` }));
}

You can also mark text inside a line, which is handy when only one argument changed:

await fetch(url, { method: 'POST', timeout: 5000 });

Diffs

A diff block keeps the + and - colouring. Add lang to keep syntax highlighting as well:

{
"scripts": {
"build": "astro build",
"build": "astro check && astro build",
"test": "vitest run"
}
}

Collapsing boilerplate

Long setup code can be folded so readers see the part that matters. Click the folded lines to expand them:

9 collapsed lines
import { describe, expect, it } from 'vitest';
import { formatListDate, formatLongDate } from '../posts-core';
const day = (iso: string) => new Date(iso);
const zones = ['UTC', 'Asia/Taipei', 'America/New_York'];
for (const zone of zones) {
// warm up Intl so the first test is not slower
new Intl.DateTimeFormat('en-US', { timeZone: zone }).format(new Date());
}
describe('dates', () => {
it('formats in the given time zone', () => {
const d = day('2025-11-01T00:30:00+08:00');
expect(formatListDate(d, 'Asia/Taipei')).toBe('2025.11.01');
expect(formatLongDate(d, 'Asia/Taipei')).toBe('November 1, 2025');
});
});

Long lines

Lines do not wrap by default; the block scrolls sideways instead, so indentation stays intact:

SELECT p.id, p.title, p.published_at, count(c.id) AS comment_count FROM posts p LEFT JOIN comments c ON c.post_id = p.id WHERE p.published_at > now() - interval '30 days' GROUP BY p.id ORDER BY comment_count DESC LIMIT 20;

Add wrap to a block when wrapping reads better, as for long prose in a config file:

description: A three-column Astro theme for long-form technical posts and series, with first-class CJK typography and light and dark code themes.