# mdx-it

> markdown-it based parser for MDX (i.e. JSX-infused markdown)

Latest version **0.2.0** (published 2017-03-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install mdx-it
pnpm add mdx-it
yarn add mdx-it
bun add mdx-it
```

Provides the command `mdx`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-03-28 |
| First published | 2017-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | James K Nelson |
| Maintainers | jamesknelson |
| Keywords | markdown, jsx, mdx |

## Links

- npm: https://www.npmjs.com/package/mdx-it
- npm.io page: https://npm.io/package/mdx-it

## Dependencies (11)

- [prismjs](https://npm.io/package/prismjs.md) ^1.6.0
- [commander](https://npm.io/package/commander.md) ^2.9.0
- [parsimmon](https://npm.io/package/parsimmon.md) ^1.2.0
- [babel-core](https://npm.io/package/babel-core.md) ^6.22.1
- [markdown-it](https://npm.io/package/markdown-it.md) ^8.2.2
- [front-matter](https://npm.io/package/front-matter.md) ^2.1.2
- [markdown-it-anchor](https://npm.io/package/markdown-it-anchor.md) ^2.6.0
- [babel-plugin-syntax-jsx](https://npm.io/package/babel-plugin-syntax-jsx.md) ^6.18.0
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) ^6.23.0
- [babel-plugin-syntax-trailing-function-commas](https://npm.io/package/babel-plugin-syntax-trailing-function-commas.md) ^6.22.0
- [babel-plugin-transform-es2015-modules-commonjs](https://npm.io/package/babel-plugin-transform-es2015-modules-commonjs.md) ^6.22.0

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2017-03-28
- 0.2.0-beta.1 — 2017-03-28
- 0.1.4 — 2017-02-05
- 0.1.3 — 2017-02-05
- 0.1.2 — 2017-02-05
- 0.1.1 — 2017-02-05
- 0.1.0 — 2017-02-04

## README

mdx-it
======

[![npm version](https://img.shields.io/npm/v/mdx-it.svg)](https://www.npmjs.com/package/mdx-it)

Convert MDX files (i.e. JSX-infused Markdown files) into standard JSX files.

This package is a wrapper around the excellent [markdown-it](https://github.com/markdown-it/markdown-it) project. The major differences from __markdown-it__ are:

- __mdx-it__ outputs a React component<br />
  __markdown-it__ outputs a HTML file

- __mdx-it__ only supports markdown-it plugins that modify the parser<br />
  __markdown-it__ supports all markdown-it plugins (that is kind of the point)

- __mdx-it__ treats `<tags>` as JSX. You need to use `<br />`, not `<br>`.<br />
  __markdown-it__ treats `<tags>` as standard HTML.

- __mdx-it__ supports theming by letting you override components at run-time<br />
  __markdown-it__ only supports theming via CSS

MDX was created so that live examples can be embedded in markdown-based documentation using JSX. It was first used in the website for the [junctions](https://github.com/jamesknelson/junctions) router for React, and the [sitepack](https://github.com/jamesknelson/sitepack) static website generator.

## Try It Yourself

The quickest way to try MDX is to use the included command line utility.

You can test with a standard markdown file, or you can copy-paste the example from the next section into a new file.

```bash
npm install -g mdx-it
mdx -o example-output.jsx example.mdx
```

If you're using webpack, you can use the [mdx-loader](https://github.com/jamesknelson/mdx-loader).

## MDX

The MDX format is almost identical to Markdown. The are two main differences:

- You may specify imports at the top of your file using ES6 modules syntax
- You may specify meta information at the top of your file using the [front-matter](https://jekyllrb.com/docs/frontmatter/) format

The output JSX will contain two exports:

- The main export will be a React stateless component that renders your content
- A `meta` object will contain the information in your front-matter

### Example

```markdown
---
description: junctions.js lets you add routes to your React components.
---
import Tab from './controls/Tab'
import ComponentExample from './controls/ComponentExample'
import GistVanillaExample from '../examples/GistVanilla.example'
import GistSugarFreeExample from '../examples/GistSugarFree.example'

# Junctions

Add routes to your reusable components.

## The Gist

<Tab.Group>
  <Tab.Item title='Vanilla React'>
      <ComponentExample example={GistVanillaExample} />
  </Tab.Item>
  <Tab.Item title='Sugar Free'>
      <ComponentExample example={GistSugarFreeExample} />
  </Tab.Item>
</Tab.Group>

## What about react-router 4?

The main difference between junctions.js and react-router is this: **junctions puts components first, react-router puts URLs first**.

This means that if you're building an application, junctions will probably solve your problem better. And while some people are still building websites, most people are building applications.

> Do not divide the web into documents and applications. Documents are dead.

- [@thejameskyle](https://twitter.com/thejameskyle/status/824790686822129665)
```

### Example Output

```jsx
import React, { createFactory } from 'react'
import Tab from './controls/Tab'
import ComponentExample from './controls/ComponentExample'
import GistVanillaExample from '../examples/GistVanilla.example'
import GistSugarFreeExample from '../examples/GistSugarFree.example'

module.exports = function({ factories={} }) {
  const {
    wrapper = createFactory('div'),
    a = createFactory('a'),
    blockquote = createFactory('blockquote'),
    h1 = createFactory('h1'),
    h2 = createFactory('h2'),
    li = createFactory('li'),
    p = createFactory('p'),
    strong = createFactory('strong'),
    ul = createFactory('ul'),
  } = factories

  return (
    wrapper({},
      h1({"id": "junctions"},
        "Junctions",
      ),
      p({},
        "Add routes to your reusable components.",
      ),
      h2({"id": "the-gist"},
        "The Gist",
      ),
      <Tab.Group>
        <Tab.Item title='Vanilla React'>
            <ComponentExample example={GistVanillaExample} />
        </Tab.Item>
        <Tab.Item title='Sugar Free'>
            <ComponentExample example={GistSugarFreeExample} />
        </Tab.Item>
      </Tab.Group>,
      h2({"id": "what-about-react-router-4"},
        "What about react-router 4?",
      ),
      p({},
        "The main difference between junctions.js and react-router is this: ",
        strong({},
          "junctions puts components first, react-router puts URLs first",
        ),
        ".",
      ),
      p({},
        "This means that if you’re building an application, junctions will probably solve your problem better. And while some people are still building websites, most people are building applications.",
      ),
      blockquote({},
        p({},
          "Do not divide the web into documents and applications. Documents are dead.",
        ),
      ),
      ul({},
        li({},
          a({"href": "https://twitter.com/thejameskyle/status/824790686822129665"},
            "@thejameskyle",
          ),
        ),
      )
    )
  )
}

module.exports.meta = {
  "description": "junctions.js lets you add routes to your React components."
}
```

## API

__mdx-it__ attempts to mimic the API of [markdown-it](https://github.com/markdown-it/markdown-it) where possible. In fact, the main export of __mdx-it__ inherits from the main export of __markdown-it__. Because of this, the __markdown-it__ documentation is your best friend.

### Example

This example is based on the [mdx-loader](https://github.com/jamesknelson/mdx-loader) package. It renders a string of MDX content, replacing image `src` attributes with `require` statements.

```js
var MDXIt = require('mdx-it')
var url = require('url')
var loaderUtils = require('loader-utils')
var frontMatter = require('front-matter')

function mdImageReplacer(md) {
  md.core.ruler.push('imageReplacer', function(state) {
    function applyFilterToTokenHierarchy(token) {
      if (token.children) {
        token.children.map(applyFilterToTokenHierarchy);
      }

      if (token.type === 'image') {
        var src = token.attrGet('src')
        if(!loaderUtils.isUrlRequest(src)) return;
        var uri = url.parse(src);
        uri.hash = null;
        token.attrSet('src', 'require("'+uri.format()+'")');
      }
    }

    state.tokens.map(applyFilterToTokenHierarchy);
  })
}

function renderMDX(content) {
  var md =
    new MDXIt({ linkify: true, typographer: true })
      .use(mdImageReplacer)
      .use(mdAnchor);

  var data = frontMatter(content);

  return md.render(data.body)
}
```

## Acknowledgements

- This wouldn't be possible without [markdown-it](https://github.com/markdown-it/markdown-it). A bit thanks to the team behind that project.

- This project also uses code from the JSX parser in the [markdown-it-jsx](https://www.npmjs.com/package/markdown-it-jsx) project.

---
_Source: https://npm.io/package/mdx-it · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
