Skip to content

Commit df94c22

Browse files
gillkyleMarcy Sutton
authored and
Marcy Sutton
committed
docs(www): 25 Workflows - Embedding components in Markdown with MDX (#14543)
* chore: updates to docs to improve examples and provide more detailed explanations * fix: simple grammatcial changes * fix: PR review suggestions * fix: correct code block titles and update example inline jsx * Amberleys PR comments * feat: add mdx as a title to code blocks * add link to glossary for components to help disambiguate * Update docs/docs/mdx/index.md Co-Authored-By: Marcy Sutton <[email protected]> * Update docs/docs/mdx/index.md Co-Authored-By: Marcy Sutton <[email protected]> * Update www/src/data/sidebars/doc-links.yaml Co-Authored-By: Marcy Sutton <[email protected]> * local merge conflicts + github suggestions = confusion
1 parent decdd8e commit df94c22

8 files changed

+161
-60
lines changed

docs/docs/adding-markdown-pages.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ plugins: [
7171
Create a folder in the `/src` directory of your Gatsby application called `markdown-pages`.
7272
Now create a Markdown file inside it with the name `post-1.md`.
7373

74-
#### Including frontmatter
74+
#### Frontmatter for metadata in markdown files
7575

76-
When you create a Markdown file, at the top of the file, add the frontmatter (metadata) block below. You can have different key value pairs that are relevant to your website. This block will be parsed by `gatsby-transformer-remark` as `frontmatter`. The GraphQL API will provide this data in your React components.
76+
When you create a Markdown file, you can include a set of key value pairs that can be used to provide additional data relevant to specific pages in the GraphQL data layer. This data is called frontmatter and is denoted by the triple dashes at the start and end of the block. This block will be parsed by `gatsby-transformer-remark` as `frontmatter`. The GraphQL API will provide the key value pairs as data in our React components.
7777

7878
```markdown:title=src/markdown-pages/post-1.md
7979
---

docs/docs/mdx/getting-started.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Getting Started
2+
title: Getting Started with MDX
33
---
44

55
The fastest way to get started with Gatsby + MDX is to use the [MDX
@@ -19,11 +19,10 @@ your site.
1919

2020
```sh
2121
cd my-mdx-starter/
22-
npm install
2322
gatsby develop
2423
```
2524

26-
1. **Open the site** running at http://localhost:8000!
25+
1. **Open the site** running at http://localhost:8000
2726

2827
1. **Update the MDX content** by opening the `my-mdx-starter` directory
2928
in your code editor of choice and edit `src/pages/index.mdx`.
@@ -37,27 +36,27 @@ can follow these steps for configuring the [gatsby-mdx](/packages/gatsby-mdx/) p
3736
1. **Add `gatsby-mdx`** and MDX as dependencies
3837

3938
```sh
40-
yarn add gatsby-mdx @mdx-js/mdx @mdx-js/react
39+
yarn add gatsby-plugin-mdx @mdx-js/mdx @mdx-js/react
4140
```
4241

43-
If you're upgrading from v0, [check out the MDX migration guide](https://mdxjs.com/migrating/v1).
42+
> **Note:** If you're upgrading from v0, additionally [check out the MDX migration guide](https://mdxjs.com/migrating/v1).
4443
45-
1. **Update your `gatsby-config.js`** to use the `gatsby-mdx` plugin
44+
1. **Update your `gatsby-config.js`** to use `gatsby-plugin-mdx`
4645

4746
```javascript:title=gatsby-config.js
4847
module.exports = {
4948
plugins: [
5049
// ....
51-
`gatsby-mdx`,
50+
`gatsby-plugin-mdx`,
5251
],
5352
}
5453
```
5554

5655
1. **Restart `gatsby develop`** and add an `.mdx` page to `src/pages
5756

58-
**Note:** If you want to query for frontmatter, exports, or other fields like
59-
`tableOfContents` and you haven't previously added a `gatsby-source-filesystem`
60-
pointing at `src/pages` in your project, you'll want to add one now.
57+
> **Note:** If you want to query for frontmatter, exports, or other fields like
58+
> `tableOfContents` and you haven't previously added a `gatsby-source-filesystem`
59+
> pointing at `src/pages` in your project, you'll want to add one now.
6160
6261
## What's next?
6362

docs/docs/mdx/index.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: Adding Components to Content with MDX
2+
title: Adding Components to Markdown with MDX
33
---
44

5-
When writing long-form content in Markdown you might want to embed components.
5+
When writing long-form content in Markdown you might want to embed [components](/docs/glossary/#component).
66
This is often achieved by either writing content in JSX or using plugins that
77
use custom syntax. The first approach isn't optimal because JSX isn't the best
88
format for content and can make it less approachable to members of a team. Custom
@@ -15,8 +15,8 @@ you're finding yourself wanting to add components to your content you can use
1515
[MDX][mdx] is Markdown for the component era.
1616
It lets you write JSX embedded inside Markdown.
1717
It’s a great combination because it allows you to use Markdown’s terse
18-
syntax (such as `# Heading`) for your content and JSX for more advanced
19-
components.
18+
syntax (such as `# Heading`) for your content and JSX for more advanced,
19+
or reusable components.
2020

2121
This is useful in content-driven sites where you want the ability
2222
to introduce components like charts or alerts without having to
@@ -27,14 +27,13 @@ interactions.
2727

2828
When using MDX you can also import other MDX documents and render
2929
them as components. This lets you write something like an FAQ
30-
page in one place and render it throughout your website.
30+
page in one place and reuse it throughout your website.
3131

3232
## What does it look like in practice?
3333

34-
MDX might seem weird at first, but it quickly feels natural
35-
after working with it for a few minutes. Importing and JSX
36-
syntax works just like in your components. This results in a
37-
seamless experience for developers and content authors alike.
34+
Importing and JSX syntax works just like it does in your components. This
35+
results in a seamless experience for developers and content authors alike.
36+
Markdown and JSX are included alongside each other like this:
3837

3938
```md
4039
import { Chart } from '../components/chart'

docs/docs/mdx/programmatically-creating-pages.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ root.
3232

3333
> **NOTE**: `gatsby-mdx` uses `.mdx` by default as a file extension to
3434
> recognize which files to use. You can also [use `.md` as a file
35-
> extension](api-reference/options/extensions) if you want.
35+
> extension](https://gatsby-mdx.netlify.com/api-reference/options/extensions) if you want.
3636
3737
```javascript=gatsby-config.js
3838
module.exports = {
@@ -54,7 +54,7 @@ module.exports = {
5454
```
5555

5656
You can read about
57-
[`gatsby-source-filesystem`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem#readme)
57+
[`gatsby-source-filesystem`](/packages/gatsby-source-filesystem)
5858
if you'd like to learn more.
5959

6060
## Add MDX Files

docs/docs/mdx/writing-pages.md

+129-32
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
title: Writing Pages in MDX
33
---
44

5-
After [installing](/docs/mdx/getting-started) the plugin, MDX files
6-
written in `src/pages` will turn into pages. This happens because
7-
`gatsby-mdx` looks for MDX files and automatically transpiles them
8-
so that Gatsby internals can render them.
5+
After [installing](/docs/mdx/getting-started) `gatsby-plugin-mdx`, MDX files
6+
located in `src/pages` will turn into pages.
97

108
Pages are rendered at a URL that is constructed from the filesystem
119
path inside `src/pages`. An MDX file at `src/pages/awesome.mdx` will
1210
result in a page being rendered at `mysite.com/awesome`.
1311

14-
By default, gatsby-mdx supports frontmatter so you can define things
15-
like titles and paths to use in your GraphQL queries.
12+
> `gatsby-plugin-mdx` looks for MDX files and automatically
13+
> transpiles them so that Gatsby internals can render them.
1614
17-
## Frontmatter
15+
## Using frontmatter in MDX
1816

19-
You can declare frontmatter at the beginning of your MDX document:
17+
By default, `gatsby-plugin-mdx` supports [frontmatter](/docs/adding-markdown-pages/#frontmatter-for-metadata-in-markdown-files)
18+
so you can define things like titles and paths to use in your GraphQL
19+
queries. You can declare frontmatter at the beginning of your MDX document:
2020

21-
```md
21+
```mdx
2222
---
2323
title: Hello, world!
2424
path: /hello-world
@@ -30,17 +30,14 @@ date: 2019-01-29
3030

3131
Which can then be [queried with GraphQL](/docs/querying-with-graphql/):
3232

33-
**Note:** To query `Mdx` content, it must be included in the node system using a
34-
source like `gatsby-source-filesystem` first.
35-
3633
```graphql
3734
query {
3835
allMdx {
3936
edges {
4037
node {
4138
frontmatter {
42-
path
4339
title
40+
path
4441
date(formatString: "MMMM DD, YYYY")
4542
}
4643
}
@@ -49,14 +46,34 @@ query {
4946
}
5047
```
5148

52-
## Importing components
49+
> **Note:** To query `Mdx` content, it must be included in the node system using a
50+
> source like the `gatsby-source-filesystem` plugin first. Instructions for sourcing
51+
> content from somewhere like your `/src/pages` directory can be found on the [plugin's README](/packages/gatsby-source-filesystem/).
52+
53+
Frontmatter is also available in `props.pageContext.frontmatter` and
54+
can be accessed in blocks of JSX in your MDX document:
55+
56+
```mdx
57+
---
58+
title: "Building with Gatsby"
59+
author: "Jay Gatsby"
60+
---
61+
62+
<h1>{props.pageContext.frontmatter.title}</h1>
63+
64+
<span>{props.pageContext.frontmatter.author}</span>
65+
66+
(Blog post content, components, etc.)
67+
```
5368

54-
Similarly to what you'd do in JSX, you can import and render components
55-
with JSX. You can also import other MDX documents.
69+
## Importing JSX components and MDX documents
5670

57-
```md
71+
Similarly to what you'd do in plain React, you can import and render JSX components
72+
directly in MDX files. You can also import other MDX documents.
73+
74+
```mdx:title=src/pages/chart.mdx
5875
import { Chart } from "../components/chart"
59-
import FAQ from "../content/faq.mdx"
76+
import FAQ from "../components/faq.mdx"
6077

6178
# Here’s a chart
6279

@@ -67,47 +84,127 @@ The chart is rendered inside our MDX document.
6784
<FAQ />
6885
```
6986

70-
## Exports
87+
The `<Chart />` component coming from a `.js` file would be written like any
88+
other React component, while the `<FAQ />` component coming from an `.mdx`
89+
file might look something like this:
90+
91+
<!-- prettier-ignore -->
92+
```mdx:title=src/components/faq.mdx
93+
## Frequently Asked Questions
94+
95+
### Why Gatsby?
96+
97+
Gatsby delivers faster, more secure sites and apps from a variety of data
98+
sources
99+
100+
### Where do I start?
101+
102+
The documentation offers guides for all different skill levels, you can
103+
find more info at the Gatsby's [Quick Start page](https://www.gatsbyjs.org/docs/quick-start)
104+
105+
<!-- This default export overrides the default layout ensuring -->
106+
<!-- that the FAQ component isn't wrapped by other elements -->
107+
export default ({ children }) => (
108+
<>
109+
{children}
110+
</>
111+
)
112+
```
113+
114+
> **Note**: the default export concept used in this code block is explained in more detail
115+
> in the docs below on [defining layouts](#defining-a-layout)
116+
117+
## Using JavaScript exports
118+
119+
MDX supports `export` syntax as well, which enables specific use cases like providing data
120+
for queries and rendering or overriding the default layout on MDX documents. You
121+
don't need to export MDX documents to import them in other files.
122+
123+
### Exporting page metadata
71124

72-
MDX supports `export` syntax as well which allows you to export metadata
73-
about a given document. gatsby-mdx will automatically add it to the
74-
GraphQL schema so you can use the exported data in your queries and
75-
rendering.
125+
You can provide additional data about a given document by exporting.
126+
`gatsby-plugin-mdx` will automatically add it to the GraphQL schema so you
127+
can use the exported data in your queries and in rendering.
76128

129+
Data exported in MDX documents in this manner is also made available on the
130+
variable name you've assigned it.
131+
132+
You can export variables, objects, or other data structures:
133+
134+
<!-- prettier-ignore -->
77135
```mdx
78136
export const metadata = {
79137
name: "World",
80138
path: "/world",
81-
}
139+
};
140+
141+
# Hello, <span children={metadata.name} />
142+
143+
The span above will read: "Hello, World".
144+
145+
<!-- you can also use other variables or data structures -->
146+
export const names = ["Abdullah", "Adam", "Alice", "Aida"]
82147

83-
# Hello, <span>{props.metadata.name}</span>
148+
<ul>{names.map(name => <li>{name}</li>)}</ul>
149+
```
150+
151+
The fields `name` and `path` defined on `metadata` could now alternatively
152+
be accessed on MDX nodes in other areas of your Gatsby project by a GraphQL
153+
query like this (this query fetches all MDX nodes and the data exports
154+
associated with them):
84155

85-
The heading above will say "Hello, World".
156+
```graphql
157+
query MdxExports {
158+
allMdx {
159+
nodes {
160+
exports {
161+
metadata {
162+
name
163+
path
164+
}
165+
}
166+
}
167+
}
168+
}
86169
```
87170

88171
### Defining a layout
89172

90-
You can specify the layout that will wrap your component using the
91-
default export.
173+
If you have [provided a default layout](/packages/gatsby-plugin-mdx/?=mdx#default-layouts) in your `gatsby-config.js`
174+
through the `gatsby-plugin-mdx` plugin options, the exported component you define
175+
from this file will replace the default.
92176

93-
```md
177+
<!-- prettier-ignore -->
178+
```mdx:title=src/pages/layout-example.mdx
94179
import PurpleBorder from "../components/purple-border"
95180

96181
# This will have a purple border
97182

98183
export default PurpleBorder
99184
```
100185

186+
The `<PurpleBorder />` component might look something like this, wrapping the MDX
187+
document in a `<div>` with a 1px purple border:
188+
189+
```jsx:title=src/components/purple-border.js
190+
import React from "react"
191+
192+
const PurpleBorder = ({ children }) => (
193+
<div style={{ border: "1px solid rebeccapurple" }}>{children}</div>
194+
)
195+
196+
export default PurpleBorder
197+
```
198+
101199
## GraphQL Queries
102200

103201
You can fetch data to use in your MDX file by exporting a `pageQuery`
104202
in the same way you would for a `.js` page. The queried data is passed
105203
as a prop, and can be accessed inside any JSX block when writing in
106204
MDX:
107205

108-
<!-- This is invalid JSX; prettier has a bug with this code snippet -->
109-
110-
```jsx
206+
<!-- prettier-ignore -->
207+
```mdx
111208
import { graphql } from "gatsby"
112209

113210
# My Awesome Page

docs/tutorial/writing-documentation-with-docz.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = {
4444

4545
Docz searches your directories for `mdx` files and renders them. Create a `docs` folder at the root of your project. Place an `index.mdx` file inside this directory with the following content:
4646

47-
```md:title=docs/index.mdx
47+
```mdx:title=docs/index.mdx
4848
---
4949
name: Getting Started
5050
route: /
@@ -102,7 +102,7 @@ The button will display its text by default with a `font-size` of `18px` however
102102
103103
Create a new file in the `docs` directory to document your newly created button component. Call the file `button.mdx`:
104104

105-
```md:title=docs/button.mdx
105+
```mdx:title=docs/button.mdx
106106
---
107107
name: Button
108108
menu: Components
@@ -115,7 +115,7 @@ Buttons make common actions more obvious and help users more easily perform them
115115

116116
Docz offers some internal components you can use to display the component and its properties. Import both these and your component itself into the document and use them:
117117

118-
```md:title=docs/button.mdx
118+
```mdx:title=docs/button.mdx
119119
---
120120
name: Button
121121
menu: Components

www/src/data/sidebars/doc-links.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@
218218
link: /docs/tailwind-css/
219219
- title: Using Bulma
220220
link: /docs/bulma/
221-
- title: Adding Components to Content with MDX
221+
- title: Adding Components to Markdown with MDX
222222
link: /docs/mdx/
223223
items:
224-
- title: Getting Started
224+
- title: Getting Started with MDX
225225
link: /docs/mdx/getting-started/
226226
- title: Writing Pages
227227
link: /docs/mdx/writing-pages/

0 commit comments

Comments
 (0)