Documentation

MDX

How markdown mode handles MDX: JSX elements and attributes, expressions, ESM statements, and documents that fail to parse.

Updated · spec 1.12.0

dialect: "mdx" reads the document as MDX 3: CommonMark plus GFM, without indented code blocks and <…> autolinks, with JSX elements and {…} expressions. MDX is the author's own content format, so the mode typesets what an author wrote as prose and leaves what the JavaScript runtime reads alone.

Processed and skipped #

ConstructIn MDX mode
Prose, headings, liststypeset
Children of a JSX elementtypeset
JSX attributes, names and valuesskipped
{…} expression containersskipped whole
import and export statementsskipped
Fenced code, inline codeskipped
Frontmatterskipped, unless frontmatterKeys names keys
transform(input, { locale: "en-US", mode: "markdown", dialect: "mdx" })

Input

import { Callout } from "./callout" export const meta = { title: "A -- B" } <Callout kind="it's" label={"x" + "y"}>He said "stop" -- twice</Callout> Total: {count + "a"} items -- "done".

Output

import { Callout } from "./callout" export const meta = { title: "A -- B" } <Callout kind="it's" label={"x" + "y"}>He said “stop”—twice</Callout> Total: {count + "a"} items—“done”.

The attribute, the expression, the import and the export come back as written; the text inside <Callout> and the paragraph are typeset.

Names compare case-sensitively #

HTML names compare case-insensitively and JSX names case-sensitively. <Code> is a component and is not skipped, so its children are typeset. <code> is the HTML element and is skipped whole.

Documents that do not parse #

MDX embeds JavaScript, so a malformed JSX tag, an unclosed or broken {…} expression or a malformed export is a parse error. transform then throws POLYTYPO_MALFORMED_INPUT. It is the only way input alone can make transform throw, and a document that fails here is one the author's own build refuses.

transform(input, { locale: "en-US", mode: "markdown", dialect: "mdx" })

Input

Broken: {"x" -- "y"

Result

Throws POLYTYPO_MALFORMED_INPUT

Not the same as CommonMark #

The same file is frequently valid in both dialects and means different things. See Dialects for the differences, and for why a dialect is never detected.

Runtime support #

Not every runtime implements MDX. A runtime that does not declines dialect: "mdx" with POLYTYPO_INVALID_DIALECT rather than mishandle it. See the runtime pages.

Normative text: spec/rules/modes.md (§3.7).

Already using polytypo? Add our badge

Two lines: a script tag and a span. No per-embed network call. Pick a language and a theme, then copy the code into your footer or credits. Embedding notes cover Content Security Policy. Sites with the badge are listed on Sites using polytypo.

<script async src="https://polytypo.dev/badge.js"></script>
<span data-polytypo-lang="en" data-polytypo-theme="light"></span>

Search runs in your browser. Nothing is sent anywhere.