MDX

MDX lets you use JavaScript and JSX components inside your Markdown files.

Prerequisites

  • Use .mdx instead of .md for the file extension.

Configuration

Reference MDX files the same way as Markdown files:

// scalar.config.json
{
  "$schema": "https://registry.scalar.com/@scalar/schemas/config",
  "scalar": "2.0.0",
  "navigation": {
    "routes": {
      "/example": {
        "title": "Example",
        "type": "page",
        "filepath": "docs/content/example.mdx" // .mdx extension
      }
    }
  }
}

Frontmatter

Add metadata at the top of your MDX files:

---
title: "Page Title"
description: "Page description for SEO"
---

# Content

This is the content of the page.

Importing Components

Import and use other MDX files as components:

// Import MDX from other files
import Intro from './intro.mdx'

<Intro />

# Example

This is **Markdown**.

And this is how the other

# Introduction

This MDX file serves as an imported component in another MDX document.

You can include any Markdown or JSX content here to be reused elsewhere.

Project snippets

Components in the snippets folder at your project root are available in every MDX page, with no import line. Save a file there:

// snippets/pop.mdx
This content comes from a project snippet.

And use it by name on any page:

# Example

<Pop />

This is **Markdown**.

Your project root is the directory that holds your scalar.config.json, or the directory its root setting points to. Only files directly inside snippets count, with an .mdx, .jsx, or .tsx extension; nested folders are not scanned. A .jsx or .tsx snippet needs a default export, or an export named after its component.

Component names

The file name becomes the component name in PascalCase. Dashes, underscores, and spaces separate words, and only the first letter of each word changes case, so capitals elsewhere in a word survive:

FileComponent
snippets/pop.mdx<Pop />
snippets/snippet-intro.mdx<SnippetIntro />
snippets/hello_world.jsx<HelloWorld />
snippets/ColorPicker.tsx<ColorPicker />
snippets/my-URL-helper.mdx<MyURLHelper />

A file whose name cannot be written as a JSX tag gets no component name: snippets/2fa-notice.mdx starts with a digit, and snippets/intro.v2.mdx keeps a dot. Import files like those explicitly instead.

Which name wins

A name your page binds itself keeps its own meaning, whether that is an explicit import, an exported declaration, or a local declaration in scope. Scalar's built-in components keep their names too, so a snippets/card.mdx file does not replace the built-in <Card>. Import it under another name to use it.

When two snippet files claim the same name, .mdx wins over .jsx, which wins over .tsx; after that, the first path wins. Each build logs a warning for every snippet file that does not get its name, so a shadowed snippet does not fail silently.

Explicit imports

Pages that import their snippets by hand keep working, and root-absolute paths resolve against your project root:

import Pop from '/snippets/pop.mdx'

<Pop />

Either way, only the snippets a page renders are bundled with that page, so an unused snippet never adds to it.

In the editor

MDX snippets preview in the editor just like explicit imports, including components nested inside a snippet and children the page passes through. A .jsx or .tsx snippet keeps its component name but shows its source instead of a preview, because the editor renders MDX rather than React.

JSX Elements

Use JSX directly in your content:

<div style={{ padding: "16px", border: "3px dashed #ccc", borderRadius: "8px", margin: "16px 0" }}>
  Custom styled content
</div>

Custom Styles

Interactive elements work too:

<button onClick={() => alert("Hello!")}>
  Click me
</button>

Expressions

Embed JavaScript expressions using curly braces:

The current year is {new Date().getFullYear()}.

The current year is 2026.

What did you think of this content?