Skip to content

goodproblems/remark-mdx-math-enhanced

Repository files navigation

Remark MDX math enhanced

An MDX plugin adding support for math environments with embedded JS expressions

What is this?

This package allows math environments in MDX documents to contain embedded JavaScript expressions analogous to MDX expressions. These expressions have full access to props, exports, etc.

How it works

Math nodes produced by remark-math are transformed into JSX element nodes at compile time and rendered at run time via a React component which your app is expected to provide (default is Math but is configurable)


🚨 Important: This plugin is quite new and currently still in beta, it's possible the API and/or approach may change so use at your own risk.


Notes

  • This plugin expects you to define your own Math component which will handle rendering. For an example implementation of a <Math/> component using Katex see examples/Math.js

  • Rendering math at runtime instead of at compile time means that client-side JS is required, and that more browser processing power is required for rendering. Accordingly, this plugin should only be used in cases where dynamic math (i.e. math with JS expressions inside) is actually required

Install

Install with npm npm install remark-mdx-math-enhanced

Use

Say we have the following .mdx file where we want to render some math with a generated value of pi times a prop value

export const pi = Math.PI

$\js{props.N}\pi = \js{props.N * pi}$

$$
\js{props.N}\pi = \js{props.N * pi}
$$

And an MDX setup something like this

import { readFileSync } from 'fs'

import remarkMath from 'remark-math'
import remarkMdxEnhanced from 'remark-mdx-math-enhanced'
import { compileSync } from '@mdx-js/mdx'

const { value } = compileSync(readFileSync('example.mdx'), {
  remarkPlugins: [remarkMath, [remarkMdxEnhanced, { component: 'Math' }]]
})

console.log(value)

Will result in something like

export const pi = Math.PI

export default function MDXContent(props) {
  return <>
    <Math>{String.raw`${props.N}\pi = ${props.N * pi}`}</Math>
    <Math display>{String.raw`${props.N}\pi = ${props.N * pi}`}</Math>
  </>
}

Note how \js{...} have been replaced by ${...} which are valid string interpolation placeholders.

API

The default export is remarkMdxMathEnhanced.

unified().use(remarkMdx).use(remarkMath).use(remarkMdxMathEnhanced[, options])

Plugin to transform math nodes to JSX element nodes which render math at run time

options

Configuration (optional).

options.component

Name of react component which will be used to render math, default is 'Math'

options.startDelimiter

Start delimiter of JS expressions, default is \js{

options.endDelimiter

Start delimiter of JS expressions, default is }

About

Plugin to allow for math with expressions in MDX

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published