· 3 min read
Components in code blocks
Fenced-block components: a code block whose language is a component name, with YAML fields as its body, becomes that component. Anywhere else (Obsidian…
What we did
- Fenced-block components: a code block whose language is a component name, with YAML fields as its body, becomes that component. Anywhere else (Obsidian, GitHub) it's a readable block of YAML.
src/lib/blocks.ts: a registry,blocks, of name → fields +render().highlight()invite.config.tsasksrenderBlock(lang, code, file)first; anything that isn't one of ours is highlighted by Shiki as before. Themarkdown()plugin passes the file'sidtohighlight().- First component,
video:src,width,heightrequired;webm,poster,captionoptional. It renders the same<video controls muted playsinline preload="metadata">I used to write by hand (WebM<source>first, a<figure>when there's a caption). The./files go throughenhanceMedia()like before, so they become imports and the size check still runs. - The build checks the YAML: a missing required field, an unknown field (
autoplaytoo, on purpose), a size that isn't a whole number, a file without./, text YAML reads as a number, or broken YAML fails, naming the file and the block. Checked in dev too: the page gives a 500 and the terminal prints the file and the field. - 12 tests in
blocks.test.ts, one of them throughenhanceMedia(). - Draft
components-in-code-blockswith a real clip (awesome-filter.webm, 960×540, 580 KB, recorded with Playwright'srecordVideo: there's no ffmpeg in WSL). The publishing guide's Videos section starts with the block now; the videos draft points to the new one. ```textblocks no longer print a Shiki warning:textis plain on purpose.- Checked: Lighthouse accessibility 100 on the draft in both themes (dev), no horizontal scroll at 320px (the video scales to 288×163),
pnpm test:e2epasses.
What I learned
- mdsvex calls the highlighter with
(code, lang, meta, filename)for every code block, and puts whatever it returns into the page as raw HTML. That makes it a hook for anything, not just colours. - A code block's language is just a word. Tools that don't know it show the code as plain text, which is why YAML inside is a good fit: it's readable as it is.
- TypeScript mapped types:
Values<Fields>works the values' types out from the field list (size→number, optional →| undefined), sorender()can't use a field the block doesn't declare. Object.hasOwn(blocks, lang)and notlang in blocks:inalso finds built-in keys liketoString, so a```toStringblock would have looked like a component.
Problems & fixes
- mdsvex's
Highlightertype says the filename may beundefined, sohighlight()couldn't require it. Themarkdown()plugin always knows the file (id), so it passes it in a small arrow function instead. pnpm previewfailed withpreview_build_missing: the build output was gone, probably cleared by a laterpnpm checkor test run (both run SvelteKit's setup). Rebuilt first.
Links
Next
- Open both drafts in Obsidian and on GitHub to check they read well there (the last "Done when" item of Phase 9.5).
- Next components:
compare(before/after) or a live demo.