Format MDX with Prettier
Prettier's MDX parser handles Markdown plus JSX components in the same file. Code fences, frontmatter, and JSX props stay tidy. Runs locally.
Loading parser…
How to format MDX
Prettier reformats the Markdown body and the JSX islands together, keeping prop quoting and code-fence indentation consistent.
- 01
Paste MDX
Drop in a docs page with <Component> blocks, frontmatter, and fenced code.
Parsing runs through Prettier’s standalone bundle (or the language-specific equivalent) loaded lazily — no parser ships until the tool is opened.
mdxPrettierASTstandalonelazyinput · sourceraw · AST# Title <Comp/>
loaded · parsedAST · ready - 02
Wait for parser
Prettier and the MDX/Babel plugins load lazily on first paint.
Indent width, tab style, and print-width re-format through a memoised pass — switching
2 → 4spaces never re-parses, only re-prints.lazyindentprint-widthmemoisedliveparser · optionsready · liveprettier + mdx
options applied · livelive - 03
Watch it reflow
Headings spaced, JSX self-close normalised, code fences kept verbatim.
The formatter is deterministic and idempotent: running it twice produces byte-identical output, which keeps diffs and git noise to a minimum.
liveidempotentdeterministicdiff-cleanstableout · formattedpretty · ready# Title <Comp />
formatted · liveidempotent - 04
Copy or download
Save as .mdx for your docs repo or copy to clipboard. Source stays in your browser.
Download writes a
Blobwith the language-appropriate extension; copy usesnavigator.clipboard.writeTextso the output preserves all whitespace exactly.exportclipboardbloblanguage MIMEclient-sideok · copiedready · doneOK Copied
shipped · ready to shipsuccess
Formatting tips that keep diffs sane
Pick one config and pin it
Inconsistent formatting causes diff churn. Commit .prettierrc / .editorconfig and enforce in CI.
Format on save
Editor integration is faster feedback than a pre-commit hook. Use both for belt-and-braces.
Idempotency matters
Running the formatter twice should produce identical output. Test this on a representative file.
Match the language community
Default to community conventions: 2 spaces for JS/CSS, 4 for Python, tabs for Go. Fights over indent are a productivity tax.
Print-width drives wrapping
80, 100, and 120 are the common targets. Wider lines parse fast but review slower side-by-side.
Don’t mix formatters and linters
Prettier handles whitespace; ESLint handles correctness. Disable overlapping rules to avoid fight-club configs.
When to format MDX
Docs sites, blog posts, component galleries.
Docs sites
Normalise pages across Docusaurus, Astro, Nextra. Idempotent: running the formatter twice produces byte-identical output, which is the foundation of a clean CI gate. Pair with a pre-commit hook so PRs arrive already formatted, focusing review on logic.
idempotentComponent galleries
Keep <Demo> blocks tidy in design-system pages. Pairs with EditorConfig + .prettierrc for end-to-end consistency. Whitespace-only changes vanish so reviewers see real diffs, and git blame stays honest about who wrote what.
CI-readyBlog posts
Reformat after a quick edit before publishing. Output respects your indent width, tab style, and print-width — settings carry over for repeat runs. Switching 2-space to 4-space costs nothing in CPU but standardises an entire codebase.
diff-cleanMigration cleanup
Tidy MDX after converting from plain markdown. Format-on-save is the fastest feedback loop; CI is the safety net. Together they make inconsistent indentation a thing of the past and free up review cycles for genuine concerns.
PR-friendlyCode reviews
Strip whitespace churn so diffs show real changes. Whitespace-only changes don't trigger downstream content-hash invalidations, so CDN cache keys and CI cache layers stay warm. Real changes get all the rebuild budget.
review-readyMulti-author repos
Enforce one style across contributors. Pairs with linters (ESLint, Stylelint, pylint) that catch what formatting alone can't — bugs, unused imports, dead code. Each tool stays in its lane, and the codebase stays clean.
deterministicFrequently asked questions
Something missing?
Tell us what you need. We ship the top requests every two weeks.