Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

A remark MDX plugin for using markdown code block metadata

License

Notifications You must be signed in to change notification settings

remcohaszing/remark-mdx-code-meta

Repository files navigation

remark-mdx-code-meta

github actions npm prettier codecov

A remark MDX plugin for using markdown code block metadata

Warning This package has been deprecated. Use rehype-mdx-code-props instead.

Installation

npm install remark-mdx-code-meta

Usage

This plugin interprets markdown code block metadata as JSX props.

For example, given a file named example.mdx with the following contents:

```js copy filename="awesome.js" onUsage={props.beAwesome} {...props}
console.log('Everything is awesome!');
```

The following script:

import { readFileSync } from 'fs';

import remarkMdxCodeMeta from 'remark-mdx-code-meta';
import { compileSync } from 'xdm';

const { contents } = compileSync(readFileSync('example.mdx'), {
  jsx: true,
  remarkPlugins: [remarkMdxCodeMeta],
});
console.log(contents);

Roughly yields:

export default function MDXContent(props) {
  return (
    <pre copy filename="awesome.js" onUsage={props.beAwesome} {...props}>
      <code className="language-js">{"console.log('Everything is awesome!');\n"}</code>
    </pre>
  );
}

Of course the <pre /> element doesn’t support those custom props. Use custom [components][] to give the props meaning.

License

MIT @ Remco Haszing