mdx-scoped-runtime
Version:
@mdx-js/runtime wrapper that handles full MDX syntax
125 lines (95 loc) • 2.71 kB
Markdown
[![npm version][version-badge]][version]
[![Build Status][build-badge]][build]
[![License: MIT][license-badge]][license]
[![module formats: cjs][module-formats-badge]][unpkg-bundle]
This is a wrapper around [`mdx-runtime`][mdx-runtime] that strips down the
`import ...` and `export default Layout` out of the MDX at runtime.
```shell
npm i mdx-scoped-runtime
```
You can provide any instances to the scope without any validation of the import path:
```js
import React from 'react';
import MDX from 'mdx-scoped-runtime';
import * as UI from '../components';
import Layout from '../ui/Layout';
// Provide custom components for markdown elements
const components = {
h1: (props) => <h1 style={{ color: 'tomato' }} {...props} />,
};
// Provide custom components that will be referenced as JSX
// in the markdown string
const scope = {
...UI,
Demo: (props) => <h1>This is a demo component</h1>,
};
const mdx = `
import Layout from '../ui/Layout';
import { Calendar } from '../components';
import Demo from 'wherever';
export default Layout
<Calendar />
<Demo />
`;
export default () => (
<MDX components={components} scope={scope}>
{mdx}
</MDX>
);
```
You can validate the imports via `allowedImports` prop:
```js
import React from 'react';
import MDX from 'mdx-scoped-runtime';
import * as UI from '../components';
import Layout from '../ui/Layout';
const scope = {
// scope can still be used in combination with allowedImports
};
const mdx = `
import Layout from '../ui/Layout';
import { Calendar } from '../components';
import Demo from 'wherever';
export default Layout
<Calendar />
<Demo />
`;
const allowedImports = {
wherever: {
ImportDefault: (props) => <h1>This is a demo component</h1>,
},
'../ui/Layout': {
ImportDefault: Layout,
},
'../components': {
Import: UI,
},
};
export default () => (
<MDX
components={components}
scope={scope}
allowedImports={allowedImports}
onError={(error) => console.log(error)}
>
{mdx}
</MDX>
);
```
MIT
[]: https://badge.fury.io/js/mdx-scoped-runtime.svg
[]: https://www.npmjs.com/package/mdx-scoped-runtime
[]: https://travis-ci.org/karolis-sh/gatsby-mdx.svg?branch=master
[]: https://travis-ci.org/karolis-sh/gatsby-mdx
[]: https://img.shields.io/badge/License-MIT-yellow.svg
[]: https://opensource.org/licenses/MIT
[]: https://www.npmjs.com/package/@mdx-js/runtime
[]: https://img.shields.io/badge/module%20formats-cjs-green.svg
[]: https://unpkg.com/mdx-scoped-runtime/