Skip to content

Commit 1656152

Browse files
author
Marvin Frachet
authoredOct 15, 2020
chore(gatsby-plugin-mdx): add pluginOptionsSchema export (#27445)
1 parent 2494ae1 commit 1656152

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
2+
import { pluginOptionsSchema } from "../gatsby-node"
3+
4+
describe(`pluginOptionsSchema`, () => {
5+
it(`should provide meaningful errors when fields are invalid`, () => {
6+
const expectedErrors = [
7+
`"extensions" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`,
8+
`"defaultLayout" must be of type object`,
9+
`"gatsbyRemarkPlugins" "[0]" does not match any of the allowed types. "[1]" does not match any of the allowed types`,
10+
`"remarkPlugins" must be an array`,
11+
`"rehypePlugins" must be an array`,
12+
`"mediaTypes" "[0]" must be a string. "[1]" must be a string`,
13+
`"shouldBlockNodeFromTransformation" must have an arity lesser or equal to 1`,
14+
]
15+
16+
const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
17+
extensions: [1, 2, 3],
18+
defaultLayout: `this should be an object`,
19+
gatsbyRemarkPlugins: [1, { not: `existing prop` }, `valid one`],
20+
remarkPlugins: `this should be an array of object`,
21+
rehypePlugins: `this should be an array of object`,
22+
mediaTypes: [1, 2],
23+
shouldBlockNodeFromTransformation: (wrong, number) => null,
24+
})
25+
26+
expect(errors).toEqual(expectedErrors)
27+
})
28+
29+
it(`should validate the schema`, () => {
30+
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
31+
extensions: [`.mdx`, `.mdxx`],
32+
defaultLayout: {
33+
posts: `../post-layout.js`,
34+
default: `../default-layout.js`,
35+
},
36+
gatsbyRemarkPlugins: [
37+
{
38+
resolve: `gatsby-remark-images`,
39+
options: {
40+
maxWidth: 590,
41+
},
42+
},
43+
`gatsby-remark-other-plugin`,
44+
],
45+
remarkPlugins: [
46+
require(`../gatsby-node.js`),
47+
[require(`../gatsby-node.js`), { target: false }],
48+
],
49+
rehypePlugins: [
50+
require(`../gatsby-node.js`),
51+
[require(`../gatsby-node.js`), { behavior: `wrap` }],
52+
],
53+
mediaTypes: [`text/markdown`, `text/x-markdown`, `custom-media/type`],
54+
shouldBlockNodeFromTransformation: node => Boolean(node),
55+
})
56+
57+
expect(isValid).toBe(true)
58+
})
59+
})

‎packages/gatsby-plugin-mdx/gatsby-node.js

+55
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,58 @@ exports.onPostBootstrap = ({ cache }, pluginOptions) => {
5757
)
5858
}
5959
}
60+
61+
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
62+
exports.pluginOptionsSchema = function ({ Joi }) {
63+
return Joi.object({
64+
extensions: Joi.array()
65+
.items(Joi.string())
66+
.default([".mdx"])
67+
.description(
68+
`Configure the file extensions that gatsby-plugin-mdx will process`
69+
),
70+
defaultLayout: Joi.object({})
71+
.unknown(true)
72+
.default({})
73+
.description(`Set the layout components for MDX source types`),
74+
gatsbyRemarkPlugins: Joi.array()
75+
.items(
76+
Joi.string(),
77+
Joi.object({
78+
resolve: Joi.string(),
79+
options: Joi.object({}).unknown(true),
80+
})
81+
)
82+
.default([])
83+
.description(`Use Gatsby-specific remark plugins`),
84+
remarkPlugins: Joi.array()
85+
.items(
86+
Joi.alternatives().try(
87+
Joi.object({}).unknown(true),
88+
Joi.array().items(Joi.object({}).unknown(true))
89+
)
90+
)
91+
.default([])
92+
.description(`Specify remark plugins`),
93+
rehypePlugins: Joi.array()
94+
.items(
95+
Joi.alternatives().try(
96+
Joi.object({}).unknown(true),
97+
Joi.array().items(Joi.object({}).unknown(true))
98+
)
99+
)
100+
.default([])
101+
.description(`Specify rehype plugins`),
102+
mediaTypes: Joi.array()
103+
.items(Joi.string())
104+
.default(["text/markdown", "text/x-markdown"])
105+
.description(`Determine which media types are processed by MDX`),
106+
shouldBlockNodeFromTransformation: Joi.function()
107+
.maxArity(1)
108+
.default(() => false)
109+
.description(
110+
`Disable MDX transformation for nodes where this function returns true`
111+
),
112+
})
113+
}
114+
}

‎packages/gatsby-plugin-mdx/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"devDependencies": {
5959
"@mdx-js/mdx": "^1.6.16",
6060
"@mdx-js/react": "^1.6.16",
61+
"gatsby-plugin-utils": "^0.2.31",
6162
"jest": "^24.9.0",
6263
"js-combinatorics": "^1.4.5",
6364
"react-test-renderer": "^16.13.1"

0 commit comments

Comments
 (0)
Please sign in to comment.