Skip to content

@ogygia/content

Markdown

How .md and .svx files compile, and the dialect they understand, from frontmatter, heading ids, callouts and tab groups to highlighted code and islands inside the prose. Every part can be switched off or extended.

On this page

@ogygia/content compiles .md and .svx files to Svelte components at build time, through mdsvex and Shiki. On top of plain markdown you get frontmatter, heading ids and a table of contents, callouts, tab groups, highlighted code with file names, and marked island imports, so an interactive component can sit in the middle of the prose. You don’t import anything for the dialect: when a page uses a tab group, the compiler adds the import for it.

Everything on this page is rendered by the dialect it describes.

---
title: Hello
---

<script>
  import Counter from '$lib/Counter.svelte' with { wake: 'visible' };
</script>

## Install {#install}

::: tip
Static HTML everywhere, except the counter below.
:::

<Counter start={3} />

Setup

Markdown compiles only when the content() plugin has a markdown option, even an empty one, and the svelte config takes the plugin’s preprocessor. Install shows both files. All settings live in content({ markdown }) in vite.config.ts; preprocess() and extensions() in svelte.config.js take no arguments and read them from there.

Once it’s on, a markdown file can be:

  • an entry in a collection, read with get() and rendered with <Region of={entry.body} />. See Collections.
  • a route of its own: src/routes/about/+page.svx is a page like any +page.svelte.

markdown.extensions changes which files compile (default ['.svx', '.md']).

The svelte config has to be loaded after the plugin list is built. If something loads it earlier (usually an import of svelte.config.js at the top of vite.config.ts), it gets no markdown settings and the plugin throws content_config_read_first.

Content of a markdown file is Svelte once it compiles, so a { in prose starts a Svelte expression. Put braces in backticks, or write {. A < that can’t start a tag (a < b) is safe as it is.

Frontmatter

The block between the --- lines at the top of a file becomes the entry’s data, checked by the collection’s schema. It’s read by a YAML parser built for frontmatter:

  • block mappings and lists at any consistent indent, one-line [a, b] and { a: 1 }, and | and > block text;
  • single and double quotes, # comments after a space;
  • numbers, null and ~, and true/false in any of their YAML cases as booleans; yes, no, on and off stay text, so country: no stays 'no';
  • dates stay text (2026-01-01 is a string; let the schema turn it into a Date);
  • a value in braces with no : inside, like title: {@const}, is the text it looks like, and so is a markdown link like [docs](/docs);
  • anchors, aliases, tags and merge keys stay text.

Two keys are taken: headings and links are filled by the compiler (see below) and never reach data.

Bad frontmatter fails the build. A line indented with a tab throws content_frontmatter_tab_indent, a line with nothing before its colon throws content_frontmatter_empty_key, and anything else the parser can’t read throws content_frontmatter_invalid with the reason. parseFrontmatter(text) from @ogygia/content/markdown runs the same parser on a string and returns { data, body }.

---
title: 'Hello: a colon needs quotes'
date: 2026-01-01        # stays the string '2026-01-01'
tags: [svelte, ogygia]
draft: false
summary: >
  Folded text, joined
  into one line.
---

Heading ids

With the default settings, every heading gets an id, so it can be linked to. Write your own at the end of the line, ## Install {#install}, using letters, digits, _, : and -. A heading without one gets an id made from its text and the ids of the headings above it: under ## Setup, the heading ### Vite gets setup-vite. That keeps two ### Vite headings in different sections apart. An h1 counts too, so in a file that starts with # Guide, that ## Setup gets guide-setup; give the h1 an explicit id, or leave it out of the file, if you want short ids. A repeat in the same place gets -1, -2 and so on, and an explicit id reserves itself, so a later heading that would make the same id gets a suffix. A heading with no letters or digits gets section.

h2 to h4 are collected into the entry’s meta.headings as { depth, id, text }, in order, with the same ids. Build a table of contents from that rather than reading the HTML back. markdown.headings takes { minDepth, maxDepth } (each 2, 3 or 4) to collect fewer. false collects none and also turns off the ids made from text: only headings with an explicit {#id} keep an id and an anchor, and code block ids lose their heading part.

h2 to h6 headings with an id also get a link anchor, <a class="og-heading-anchor" href="#id">, that the theme shows on hover. It’s plain HTML, so it works with no JavaScript. markdown.headingAnchors: false leaves it out, and markdown.headingIds: false turns off the {#id} syntax.

<script lang="ts">
  import type { Heading } from '@ogygia/content';
  let { headings }: { headings: Heading[] } = $props();
</script>

<nav>
  {#each headings as h}
    <a
      href="#{h.id}"
      style:padding-left="{(h.depth - 2) * 1}rem">{h.text}</a
    >
  {/each}
</nav>

Callouts

A ::: block makes a callout: tip, info, note (styled as info), important, warning, caution (styled as warning) and danger. Text after the name on the opening line is the title; without one, the title is the name in capitals (TIP, WARNING), except details, which shows Details. details makes a native <details> disclosure whose title is its summary.

The body is ordinary markdown, with code, lists and links. It needs no blank lines around it, callouts can nest, and one you forget to close is closed at the end of the file. A ::: inside a code block is left alone.

The output is <div class="og-admonition og-admonition-<kind>" role="note"> with the title in .og-admonition-title; the stock theme styles it, or you style those classes yourself. markdown.containers: false turns the syntax off.

Heads up

Props must be serializable. (This box is the example above, rendered.)

What it expands into

An .og-admonition wrapper the theme styles. Markdown keeps rendering inside it, including code, links and lists.

::: warning Heads up
Props must be serializable.
:::

Tabs

Two kinds of block become tabs. Both compile to the TabGroup and Tab components, and the compiler adds their import to the file.

  • ::: code-group holds code blocks, one tab each. The label goes in brackets after the language (bash [npm]) and is taken out before highlighting. A block with no label is Tab 1, Tab 2 and so on. Anything between the code blocks is ignored.
  • ::: tabs holds any markdown, with a == Label line starting each tab.

The choice a reader makes is shared and remembered. Every code group with the same set of labels on a page switches together (pick pnpm once, and every install block follows), and the choice is saved in localStorage, so later pages open on it. Each ::: tabs block on a page is on its own unless you name a group. Unnamed blocks are numbered per file (tabs0, tabs1), so the first unnamed ::: tabs on every page shares one remembered choice; name groups that shouldn’t share. A name after the opener puts blocks in one group: ::: code-group install, ::: tabs platform.

A tab group is a small island that wakes on load, so a page with tabs ships a little JavaScript; the panels themselves are server HTML. In a .svelte file, use the components directly: <TabGroup group="install"><Tab label="npm">…</Tab></TabGroup>, from @ogygia/content. markdown.tabs: false turns the syntax off.

npm i ogygia
pnpm add ogygia

Query the index in a server load.

Ship the static index; the worker filters it.

::: code-group

```bash [npm]
npm i ogygia
```

```bash [pnpm]
pnpm add ogygia
```

:::
::: tabs
== On the server
Query the index in a server load.
== In a worker
Ship the static index; the worker filters it.
:::

Code highlighting

Code blocks are highlighted by Shiki at build time, so the page ships HTML and no highlighter. The defaults:

  • Themes: github-light and github-dark, switched by CSS light-dark() and the page’s color-scheme. Set markdown.themes to { light, dark } (Shiki theme names or theme objects) and markdown.defaultColor to another Shiki defaultColor, or false.
  • Languages: about forty, with their aliases (ts, sh, yml, md and so on): Svelte, TypeScript, JavaScript, JSX, TSX, HTML, CSS, SCSS, Sass, Less, Vue, Astro, JSON, JSONC, JSON5, YAML, TOML, INI, XML, GraphQL, SQL, Bash, shell sessions, PowerShell, diff, Docker, Make, nginx, Markdown, MDX, Python, Rust, Go, Ruby, PHP, Java, Kotlin, C, C++, C#, Swift and plain text. markdown.langs replaces the list. A language not in it renders as plain text instead of failing the build.
  • Wrapper: each block sits in <div class="code-only">. markdown.wrapperClass renames it, false leaves it out.

What follows the language on the opening line is the block’s meta. title=src/lib/site.server.ts (or file=) names the file; a site’s code header shows it, and the <pre> carries it as data-file. A value with spaces goes in double quotes. The name reads to the end of the token, so paths with /, . and +page work as written. copy=false and link=false are read into the block’s meta too (and /// copy: and /// link: with slash_meta()), for a variant generator of your own; the stock output doesn’t use them.

Every block also gets a stable id, <heading-id>-code-<hash> (code-<hash> before the first heading), made from its section and its text, so a link to it survives blocks being added above it. markdown.codeIds: false leaves it out.

Line highlights and type hovers

{2-4} line highlights, // [!code ++] comments and twoslash type hovers come from Shiki transformers, and none are on by default. Add the ones you use to markdown.code.transformers; each gets the block’s raw meta, so {2-4} and twoslash after the language reach them. Their styles are yours to add.

File names in a comment

Content ported from svelte.dev names files with a first-line comment: /// file: App.svelte (or <!--- file: … --->, ### file: …). Add slash_meta() from @ogygia/content/markdown to markdown.code.meta, next to the default infostring(), and the comment becomes the file name and is taken out of the code. Parsers run in order; a later one wins on the same key.

```ts title=src/lib/site.server.ts
export const site = {};
```
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { content } from '@ogygia/content/vite';
import {
  transformerMetaHighlight,
  transformerNotationDiff
} from '@shikijs/transformers';
import { transformerTwoslash } from '@shikijs/twoslash';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    content({
      markdown: {
        code: {
          transformers: [
            transformerMetaHighlight(),
            transformerNotationDiff(),
            transformerTwoslash({ explicitTrigger: true })
          ]
        }
      }
    }),
    sveltekit()
  ]
});
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { content } from '@ogygia/content/vite';
import {
  infostring,
  slash_meta
} from '@ogygia/content/markdown';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    content({
      markdown: {
        code: {
          meta: [infostring(), slash_meta()]
        }
      }
    }),
    sveltekit()
  ]
});

Code diffs

Two transformers from @ogygia/content/markdown mark changed code, in the style svelte.dev uses. They do nothing to a block without marks, and the marks never reach the reader.

diff_markers() marks whole lines. A line starting +++ is added and --- removed (a bare +++ or --- marks an empty line). The lines get og-diff og-diff-add or og-diff og-diff-remove, and the <pre> gets og-has-diff. It skips diff, yaml, markdown, mdx and svx blocks, where a leading --- means something.

inline_markers({ classes? }) marks a span inside a line: +++added+++ and ---removed---. A --- alone on its line is never a mark, so frontmatter shown in a code block is safe, and it skips diff, markdown, mdx and svx blocks. It works alongside twoslash. The spans get og-mark-add and og-mark-remove; classes: { add, remove } uses your own.

For languages either one skips, use // [!code ++] comments from @shikijs/transformers.

```js
--- export let count;
+++ let { count } = $props();
```
export let count;
let { count } = $props();
```js
let +++doubled = $derived(count * 2)+++;
```
let doubled = $derived(count * 2);
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { content } from '@ogygia/content/vite';
import {
  diff_markers,
  inline_markers
} from '@ogygia/content/markdown';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    content({
      markdown: {
        code: {
          transformers: [
             diff_markers(),
            inline_markers() 
          ]
        }
      }
    }),
    sveltekit()
  ]
});

Components in markdown

A <script> block in a markdown file works as it does in a component, except that the content macros (code, md, loader and regions) aren’t compiled there, and a call fails the build with macro_in_markup. Call them in a .ts module and import the value. A component imported with a mark becomes an island or a server island, exactly as in a .svelte page; one imported without a mark renders as server HTML. The live demos in these docs are islands inside .svx pages.

A markdown file’s own <style> block, and the styles of the components it imports, reach the page too, even though the file is only ever rendered on the server: the build emits them as a stylesheet, and the body links it when it renders.

Place a block-level island on a line of its own, not inside a sentence: a <div> inside a paragraph is moved out by the browser’s parser and hydrates twice.

<script>
  import Counter from '$lib/Counter.svelte' with { wake: 'load' };
</script>

Click it:

<Counter start={3} />

Plugins

Every option is listed under markdown options.

  • remark and rehype add unified plugins: a plugin, a [plugin, options] pair, or a preset. A plain entry runs after the built-in passes. { enforce: 'pre', plugin } runs before them, so headings and code a plugin generates get ids and reach the table of contents and the link check like hand-written ones. A plugin whose output depends on files outside the document adds cache_key: () => string (a value that changes when those files do) and dependencies: (filename) => string[] (files whose edits recompile the document in dev).
  • code.transformers are Shiki transformers, run on every block.
  • code.meta are parsers for a block’s meta, (fence) => fence, run in order. The default is [infostring()].
  • code.variants turn one block into several switchable versions (a TypeScript block and its JavaScript twin, say). The first generator that claims a block renders every version as static HTML with a switcher, bound to a stored preference.
  • overrides wraps chosen elements (a, img and code with true, or { tags }) so a site can swap how they render with its own components. See Swap how markdown elements render.
  • layout is passed to mdsvex as is.

A .md file with nothing dynamic in it (no script, no component, no tabs, no Svelte expression) can compile to plain HTML data instead of a component: set markdown.region: true. Its body then arrives already rendered, so awaiting it costs nothing. A file that isn’t static, every .svx file, and every file while markdown.overrides is on keep the component path.

Images in markdown are handled by @ogygia/image when its plugin is listed: see Markdown images.

import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { content } from '@ogygia/content/vite';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    content({
      markdown: {
        remark: [remarkMath],
        rehype: [rehypeKatex]
      }
    }),
    sveltekit()
  ]
});

Build cache

Compiling markdown is the slow part of a content build, so its results are kept in node_modules/.ogygia/: each compiled document under markup/, each highlighted code block under fences/. An entry’s address is a hash of its text, the settings that shape it, and the package’s own code, so a change to any of them makes a new entry and nothing is ever invalidated by hand. A host that caches node_modules between builds keeps it, and a second build recompiles only what changed. A document with islands in it is always compiled, because compiling it is what registers its islands with the build.

Plugins and transformers can’t be hashed, so the address holds only part of them. Remark and rehype plugins count by how many there are (plus any cache_key they return), and Shiki transformers by their name. Neither their options nor their package versions are in it, and neither is Shiki’s version. So when you swap a plugin for another, change its options, upgrade it or Shiki, or change a custom stage’s code, also change markdown.code.cacheSalt. It salts both the document and the code block cache; without the change, the cache keeps serving the old output.

Do and don’t

  • Do use the dialect: callouts and tabs carry structure that plain paragraphs lose.
  • Do keep install commands in a code-group, so readers pick a package manager once.
  • Do give a heading an explicit {#id} when other pages link to it, so rewording it doesn’t break the links.
  • Don’t import TabGroup in a content file; the compiler adds it when the syntax appears.
  • Don’t fake an edit with a diff block. The diff language loses the real highlighting, while the marks keep it.
  • Don’t add a separate mdsvex plugin or preprocessor; all settings live in content({ markdown }).