MDX Documentation
MDX is an authorable format that lets you seamlessly write JSX in your Markdown documents.
You can import components, such as interactive charts or alerts, and embed them within your content.
This makes writing long-form content with components a blast 🚀.
more: mdxjs
When creating MDX documentation files, you can include a set of key-value pairs that can be used to provide structured data. The format used is known as frontmatter
---title: My Documentroute: /tutorials/mdx-documentation/type: tutorialtags:- mdx- documentationorder: 0---
In MDX, you can import any React components and use them to create rich documentation pages:
---title: My Button---import { Button } from 'theme-ui';<Button onClick={() => alert('clicked')}>click me</Button>
mdxjs allows you to import external
.mdx
files statically and display their content:---title: Test Transclusion---import Transclusion from '../sections/transclusion.mdx';<Transclusion />
By default,
.md
files are not transformed to react components - they are rather loaded with raw-loader and are translated as a string.You can still display
.md
files using our <Markdown />
component:---title: Test Transclusion---import { Markdown } from '@component-controls/components';import changelog from '../../../CHANGELOG.md';<Markdown>{changelog}</Markdown>
If you prefer to compile
.md
files with mdxjs
and use transclusion, you can set up a custom webpack config:.config/buildtime.js
module.exports = {webpack: (config = {}) => {return {...config,module: {...config.module,rules: [...config.module.rules.filter(rule => !rule.test.test('.md')),{test: /\.md$/i,use: [{loader: 'babel-loader',options: {presets: ['@babel/preset-env', '@babel/preset-react'],},},'@mdx-js/loader',],},],},};},};
And then use the imported markdown file as a react component:
---title: Test Transclusion---import Changelog from '../../../CHANGELOG.md';<Changelog />
You can include source code in your MDX and MD documentation and add some useful parameters
The language can be specified as the first parameter
```jsximport { Button } from 'theme-ui';```
A title attribute can be added. Due to some current MDX limitations, the title can not contain spaces.
my-title
```jsx:title=my-titleimport { Button } from 'theme-ui';```
You can specify a line to highlight
```jsx {2}import { Button } from 'theme-ui';<Button>click me </Button>;```
Or a range of lines
```jsx {2-3}import { Button } from 'theme-ui';<Button>click me </Button>;```
You can use
:emoji:
in your documents:rocket: :dog: :+1:
= 🚀 🐶 👍