39 releases

new 0.2.14 Dec 9, 2024
0.2.13 Nov 6, 2024
0.2.11 Oct 29, 2024
0.2.5 Jul 2, 2024
0.1.2 Nov 25, 2022

#49 in Web programming

Download history 956/week @ 2024-08-18 1065/week @ 2024-08-25 1263/week @ 2024-09-01 845/week @ 2024-09-08 368/week @ 2024-09-15 754/week @ 2024-09-22 581/week @ 2024-09-29 649/week @ 2024-10-06 700/week @ 2024-10-13 555/week @ 2024-10-20 551/week @ 2024-10-27 1032/week @ 2024-11-03 549/week @ 2024-11-10 441/week @ 2024-11-17 656/week @ 2024-11-24 498/week @ 2024-12-01

2,163 downloads per month

MIT license

465KB
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<(), markdown::message::Message> {
    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

~16MB
~293K SLoC