|
| 1 | +--- |
| 2 | +title: MDX |
| 3 | +disableTableOfContents: true |
| 4 | +--- |
| 5 | + |
| 6 | +Learn what MDX is, and how you can use it when creating content for your Gatsby site. |
| 7 | + |
| 8 | +## What is MDX? |
| 9 | + |
| 10 | +[MDX](/docs/glossary/#mdx) is an extension to [Markdown](/docs/glossary/markdown/) that lets you include [JSX](/docs/glossary/#jsx) in Markdown documents. MDX makes it possible to include React components in your Gatsby blog posts and pages. |
| 11 | + |
| 12 | +Markdown defines a plain text syntax for HTML elements such as `h1`, `strong`, and `a`, but still supports inline HTML. An example Markdown document follows. |
| 13 | + |
| 14 | +```markdown |
| 15 | +# Hello world! |
| 16 | + |
| 17 | +You can use Markdown to create documents for [Gatsby](https://www.gatsbyjs.org/). |
| 18 | + |
| 19 | +<figure class="chart"> |
| 20 | + <object data="chart.svg" type="image/svg+xml"></object> |
| 21 | + <figcaption>MDX adoption has increased 120% since last year.</figcaption> |
| 22 | +</figure> |
| 23 | +``` |
| 24 | + |
| 25 | +MDX takes this one step further, and makes it possible to use JSX in your Markdown documents. Try making the `figure` element into a `Figure` component. |
| 26 | + |
| 27 | +```jsx |
| 28 | +export const Figure = props => { |
| 29 | + return ( |
| 30 | + <figure class="chart"> |
| 31 | + <object data={props.data} type="image/svg+xml"></object> |
| 32 | + <figcaption>{props.caption}</figcaption> |
| 33 | + </figure> |
| 34 | + ) |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Now you can import this component into your Markdown document. |
| 39 | + |
| 40 | +```markdown |
| 41 | +import { Figure } from './components/Figure'; |
| 42 | + |
| 43 | +# Hello world! |
| 44 | + |
| 45 | +You can use Markdown to create documents for [Gatsby](https://www.gatsbyjs.org/). |
| 46 | + |
| 47 | +<Figure data="chart.svg" caption="MDX adoption has increased 120% since last year." /> |
| 48 | +``` |
| 49 | + |
| 50 | +If you're creating a Gatsby site from scratch, the [gatsby-starter-mdx-basic](https://github.com/ChristopherBiscardi/gatsby-starter-mdx-basic) is the fastest way to add MDX support. Use the Gatsby [CLI](/docs/glossary/#cli) to create a new project with this starter as a base. |
| 51 | + |
| 52 | +```shell |
| 53 | +gatsby new my-mdx-starter https://github.com/ChristopherBiscardi/gatsby-starter-mdx-basic |
| 54 | +``` |
| 55 | + |
| 56 | +To use MDX with an existing Gatsby site, add the [`gatsby-plugin-mdx`](/packages/gatsby-plugin-mdx/?=gatsby-plugin-mdx) plugin. As with Gatsby itself, you can install it using [npm](/docs/glossary/#npm). You'll also need to install MDX itself, and the React implementation of MDX. |
| 57 | + |
| 58 | +```shell |
| 59 | +npm install --save gatsby-plugin-mdx @mdx-js/mdx @mdx-js/react |
| 60 | +``` |
| 61 | + |
| 62 | +Then add `gatsby-plugin-mdx` to your plugins list in `gatsby-config.js`, and set the [configuration options](/packages/gatsby-plugin-mdx/?=gatsby-plugin-mdx#configuration) you prefer. |
| 63 | + |
| 64 | +MDX enhances Markdown's capabilities so that you can use React components anywhere in your Gatsby-powered site. |
| 65 | + |
| 66 | +### Learn more about MDX |
| 67 | + |
| 68 | +- [MDX](https://mdxjs.com/) official site |
| 69 | +- [What is MDX](https://www.youtube.com/watch?v=d2sQiI5NFAM) (video) |
| 70 | +- [Adding Components to Markdown with MDX](/docs/mdx/) from the Gatsby docs |
| 71 | +- [Introducing JSX](https://reactjs.org/docs/introducing-jsx.html) from the React documentation |
0 commit comments