Layouts

You can define layouts in the markdoc options.

import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

markdoc({
    layouts: {
        default: join(
            dirname(fileURLToPath(import.meta.url)),
            './src/lib/Layout.svelte',
        ),
    },
});

Layout files are basically Svelte components that render the children snippet. It is used for all files by default.

<script>
    let { children } = $props();
</script>

<nav>...</nav>

{@render children?.()}

Named

If you want to use a named layout for a specific file, you can specify it in the frontmatter.

import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

markdoc({
    layouts: {
        default: join(
            dirname(fileURLToPath(import.meta.url)),
            './src/lib/Layout.svelte',
        ),
        some_other_layout: join(
            dirname(fileURLToPath(import.meta.url)),
            './src/lib/SomeOtherLayout.svelte',
        ),
    },
});
---
layout: some_other_layout
---

# some other content

Props

Layouts will be passed the frontmatter as props from the Markdoc file.

<script>
    let { title, description } = $props();
</script>

<svelte:head>
    <title>{title}</title>
    <meta name="description" content={description} />
</svelte:head>
---
title: Lorem ipsum
description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
---