MDX Notes
How to control layout, sidebar, navigation, table of contents, draft mode, and feature flags — all from your MDX frontmatter.
Frontmatter as Configuration
Frontmatter isn't just for metadata and SEO. It's also a per-page configuration system. You can control layout, navigation, UI components, and feature flags right inside each MDX document — no external config files needed.
This is powerful because the configuration lives next to the content. You can see exactly how a page behaves by reading its frontmatter. No jumping between files.
Layout Selection
The layout field picks which template wraps your content. Different page types get different visual structures, but they all go through the same content pipeline.
---
# Layout options (defined by your application)
layout: "docs" # Documentation with sidebar
# layout: "blog" # Blog post with author info, date
# layout: "landing" # Full-width, no chrome
# layout: "tutorial" # Step-by-step with progress
# layout: "reference" # API reference with type tables
# layout: "minimal" # Content only, no nav/footer
# layout: "fullscreen" # Presentation-style
---Implementing Layout Selection
Here's how you'd wire up layout switching in different frameworks.
Next.js (App Router):
Docusaurus:
---
# Docusaurus built-in layout options
sidebar_class_name: "custom-sidebar"
hide_title: false
hide_table_of_contents: false
# Custom page class for CSS targeting
custom_edit_url: "https://github.com/org/repo/edit/main/docs/page.mdx"
---Gatsby:
Sidebar Configuration
You can control sidebar visibility, position, grouping, and ordering from each document's frontmatter. No central sidebar config file needed.
---
sidebar:
# Display in sidebar? (false to hide from nav while keeping page accessible)
visible: true
# Sidebar label (shorter than page title)
label: "Auth Setup"
# Position within its group (lower = higher)
order: 3
# Sidebar group/category
group: "Getting Started"
# Group order (for sorting groups themselves)
groupOrder: 1
# Sidebar position on page
position: "left" # "left", "right", "none"
# Collapsible section behavior
collapsed: false # Start collapsed?
collapsible: true # Can it be collapsed?
# Icon for visual distinction
icon: "🔐"
# Badge for status indicators
badge:
text: "New"
color: "green"
---Building a Dynamic Sidebar
Here's how you'd read all the sidebar frontmatter and build a grouped, sorted navigation:
Docusaurus Sidebar Configuration
---
# Docusaurus-specific sidebar fields
sidebar_position: 3
sidebar_label: "Authentication"
sidebar_class_name: "sidebar-auth"
sidebar_custom_props:
icon: "🔐"
description: "Learn how to authenticate API requests"
---Navigation Order
Control how pages show up in nav menus, breadcrumbs, and prev/next pagination.
---
# Explicit ordering
nav:
order: 5 # Position in nav menu
weight: 5 # Alternative naming convention
# Breadcrumb path
breadcrumb:
- label: "Documentation"
href: "/docs"
- label: "API Reference"
href: "/docs/api"
- label: "Authentication"
# Previous/Next links (manual override)
prev:
title: "Getting Started"
href: "/docs/api/getting-started"
next:
title: "Endpoints"
href: "/docs/api/endpoints"
# Navigation menu visibility
showInMainNav: true
showInFooterNav: false
# Dropdown grouping
menuGroup: "API"
menuGroupOrder: 2
---Sequential Navigation Component
This component uses explicit prev/next if defined, or auto-determines them from page order:
Table of Contents Settings
Configure the auto-generated TOC on a per-page basis. Some pages need a deep TOC, others don't need one at all.
---
toc:
# Enable/disable TOC for this page
enabled: true
# Heading levels to include
minDepth: 2 # Start at h2 (skip h1 title)
maxDepth: 4 # Include h2, h3, h4
# TOC position
position: "right" # "right", "inline", "floating", "none"
# Behavior
sticky: true # Stick to viewport on scroll
highlightActive: true # Highlight current section
collapsible: false # Allow collapsing
# Display
title: "On This Page" # Custom TOC heading
ordered: false # Use numbered list
# Exclude specific headings
exclude:
- "References"
- "Changelog"
---Framework Implementations
Docusaurus:
---
# Docusaurus TOC configuration
hide_table_of_contents: false
toc_min_heading_level: 2
toc_max_heading_level: 4
---Next.js (custom implementation):
Pagination Configuration
Control how long content gets split across pages or how posts appear in series.
---
pagination:
# Enable pagination for long content
enabled: true
# Manual page breaks (split single MDX into multiple pages)
splitAtHeadings: true
maxSectionsPerPage: 5
# Series pagination
series: "React Fundamentals"
seriesOrder: 3
totalInSeries: 8
# Content list pagination (for index pages)
itemsPerPage: 10
showPageNumbers: true
infiniteScroll: false
---Draft Mode
Control content visibility and preview capabilities right from frontmatter.
Draft Filtering in Build
Here's a real filtering function that handles drafts, scheduled posts, and expired content:
Custom CSS and Classes
Need page-specific styles without touching global stylesheets? Frontmatter can handle that too.
---
# Page-level CSS customization
style:
# CSS class applied to page wrapper
className: "api-docs dark-theme"
# Custom CSS variables for this page
variables:
--accent-color: "#e63946"
--content-max-width: "900px"
--font-code: "JetBrains Mono"
# Layout overrides
fullWidth: true
maxWidth: "1400px"
contentPadding: "2rem"
# Background
backgroundColor: "#fafafa"
backgroundImage: "/images/patterns/grid.svg"
# Typography
fontSize: "base" # "sm", "base", "lg"
lineHeight: "relaxed" # "tight", "normal", "relaxed"
# Body class (useful for CSS targeting)
bodyClass: "page-api-auth"
# Custom stylesheet import
customCSS: "/styles/api-docs.css"
---Applying Custom Styles
Feature Flags Per Page
Turn interactive features on or off for individual pages. Great for A/B testing, progressive rollouts, or just disabling stuff that doesn't make sense on certain pages.
---
features:
# Interactive elements
codePlayground: true # Embedded code sandbox
liveEditor: true # Edit-in-place for code blocks
copyButton: true # Copy code button
# UI components
feedbackWidget: true # "Was this helpful?" widget
commentSection: false # Reader comments
shareButtons: true # Social share buttons
editOnGithub: true # "Edit this page" link
# Navigation
breadcrumbs: true
pageProgress: true # Reading progress bar
scrollToTop: true # Scroll-to-top button
# Experimental
aiAssistant: false # AI chat helper
searchHighlight: true # Highlight search terms
readingMode: true # Distraction-free toggle
# Analytics
trackScrollDepth: true
trackTimeOnPage: true
heatmapEnabled: false
# Access control
requireAuth: false
premiumOnly: false
membershipLevel: "free" # "free", "pro", "enterprise"
---Conditional Feature Rendering
A simple FeatureGate component that checks frontmatter before rendering:
Complete Configuration Schema
Here's everything tied together in one Zod schema:
Best Practices
- Set sensible defaults — Most pages should work with zero configuration. Only require fields when you truly need per-page values.
- Use layout inheritance — Define base layouts with default settings, then let frontmatter override specific things.
- Keep feature flags boolean — Simple on/off. If you need complex conditional logic, put it in code, not YAML.
- Document your options — Maintain a reference page listing all config fields, valid values, and what they do.
- Validate at build time — Catch invalid layout names, bad order values, and unsupported feature flags before you deploy.
- Group related fields — Use nested objects (
sidebar.*,toc.*,nav.*) instead of cluttering the top level with dozens of flat keys.
- Support gradual adoption — Let pages work with zero config. Authors add fields as they need customization, not before.
- Show active config in dev mode — Build a dev-only panel that displays the current page's resolved configuration. Makes debugging layout issues way easier.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Page Configuration via Frontmatter.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this MDX Tutorial topic.
Search Terms
mdx-tutorial, mdx tutorial, mdx, tutorial, frontmatter, page, configuration, page configuration via frontmatter
Related MDX Tutorial Topics