Skip to content

Commit c92db36

Browse files
axe312gerimjoshinLekoArtswardpeetpieh
authored
BREAKING CHANGE(gatsby-plugin-mdx): MDX v2 (#35650)
Co-authored-by: Josh Johnson <[email protected]> Co-authored-by: LekoArts <[email protected]> Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Lennart <[email protected]> Co-authored-by: pieh <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 3c0dd6d commit c92db36

File tree

200 files changed

+5763
-8418
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+5763
-8418
lines changed

.circleci/config.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,12 @@ jobs:
434434
test_path: e2e-tests/themes
435435
test_command: cd production-runtime; yarn test
436436

437-
mdx_e2e_tests:
437+
e2e_tests_mdx:
438438
<<: *e2e-executor
439439
steps:
440440
- e2e-test:
441441
test_path: e2e-tests/mdx
442442
test_command: CYPRESS_PROJECT_ID=spbj28 CYPRESS_RECORD_KEY=af30ea46-121f-4fb7-97dd-f17ec224402e yarn test
443-
- e2e-test:
444-
test_path: e2e-tests/mdx-less-babel
445-
test_command: CYPRESS_PROJECT_ID=xo139a CYPRESS_RECORD_KEY=74d485b5-3ba6-40e1-a7d4-e63c0a8d2a4d yarn test
446443

447444
e2e_tests_visual-regression:
448445
<<: *e2e-executor
@@ -646,6 +643,8 @@ workflows:
646643
<<: *e2e-test-workflow
647644
- e2e_tests_contentful:
648645
<<: *e2e-test-workflow
646+
- e2e_tests_mdx:
647+
<<: *e2e-test-workflow
649648
- e2e_tests_trailing-slash:
650649
<<: *e2e-test-workflow
651650
- e2e_tests_development_runtime:
@@ -660,8 +659,6 @@ workflows:
660659
<<: *e2e-test-workflow
661660
- themes_e2e_tests_development_runtime:
662661
<<: *e2e-test-workflow
663-
- mdx_e2e_tests:
664-
<<: *e2e-test-workflow
665662
- starters_validate:
666663
<<: *ignore_master
667664
- starters_publish:

benchmarks/gabe-fs-mdx/gatsby-config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require("path")
2+
13
module.exports = {
24
siteMetadata: {
35
siteTitle: `Gatsby MDX Benchmark`,
@@ -12,9 +14,7 @@ module.exports = {
1214
},
1315
{
1416
resolve: `gatsby-plugin-mdx`,
15-
options: {
16-
lessBabel: true,
17-
},
1817
},
19-
],
18+
!process.env.CI && `gatsby-plugin-webpack-bundle-analyser-v2`,
19+
].filter(Boolean),
2020
}

benchmarks/gabe-fs-mdx/gatsby-node.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const path = require("path")
22

3-
const blogPost = path.resolve(`./src/templates/blog-post.js`)
4-
53
exports.createPages = async ({ graphql, actions }) => {
64
const { createPage } = actions
75

@@ -14,6 +12,13 @@ exports.createPages = async ({ graphql, actions }) => {
1412
frontmatter {
1513
title # used in prev/next
1614
}
15+
parent {
16+
... on File {
17+
id
18+
name
19+
absolutePath
20+
}
21+
}
1722
}
1823
}
1924
}
@@ -25,13 +30,15 @@ exports.createPages = async ({ graphql, actions }) => {
2530

2631
const posts = result.data.allMdx.nodes
2732

28-
posts.forEach(({ id, slug }, index) => {
33+
posts.forEach(({ id, slug, parent }, index) => {
2934
const previous = index === posts.length - 1 ? null : posts[index + 1]
3035
const next = index === 0 ? null : posts[index - 1]
3136

3237
createPage({
3338
path: slug,
34-
component: blogPost,
39+
component: `${path.resolve(
40+
`./src/templates/blog-post.js`
41+
)}?__contentFilePath=${parent.absolutePath}`,
3542
context: {
3643
id,
3744
slug,

benchmarks/gabe-fs-mdx/gen.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ date: ${faker.date.recent(1000).toISOString().slice(0, 10)}
1919
import { Link } from "gatsby"
2020
2121
export const author = "Fred Flintstone"
22-
export default props => <main {...props} />
2322
2423
<Link to="/">Go Home</Link>
2524

benchmarks/gabe-fs-mdx/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"format": "prettier --write \"**/*.{js,jsx,json,md}\""
1414
},
1515
"devDependencies": {
16+
"gatsby-plugin-webpack-bundle-analyser-v2": "^1.1.27",
1617
"prettier": "2.0.4"
1718
},
1819
"repository": {
@@ -28,13 +29,12 @@
2829
"mdx"
2930
],
3031
"dependencies": {
31-
"@mdx-js/mdx": "^1",
32-
"@mdx-js/react": "^1",
32+
"@mdx-js/react": "^2",
3333
"faker": "^4.1.0",
34-
"gatsby": "^2",
35-
"gatsby-plugin-mdx": "^1",
36-
"gatsby-source-filesystem": "^2",
37-
"react": "^16.12.0",
38-
"react-dom": "^16.12.0"
34+
"gatsby": "^4",
35+
"gatsby-plugin-mdx": "^3",
36+
"gatsby-source-filesystem": "^4",
37+
"react": "^17",
38+
"react-dom": "^17"
3939
}
4040
}

benchmarks/gabe-fs-mdx/src/templates/blog-post.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
import React from "react"
22
import { graphql, Link } from "gatsby"
33
import Layout from "../components/layout_1"
4-
import { MDXRenderer } from "gatsby-plugin-mdx"
5-
6-
const Article = ({ data }) => {
7-
const { body } = data.mdx
8-
const { mdx } = data
94

5+
const Article = ({ children, data: { mdx } }) => {
106
return (
117
<Layout>
128
<Link to="/">Go back to index page</Link>
139
<div>
1410
<h1>{mdx.frontmatter.title}</h1>
15-
<div>
16-
<MDXRenderer>{body}</MDXRenderer>
17-
</div>
11+
{children}
1812
</div>
1913
</Layout>
2014
)
2115
}
2216

23-
export const query = graphql`
24-
query MdxQuery($id: String!) {
17+
export const pageQuery = graphql`
18+
query($id: String!) {
2519
mdx(id: { eq: $id }) {
26-
body
20+
id
2721
frontmatter {
2822
title
2923
}

docs/docs/how-to/routing/mdx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Add components to content using MDX
33
examples:
44
- label: Using MDX
5-
href: "https://github.com/gatsbyjs/gatsby/tree/master/examples/using-MDX"
5+
href: "https://github.com/gatsbyjs/gatsby/tree/master/examples/using-mdx"
66
---
77

88
## Introduction

docs/docs/mdx/importing-and-using-components.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ Here is a demo:
119119

120120
### Additional resources
121121

122-
- Follow this detailed [example on using MDX](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-MDX) to import and render components.
122+
- Follow this detailed [example on using MDX](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-mdx) to import and render components.

docs/docs/reference/graphql-data-layer/schema-customization.md

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ suddenly fail.
131131
Both problems can be solved by providing explicit type definitions for Gatsby's
132132
GraphQL schema.
133133

134-
### Creating type definitions
134+
## Creating type definitions
135135

136136
Look at the latter case first. Assume a new author joins the team, but in the
137137
new author entry there is a typo on the `joinedAt` field: "201-04-02" which is
@@ -150,7 +150,7 @@ not a valid Date.
150150
This will confuse Gatsby's type inference since the `joinedAt`
151151
field will now have both Date and String values.
152152

153-
#### Fixing field types
153+
### Fixing field types
154154

155155
To ensure that the field will always be of Date type, you can provide explicit
156156
type definitions to Gatsby with the [`createTypes`](/docs/reference/config-files/actions/#createTypes) action.
@@ -176,7 +176,7 @@ provided, they will still be handled by Gatsby's type inference.
176176
> (available in Gatsby v2.12 and above),
177177
> and [`sourceNodes`](/docs/reference/config-files/gatsby-node/#sourceNodes) APIs.
178178
179-
#### Opting out of type inference
179+
### Opting out of type inference
180180

181181
There are however advantages to providing full definitions for a node type, and
182182
bypassing the type inference mechanism altogether. With smaller scale projects
@@ -209,7 +209,7 @@ Note that you don't need to explicitly provide the Node interface fields (`id`,
209209
> [specifying nullability](https://graphql.org/learn/schema/#lists-and-non-null)
210210
> in GraphQL, i.e. if a field value is allowed to be `null` or not.
211211
212-
#### Defining media types
212+
### Defining media types
213213

214214
You can specify the media types handled by a node type using the `@mimeTypes` extension:
215215

@@ -222,7 +222,7 @@ type Markdown implements Node
222222

223223
The types passed in are used to determine child relations of the node.
224224

225-
#### Defining child relations
225+
### Defining child relations
226226

227227
The `@childOf` extension can be used to explicitly define what node types or media types a node is a child of and immediately add `child[MyType]` and `children[MyType]` fields on the parent.
228228

@@ -255,7 +255,7 @@ type Mdx implements Node
255255
}
256256
```
257257

258-
#### Nested types
258+
### Nested types
259259

260260
So far, the example project has only been dealing with scalar values (`String` and `Date`;
261261
GraphQL also knows `ID`, `Int`, `Float`, `Boolean` and `JSON`). Fields can
@@ -305,7 +305,7 @@ always starting from the Node types created by source and transformer plugins.
305305
> it is not a top-level type created by source or transformer plugins: it has no
306306
> `id` field, and is there to describe the data shape on a nested field.
307307
308-
#### Gatsby Type Builders
308+
### Gatsby Type Builders
309309

310310
In many cases, GraphQL SDL provides a succinct way to provide type definitions
311311
for your schema. If however you need more flexibility, `createTypes` also
@@ -362,7 +362,7 @@ schema.buildObjectType({
362362
> Note that the `createTypes` action also accepts `graphql-js` types directly,
363363
> but usually either SDL or Type Builders are the better alternatives.
364364
365-
#### Foreign-key fields
365+
### Foreign-key fields
366366

367367
In the example project, the `frontmatter.author` field on
368368
`MarkdownRemark` nodes to expand the provided field value to a full `AuthorJson` node.
@@ -466,15 +466,15 @@ For the above example you can read `@link` this way: Use the value from the fiel
466466

467467
Keep in mind that in the example above, the link of `posts` in `AuthorJson` works because `frontmatter` and `author` are both objects. If, for example, the `Frontmatter` type had a list of `authors` instead (`frontmatter.authors.email`), it wouldn't work since the `by` argument doesn't support arrays. In that case, you'd have to provide a custom resolver with [Gatsby Type Builders](/docs/reference/graphql-data-layer/schema-customization/#gatsby-type-builders) or [createResolvers API](/docs/reference/graphql-data-layer/schema-customization/#createresolvers-api).
468468

469-
#### Extensions and directives
469+
### Extensions and directives
470470

471-
Out of the box, Gatsby provides [four extensions](/docs/reference/config-files/actions/#createTypes)
472-
that allow adding custom functionality to fields without having to manually
473-
write field resolvers: the `link` extension has already been discussed above,
474-
`dateformat` allows adding date formatting options, `fileByRelativePath` is
475-
similar to `link` but will resolve relative paths when linking to `File` nodes,
476-
and `proxy` is helpful when dealing with data that contains field names with
477-
characters that are invalid in GraphQL.
471+
Out of the box, Gatsby provides [four extensions](/docs/reference/config-files/actions/#createTypes) that allow adding custom functionality to fields without having to manually write field resolvers:
472+
473+
1. The `link` extension has already been discussed above
474+
1. `dateformat` allows adding date formatting options
475+
1. `fileByRelativePath` is similar to `link` but will resolve relative paths when linking to `File` nodes
476+
1. `proxy` is helpful when dealing with data that contains field names with
477+
characters that are invalid in GraphQL or to alias fields
478478

479479
To add an extension to a field you can either use a directive in SDL, or the
480480
`extensions` property when using Gatsby Type Builders:
@@ -518,7 +518,75 @@ query {
518518
`publishedAt` is also provided a default `formatString` which will be used
519519
when no explicit formatting options are provided in the query.
520520

521-
#### Setting default field values
521+
If the JSON would contain keys you'd want to `proxy` to other names, you could do it like this:
522+
523+
```json
524+
{
525+
"name": "Doe",
526+
"firstName": "Zoe",
527+
"email": "[email protected]",
528+
"start_date": "2022/08/01"
529+
}
530+
```
531+
532+
```graphql
533+
type AuthorJson implements Node {
534+
startDate: Date! @dateformat @proxy(from: "start_date")
535+
}
536+
```
537+
538+
You can also combine multiple extensions (built-in and custom ones).
539+
540+
#### Aliasing fields
541+
542+
You can use the `@proxy` directive to alias (nested) fields to another field on the same node. This is helpful if e.g. you want to keep the shape you have to query flat or if you need it to keep things backwards compatible.
543+
544+
If you'd add a new field using [`createNodeField`](/docs/reference/config-files/actions/#createNodeField) to the `MarkdownRemark` nodes (change this check if you use another source/type) like this:
545+
546+
```js:title=gatsby-node.js
547+
exports.onCreateNode = ({ node, actions }) => {
548+
const { createNodeField } = actions
549+
if (node.internal.type === `MarkdownRemark`) {
550+
createNodeField({
551+
node,
552+
name: `someInformation`,
553+
value: `Hello World`
554+
})
555+
}
556+
}
557+
```
558+
559+
The `Hello World` would be queryable at:
560+
561+
```graphql
562+
nodes {
563+
fields {
564+
someInformation
565+
}
566+
}
567+
```
568+
569+
To be able to query `someInformation` like this instead you have to alias the `fields.someInformation` field.
570+
571+
```graphql
572+
nodes {
573+
someInformation
574+
}
575+
```
576+
577+
```javascript:title=source-plugin/gatsby-node.js
578+
exports.createSchemaCustomization = ({ actions }) => {
579+
const { createTypes } = actions
580+
581+
createTypes(`
582+
type MarkdownRemark implements Node {
583+
someInformation: String @proxy(from: "fields.someInformation")
584+
}
585+
`)
586+
}
587+
```
588+
589+
### Setting default field values
522590

523591
For setting default field values, Gatsby currently does not (yet) provide an
524592
out-of-the-box extension, so resolving a field to a default value (instead of
@@ -552,7 +620,7 @@ exports.createSchemaCustomization = ({ actions, schema }) => {
552620
}
553621
```
554622

555-
#### Creating custom extensions
623+
### Creating custom extensions
556624

557625
With the [`createFieldExtension`](/docs/reference/config-files/actions/#createFieldExtension) action
558626
it is possible to define custom extensions as a way to add reusable functionality

e2e-tests/mdx-less-babel/.gitignore

Lines changed: 0 additions & 12 deletions
This file was deleted.

e2e-tests/mdx-less-babel/cypress.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

e2e-tests/mdx-less-babel/cypress/fixtures/file-to-attempt-rce-on.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)