Skip to content

Commit a94e0a9

Browse files
authored
Allow custom MDX components (#294)
* Create mdx-components file for shadowing * Update docs * Create sharp-boats-knock.md
1 parent c2037d8 commit a94e0a9

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

.changeset/sharp-boats-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/gatsby-theme-doctocat": minor
3+
---
4+
5+
You can now make custom React components globally available (no import required) to all markdown files in your site.

docs/content/components/index.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
title: Components
33
---
44

5-
6-
Doctocat exports a collection of components that can be imported in your project and used to enhance your markdown files.
5+
Doctocat provides a collection of React components that are globally available in all `.md` and `.mdx` files (no import required).

docs/content/usage/customization.mdx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
22
title: Customization
3+
description: Here are a few ways you can customize your Doctocat site.
34
---
45

56
import {BorderBox} from '@primer/components'
67
import {Contributors} from '@primer/gatsby-theme-doctocat'
78

8-
Here are a few ways you can customize your Doctocat site:
9-
109
## Site metadata
1110

1211
You can customize your site's metadata via the `siteMetadata` object in `gatsby-config.js`:
@@ -40,7 +39,7 @@ Side navigation for your site is generated from the content in `src/@primer/gats
4039
url: /example/example-2
4140
```
4241
43-
_Note: Doctocat only supports one level of nesting._
42+
<Note variant="warning">Doctocat only supports one level of nesting.</Note>
4443
4544
## Repository
4645
@@ -177,3 +176,26 @@ Doctocat uses [`gatsby-plugin-manifest`](https://www.gatsbyjs.org/packages/gatsb
177176
- 512x512 pixels or larger.
178177
- Square. If it’s not, the image will be [letterboxed](<https://en.wikipedia.org/wiki/Letterboxing_(filming)>) with a transparent background.
179178
- JPEG, PNG, WebP, TIFF, GIF, or SVG format.
179+
180+
## MDX components
181+
182+
Doctocat uses [MDX](https://mdxjs.com/) to allow you to embed React components in your markdown files and provides a few React [components](/components) that are globally available in all `.md` and `.mdx` files (no import required). To add custom components to this list of globally available components, create an `mdx-components.js` file in `src/@primer/gatsby-theme-doctocat/` and export your custom components from this file:
183+
184+
```js
185+
// src/@primer/gatsby-theme-doctocat/mdx-components.js
186+
import {SomeComponent} from 'path/to/some-component'
187+
188+
export default {
189+
SomeComponent,
190+
}
191+
```
192+
193+
```md
194+
---
195+
title: Some markdown file
196+
---
197+
198+
You can now use your component in any markdown file like so:
199+
200+
<SomeComponent />
201+
```

theme/src/components/wrap-root-element.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {MDXProvider} from '@mdx-js/react'
22
import {Link, ThemeProvider} from '@primer/components'
33
import React from 'react'
4+
import mdxComponents from '../mdx-components'
45
import Blockquote from './blockquote'
56
import Caption from './caption'
67
import Code from './code'
@@ -36,13 +37,14 @@ const components = {
3637
ol: List.withComponent('ol'),
3738
dl: DescriptionList,
3839

39-
// Shortcodes (https://mdxjs.com/blog/shortcodes)
40+
// Custom components
4041
Note,
4142
Do,
4243
Dont,
4344
DoDontContainer,
4445
Caption,
45-
ImageContainer
46+
ImageContainer,
47+
...mdxComponents
4648
}
4749

4850
function wrapRootElement({element}) {

theme/src/mdx-components.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Users can shadow this file to add custom components to scope of all MDX files.
2+
export default {}

0 commit comments

Comments
 (0)