Skip to content

Commit

Permalink
Allow custom MDX components (#294)
Browse files Browse the repository at this point in the history
* Create mdx-components file for shadowing

* Update docs

* Create sharp-boats-knock.md
  • Loading branch information
colebemis committed Nov 19, 2021
1 parent c2037d8 commit a94e0a9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-boats-knock.md
@@ -0,0 +1,5 @@
---
"@primer/gatsby-theme-doctocat": minor
---

You can now make custom React components globally available (no import required) to all markdown files in your site.
3 changes: 1 addition & 2 deletions docs/content/components/index.mdx
Expand Up @@ -2,5 +2,4 @@
title: Components
---


Doctocat exports a collection of components that can be imported in your project and used to enhance your markdown files.
Doctocat provides a collection of React components that are globally available in all `.md` and `.mdx` files (no import required).
28 changes: 25 additions & 3 deletions docs/content/usage/customization.mdx
@@ -1,12 +1,11 @@
---
title: Customization
description: Here are a few ways you can customize your Doctocat site.
---

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

Here are a few ways you can customize your Doctocat site:

## Site metadata

You can customize your site's metadata via the `siteMetadata` object in `gatsby-config.js`:
Expand Down Expand Up @@ -40,7 +39,7 @@ Side navigation for your site is generated from the content in `src/@primer/gats
url: /example/example-2
```

_Note: Doctocat only supports one level of nesting._
<Note variant="warning">Doctocat only supports one level of nesting.</Note>

## Repository

Expand Down Expand Up @@ -177,3 +176,26 @@ Doctocat uses [`gatsby-plugin-manifest`](https://www.gatsbyjs.org/packages/gatsb
- 512x512 pixels or larger.
- Square. If it’s not, the image will be [letterboxed](<https://en.wikipedia.org/wiki/Letterboxing_(filming)>) with a transparent background.
- JPEG, PNG, WebP, TIFF, GIF, or SVG format.

## MDX components

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:

```js
// src/@primer/gatsby-theme-doctocat/mdx-components.js
import {SomeComponent} from 'path/to/some-component'

export default {
SomeComponent,
}
```

```md
---
title: Some markdown file
---

You can now use your component in any markdown file like so:

<SomeComponent />
```
6 changes: 4 additions & 2 deletions theme/src/components/wrap-root-element.js
@@ -1,6 +1,7 @@
import {MDXProvider} from '@mdx-js/react'
import {Link, ThemeProvider} from '@primer/components'
import React from 'react'
import mdxComponents from '../mdx-components'
import Blockquote from './blockquote'
import Caption from './caption'
import Code from './code'
Expand Down Expand Up @@ -36,13 +37,14 @@ const components = {
ol: List.withComponent('ol'),
dl: DescriptionList,

// Shortcodes (https://mdxjs.com/blog/shortcodes)
// Custom components
Note,
Do,
Dont,
DoDontContainer,
Caption,
ImageContainer
ImageContainer,
...mdxComponents
}

function wrapRootElement({element}) {
Expand Down
2 changes: 2 additions & 0 deletions theme/src/mdx-components.js
@@ -0,0 +1,2 @@
// Users can shadow this file to add custom components to scope of all MDX files.
export default {}

1 comment on commit a94e0a9

@vercel
Copy link

@vercel vercel bot commented on a94e0a9 Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

doctocat – ./

doctocat.vercel.app
doctocat-git-main-primer.vercel.app
doctocat-primer.vercel.app

Warning: doctocat.now.sh was not aliased since new "now.sh" domains are deprecated. Please use "vercel.app" instead. Learn More →

Please sign in to comment.