24 releases

0.1.23 Feb 5, 2024
0.1.21 Dec 21, 2023
0.1.20 Nov 20, 2023
0.1.14 Jul 6, 2023
0.1.2 Nov 25, 2022

#35 in Text processing

Download history 167/week @ 2023-12-22 863/week @ 2023-12-29 1233/week @ 2024-01-05 661/week @ 2024-01-12 1165/week @ 2024-01-19 1232/week @ 2024-01-26 902/week @ 2024-02-02 1054/week @ 2024-02-09 1001/week @ 2024-02-16 1110/week @ 2024-02-23 1549/week @ 2024-03-01 1109/week @ 2024-03-08 995/week @ 2024-03-15 667/week @ 2024-03-22 746/week @ 2024-03-29 481/week @ 2024-04-05

3,047 downloads per month

MIT license

455KB
10K SLoC

mdxjs-rs

Build Coverage GitHub docs.rs crates.io

Compile MDX to JavaScript in Rust.

When should I use this?

You can use this crate when you’re dealing with the Rust language and want to compile MDX to JavaScript. To parse the MDX format to a syntax tree, use markdown-rs instead.

This project does not yet support plugins. To benefit from the unified (remark and rehype) ecosystem, use @mdx-js/mdx.

What is this?

This Rust crate works exactly like the npm package @mdx-js/mdx. It uses the Rust crates markdown-rs and SWC to deal with the markdown and JavaScript inside MDX.

Questions

Contents

Install

With Rust (rust edition 2018+, ±version 1.56+), install with cargo:

cargo add mdxjs

Use

extern crate mdxjs;

fn main() -> Result<(), String> {
    println!(
        "{}",
        mdxjs::compile(
            r###"
import {Chart} from './snowfall.js'
export const year = 2018

# Last year’s snowfall

In {year}, the snowfall was above average.
It was followed by a warm spring which caused
flood conditions in many of the nearby rivers.

<Chart year={year} color="#fcb32c" />
"###,
            &Default::default()
        )?
    );

    Ok(())
}

Yields (prettified):

import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
import {Chart} from './snowfall.js'
export const year = 2018

function _createMdxContent(props) {
  const _components = Object.assign({h1: 'h1', p: 'p'}, props.components)
  return _jsxs(_Fragment, {
    children: [
      _jsx(_components.h1, {children: 'Last year’s snowfall'}),
      '\n',
      _jsxs(_components.p, {
        children: [
          'In ',
          year,
          ', the snowfall was above average.\nIt was followed by a warm spring which caused\nflood conditions in many of the nearby rivers.'
        ]
      }),
      '\n',
      _jsx(Chart, {year: year, color: '#fcb32c'})
    ]
  })
}

function MDXContent(props = {}) {
  const {wrapper: MDXLayout} = props.components || {}
  return MDXLayout
    ? _jsx(MDXLayout, Object.assign({}, props, {children: _jsx(_createMdxContent, props)}))
    : _createMdxContent(props)
}

export default MDXContent

API

mdxjs-rs exposes compile, JsxRuntime, Options, and a few other structs and enums.

See the crate docs for more info.

Project

Test

mdxjs-rs is tested with a lot of tests. These tests reach all branches in the code, which means that this project has 100% code coverage.

The following bash scripts are useful when working on this project:

  • run examples:
    RUST_BACKTRACE=1 cargo run --example lib
    
  • format:
    cargo fmt && cargo fix
    
  • lint:
    cargo fmt --check && cargo clippy --all-targets
    
  • test:
    RUST_BACKTRACE=1 cargo test
    
  • docs:
    cargo doc --document-private-items
    

Version

mdxjs-rs follows SemVer.

Security

MDX is a programming language. It is JavaScript. It is not safe to let people you don’t trust write MDX.

Contribute

See contributing.md for ways to help. See support.md for ways to get help. See code-of-conduct.md for how to communicate in and around this project.

Sponsor

Support this effort and give back by sponsoring:

Thanks

Special thanks go out to:

  • Vercel for funding the initial development

License

MIT © Titus Wormer

Dependencies

~10MB
~225K SLoC