1 | # gatsby-transformer-mdx
|
2 |
|
3 | [![npm version][version-badge]][version]
|
4 | [![Build Status][build-badge]][build]
|
5 | [![License: MIT][license-badge]][license]
|
6 | [![module formats: cjs][module-formats-badge]][unpkg-bundle]
|
7 |
|
8 | Mdx files handling in Gatsby sites.
|
9 |
|
10 | ## Install
|
11 |
|
12 | As an npm package:
|
13 |
|
14 | ```shell
|
15 | npm i -D gatsby-transformer-mdx
|
16 | ```
|
17 |
|
18 | ## How to use
|
19 |
|
20 | ### Default case
|
21 |
|
22 | In your `gatsby-config.js`:
|
23 |
|
24 | ```javascript
|
25 | module.exports = {
|
26 | plugins: ['gatsby-transformer-mdx'],
|
27 | };
|
28 | ```
|
29 |
|
30 | This way all your mdx files in `src/pages` will converted to pages. Also you can
|
31 | import mdx files as any other React component.
|
32 |
|
33 | ### Using MDX to programmatically create pages
|
34 |
|
35 | ```javascript
|
36 | // gatsby-config.js
|
37 | module.exports = {
|
38 | plugins: [
|
39 | {
|
40 | resolve: 'gatsby-source-filesystem',
|
41 | options: {
|
42 | name: 'blog',
|
43 | path: `${__dirname}/src/content/blog`,
|
44 | },
|
45 | },
|
46 | 'gatsby-transformer-mdx',
|
47 | ],
|
48 | };
|
49 | ```
|
50 |
|
51 | ```javascript
|
52 | // gatsby-node.js
|
53 | exports.createPages = async ({ actions, graphql }) => {
|
54 | const { createPage } = actions;
|
55 | const result = await graphql(`
|
56 | {
|
57 | allMdx(filter: { sourceName: { eq: "blog" } }) {
|
58 | edges {
|
59 | node {
|
60 | id
|
61 | fileAbsolutePath
|
62 | frontmatter {
|
63 | pathname
|
64 | }
|
65 | }
|
66 | }
|
67 | }
|
68 | }
|
69 | `);
|
70 |
|
71 | if (result.errors) throw result.errors;
|
72 |
|
73 | result.data.allMdx.edges.forEach(({ node }) => {
|
74 | const {
|
75 | frontmatter: { pathname },
|
76 | } = node;
|
77 |
|
78 | createPage({
|
79 | path: `/blog${pathname}`,
|
80 | component: node.fileAbsolutePath,
|
81 | context: { id: node.id },
|
82 | });
|
83 | });
|
84 | };
|
85 | ```
|
86 |
|
87 | ## Options
|
88 |
|
89 | - [`pagesPath`](#define-mdx-pages-location-with-pagespath)
|
90 | - [`loaders`](#altering-the-webpack-mdx-loaders-with-loaders)
|
91 | - [`defaultLayout`](#define-default-mdx-layout-with-defaultlayout)
|
92 | - [`defaultImports`](#adding-components-to-mdx-scope-with-defaultimports)
|
93 |
|
94 | ### Define mdx pages location with `pagesPath`
|
95 |
|
96 | ```javascript
|
97 | // gatsby-config.js
|
98 | module.exports = {
|
99 | plugins: [
|
100 | {
|
101 | resolve: 'gatsby-transformer-mdx',
|
102 | options: {
|
103 | pagesPath: `${__dirname}/src/blog`,
|
104 | },
|
105 | },
|
106 | ],
|
107 | };
|
108 | ```
|
109 |
|
110 | \* The default is `__dirname + '/src/pages'`.
|
111 |
|
112 | ### Altering the webpack mdx loaders with `loaders`
|
113 |
|
114 | ```js
|
115 | // gatsby-config.js
|
116 | const emoji = require('remark-emoji');
|
117 |
|
118 | module.exports = {
|
119 | plugins: [
|
120 | {
|
121 | resolve: 'gatsby-transformer-mdx',
|
122 | options: {
|
123 | loaders: {
|
124 | // eg. Disable babel-loader cache
|
125 | js: () => ({ cacheDirectory: false }),
|
126 | // eg. Use emoji plugin
|
127 | mdx: () => ({ mdPlugins: [emoji] }),
|
128 | },
|
129 | },
|
130 | },
|
131 | ],
|
132 | };
|
133 | ```
|
134 |
|
135 | Checkout the [demo](https://github.com/karolis-sh/gatsby-mdx/tree/master/demos/enhancing-mdx-loaders).
|
136 |
|
137 | ### Define default mdx layout with `defaultLayout`
|
138 |
|
139 | Pass the absolute path to module:
|
140 |
|
141 | ```javascript
|
142 | // gatsby-config.js
|
143 | module.exports = {
|
144 | plugins: [
|
145 | {
|
146 | resolve: 'gatsby-transformer-mdx',
|
147 | options: {
|
148 | defaultLayout: `${__dirname}/src/layouts/PurpleLayout`,
|
149 | },
|
150 | },
|
151 | 'gatsby-plugin-catch-links',
|
152 | ],
|
153 | };
|
154 | ```
|
155 |
|
156 | You can always override it with `export default` syntax.
|
157 |
|
158 | Checkout the [demo](https://github.com/karolis-sh/gatsby-mdx/tree/master/demos/default-mdx-layout).
|
159 |
|
160 | \* Make sure that the provided default layout module exports the layout component
|
161 | as default.
|
162 |
|
163 | ### Adding components to mdx scope with `defaultImports`
|
164 |
|
165 | ```javascript
|
166 | // gatsby-config.js
|
167 | module.exports = {
|
168 | plugins: [
|
169 | {
|
170 | resolve: 'gatsby-transformer-mdx',
|
171 | options: {
|
172 | defaultImports: [
|
173 | "import Clock from 'react-live-clock';",
|
174 | { value: '{ PinkBox }', path: `${__dirname}/src/ui` },
|
175 | ],
|
176 | },
|
177 | },
|
178 | ],
|
179 | };
|
180 | ```
|
181 |
|
182 | ```md
|
183 | <!-- any-mdx-file.mdx -->
|
184 |
|
185 | # The time is <Clock format="HH:mm:ss" ticking />
|
186 |
|
187 | <PinkBox>
|
188 | Lore ipsum
|
189 | </PinkBox>
|
190 | ```
|
191 |
|
192 | Checkout the [demo](https://github.com/karolis-sh/gatsby-mdx/tree/master/demos/default-mdx-imports).
|
193 |
|
194 | ## License
|
195 |
|
196 | MIT
|
197 |
|
198 | [version-badge]: https://badge.fury.io/js/gatsby-transformer-mdx.svg
|
199 | [version]: https://www.npmjs.com/package/gatsby-transformer-mdx
|
200 | [build-badge]: https://travis-ci.org/karolis-sh/gatsby-mdx.svg?branch=master
|
201 | [build]: https://travis-ci.org/karolis-sh/gatsby-mdx
|
202 | [license-badge]: https://img.shields.io/badge/License-MIT-yellow.svg
|
203 | [license]: https://opensource.org/licenses/MIT
|
204 | [module-formats-badge]: https://img.shields.io/badge/module%20formats-cjs-green.svg
|
205 | [unpkg-bundle]: https://unpkg.com/gatsby-transformer-mdx/
|