Code-heavy posts keep hitting the same three shapes. An install command that's different for npm, bun and pnpm. A setup that only makes sense as numbered steps. A pile of "what about…" questions that would bury the main point if I wrote them all out.
Plain Portable Text handles none of those well. You get three code blocks in a row, or an ordered list with code wedged between items, or a FAQ that doubles the length of the post.
So I added three blocks to this site's editor: code tabs, steps and an accordion. They live in a small local EmDash plugin, render inline in posts and guides, and need zero client-side JavaScript. The idea and the field shapes come from bitdoze's emdashhq-widgets plugin (MIT), which I adapted to this site's styling and Markdown output.
Here's how the pieces fit.
Two halves of a custom block
A custom Portable Text block in EmDash has two parts:
- A block definition in a plugin, which tells the editor how to insert and edit the block. It shows up in the
/ menu with a form built from Block Kit fields. - An Astro component on the site, which renders the saved block.
One catch from the docs: sandboxed plugins can't register Portable Text block types, because the renderer has to be imported into the site at build time. So this has to be a native plugin. For a site-only plugin that's easy, since it can live right in the repo.
Registering a local plugin
The plugin is a file in src/plugins/, registered in astro.config.mjs with a descriptor that points at it:
emdash({
database: d1({ binding: "DB" }),
storage: r2({ binding: "MEDIA" }),
plugins: [
{
id: "content-blocks",
version: "0.1.0",
entrypoint: new URL("./src/plugins/content-blocks/index.ts", import.meta.url).href,
},
],
}),
The plugin itself is definePlugin() with an admin.portableTextBlocks array. I already had one of these for marketing sections on CMS pages (hero, pricing, FAQ and friends), so the widgets went into the same plugin under their own category:
import { definePlugin } from "emdash";
import type { PluginDefinition } from "emdash";
const definition: PluginDefinition = {
id: "content-blocks",
version: "0.1.0",
admin: {
portableTextBlocks: [
],
},
};
export default function createPlugin() {
return definePlugin(definition);
}
The block definitions
Each block gets a type, which becomes the saved block's _type and the key you map a component to. The fields array is Block Kit, and every action_id turns into a property on the saved block.
Code tabs is one repeater. Each row is a tab with a label, a language and the code:
{
type: "widget.tabs",
label: "Code tabs",
category: "Widgets",
description: "One code sample per tab: npm / bun / pnpm, macOS / Linux, TS / JS",
fields: [
{
type: "repeater",
action_id: "tabs",
label: "Tabs",
item_label: "Tab",
min_items: 1,
max_items: 8,
fields: [
{ type: "text_input", action_id: "label", label: "Tab label", placeholder: "bun" },
{ type: "text_input", action_id: "language", label: "Language" },
{ type: "text_input", action_id: "code", label: "Code", multiline: true },
],
},
],
},
Steps adds an optional heading, and each step has a title, details and optional code. The accordion has a repeater of sections plus two toggles: "only one section open at a time" and "open the first section".
The thing to plan around is that repeater rows hold scalar fields. A step's details can't be rich text, so they're a multiline text input. To keep that from feeling like a downgrade, the label tells the editor the two rules the renderer follows: a blank line starts a new paragraph, and backticks become inline code.
{
type: "text_input",
action_id: "body",
label: "Details (blank line = new paragraph, `backticks` = inline code)",
multiline: true,
},
Rendering without JavaScript
On the site, every Portable Text body goes through one component, ArticleBody.astro. Wiring the widgets in is three lines in the components.type map:
---
import { PortableText } from "emdash/ui";
import CodeBlock from "./CodeBlock.astro";
import Accordion from "./widgets/Accordion.astro";
import Steps from "./widgets/Steps.astro";
import Tabs from "./widgets/Tabs.astro";
---
<PortableText
value={value}
components={{
type: {
code: CodeBlock,
"widget.tabs": Tabs,
"widget.steps": Steps,
"widget.accordion": Accordion,
},
}}
/>
Each component gets the saved block as node.
Code tabs use daisyUI's radio tabs. Every tab is an <input type="radio">, so switching works with no script and the arrow keys move between tabs. The one gotcha: radios with the same name are a group across the whole page, so two tab sets would fight over one selection. Scoping the name to the block's _key fixes that:
---
import CodeBlock from "../CodeBlock.astro";
const { node } = Astro.props;
const tabs = (node.tabs ?? []).filter((tab) => tab?.code?.trim());
const group = `tabs-${node._key ?? "block"}`;
---
<div role="tablist" class="tabs tabs-lift not-prose my-8">
{tabs.map((tab, index) => (
<Fragment>
<input
type="radio"
name={group}
role="tab"
class="tab font-mono text-sm"
aria-label={tab.label?.trim() || tab.language?.trim() || `Tab ${index + 1}`}
checked={index === 0}
/>
<div role="tabpanel" class="tab-content p-0">
<CodeBlock node={{ _type: "code", _key: `${group}-${index}`, code: tab.code, language: tab.language }} />
</div>
</Fragment>
))}
</div>
Each panel reuses the site's existing CodeBlock, so tabbed code gets the same server-side highlighting as every other code block.
The accordion is native <details>. The "one open at a time" toggle uses the name attribute on <details>, which tells the browser to close the others in the group when one opens. Same trick as the tabs: scope the name to _key.
---
const { node } = Astro.props;
const items = (node.items ?? []).filter((item) => item?.title?.trim());
const group = node.single ? `accordion-${node._key ?? "block"}` : undefined;
---
<div class="not-prose my-8 flex flex-col gap-3">
{items.map((item, index) => (
<details class="collapse collapse-arrow rounded-xl border border-base-300" name={group} open={node.openFirst && index === 0}>
<summary class="collapse-title text-lg font-semibold">{item.title.trim()}</summary>
<div class="collapse-content">
<WidgetText text={item.body} />
</div>
</details>
))}
</div>
Steps is an <ol> with a border on the left as the rail and a numbered circle on each item. No interactivity at all.
WidgetText is the small component that turns those plain-text bodies into paragraphs. It splits on blank lines, then splits each paragraph on backtick pairs:
---
const { text = "", class: className } = Astro.props;
const paragraphs = text
.trim()
.split(/\n\s*\n/)
.map((p) => p.trim())
.filter(Boolean)
.map((p) => p.split(/(`[^`\n]+`)/).filter(Boolean));
---
{paragraphs.map((parts) => (
<p class:list={["whitespace-pre-line", className]}>
{parts.map((part) =>
part.startsWith("`") && part.endsWith("`") && part.length > 2
? <code>{part.slice(1, -1)}</code>
: part,
)}
</p>
))}
Astro escapes the text, so a body can't inject HTML.
All three use daisyUI theme tokens (border-base-300, text-primary, bg-base-100) instead of hard-coded colors, so they follow the light and dark themes with no extra CSS.
Don't forget the Markdown
This site also serves every post as Markdown: to agents that ask for text/markdown, and in llm.txt. EmDash's portableTextToMarkdown doesn't know about custom blocks, so new block types quietly disappear from that output unless you handle them.
The fix is to flatten each widget into blocks the converter already understands: a bold label, plain paragraphs and code blocks. Tabs become a bold tab name followed by its code block. Steps become bold numbered titles with their details and code. Accordion sections become a bold title and the body.
function flattenWidget(block: Block): Block[] {
const out: Block[] = [];
if (block._type === "widget.tabs") {
for (const tab of rows(block.tabs)) {
para(str(tab.label) || str(tab.language), true);
code(str(tab.code), str(tab.language));
}
} else if (block._type === "widget.steps") {
para(str(block.title), true);
rows(block.steps).forEach((step, index) => {
para(`${index + 1}. ${str(step.title)}`.trim(), true);
body(str(step.body));
code(str(step.code), str(step.language));
});
} else if (block._type === "widget.accordion") {
for (const item of rows(block.items)) {
para(str(item.title), true);
body(str(item.body));
}
}
return out;
}
para also turns backtick runs into spans with a code mark, so inline code comes out as real inline code instead of escaped backticks.
Here's one accordion section three ways: what the editor saves, what the page renders, and what the Markdown version gets.
What I'd tell you before building your own
- Pick block types that belong in prose. Marketing sections on this site only render on CMS pages. These three are meant to sit between paragraphs, which is why
ArticleBody maps them and the Markdown output flattens them. - Namespace the types.
widget.tabs is unlikely to collide with another plugin's tabs. - Write the editing rules into the field labels. The editor sees the label every time. It's the cheapest documentation you'll get.
- Handle the Markdown at the same time as the component. Otherwise the block works on the page and vanishes everywhere else, which is easy to miss.
That's the whole plugin: three block definitions, three small components and one function for the Markdown. If you want the original set this was modeled on, bitdoze's emdashhq-widgets has more of them.