Getting Started
#
Table of contents#
Hello WorldThe smallest MDX example looks like this:
It displays a heading saying “Hello, world!” on the page. You could also write it like so:
This displays the same heading.
#
SyntaxMDX syntax can be boiled down to being JSX in Markdown. It’s a superset of Markdown syntax that also supports importing, exporting, and JSX.
#
MarkdownTraditionally, Markdown is used to generate HTML. Many developers like writing markup in Markdown as it often looks more like what’s intended and it is typically terser. Instead of the following HTML:
You can write the equivalent in Markdown (or MDX) like so:
Markdown is good for content. MDX supports standard Markdown syntax. It’s important to understand Markdown in order to learn MDX.
#
JSXRecently, more and more developers have started using JSX to generate HTML markup. JSX is typically combined with a frontend framework like React or Vue. These frameworks add support for components, which let you change repeating things like the following markup:
…to JSX (or MDX) like this:
JSX is good for components. It makes repeating things more clear and allows for separation of concerns. MDX fully supports JSX syntax. Any line that start with the <
character starts a JSX block.
#
MDXWe love HTML, but we’ve created MDX to let you combine the benefits of Markdown with the benefits of JSX. The following example shows how they can be combined. It’s interactive so go ahead and change the code!
Below is a JSX block:
MDX supports two more features: imports and exports.
#
Importsimport
(ES2015)can be used to import components, data, and documents.
#
ComponentsYou can import components, such as your own or from rebass, like so:
#
DataYou can also import data that you want to display:
#
DocumentsYou can embed MDX documents in other documents. This is also known as transclusion. You can achieve this by importing an .mdx
(or .md
) file:
#
Exportsexport
(ES2015) can be used to export data and components. For example, you can export metadata like which layout to use or the authors of a document. It’s a mechanism for an imported MDX file to communicate with the thing that imports it.
Say we import our MDX file, using webpack and React, like so:
And our MDX file looks as follows (note the export
):
After bundling and evaluating, we could get something like this:
This is similar to what frontmatter allows in Markdown, but instead of supporting only data in something like YAML, MDX lets you use richer JavaScript structures.
#
Defining variables with exportsIf you need to define a variable in your MDX document, you can use an export to do so. Not only do exports emit data, they instantiate data you can reference in JSX blocks:
#
Working with componentsIn addition to rendering components inline, you can also pass in components to be used instead of the default HTML elements that Markdown compiles to.
This allows you to use your existing components and even CSS-in-JS like styled-components
.
The components
object is a mapping between the HTML name and the desired component you’d like to render.
You can also import your components from another location like your UI library:
With the above, the Heading
component will be rendered for any h1
, Text
for p
elements, and so on.
In addition to HTML elements, there is one special mapping: inlineCode
can be used for code inside paragraphs, tables, etc.
See the Table of components for supported names.
#
MDXProviderIf you’re using an app layout that wraps your application, you can use the MDXProvider
to only pass your components in one place:
This allows you to remove duplicated component passing and importing. It will typically go in layout files.
#
Using the wrapperThe MDXProvider has a special wrapper
key that you can use in the component mapping.
With your wrapper component you can set the layout of your document, inject styling, or even manipulate the children passed to the component.
#
Default exportsSometimes from an MDX file you might want to override the wrapper. This is especially useful when you want to override layout for a single entry point at the page level. To achieve this you can use the ES default export and it will wrap your MDX document instead of the wrapper passed to MDXProvider.
You can declare a default export as a function:
Or directly as a component:
Either works. Whatever you prefer!