Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including mdx-scoped-runtime with all npm packages installed. Try it out:

require("react/package.json"); // react is a peer dependency. var mdxScopedRuntime = require("mdx-scoped-runtime")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mdx-scoped-runtime v0.7.4

@mdx-js/runtime wrapper that handles full MDX syntax

mdx-scoped-runtime

npm version Build Status License: MIT module formats: cjs

This is a wrapper around mdx-runtime that strips down the import ... and export default Layout out of the MDX at runtime.

Install

npm i mdx-scoped-runtime

How to use

You can provide any instances to the scope without any validation of the import path:

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

# Hello, world!

<Calendar />

<Demo />
`;

export default () => (
  <MDX components={components} scope={scope}>
    {mdx}
  </MDX>
);

Advanced usage

You can validate the imports via allowedImports prop:

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

# Hello, world!

<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>
);

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free