# micromark-extension-math [![Build][build-badge]][build] [![Coverage][coverage-badge]][coverage] [![Downloads][downloads-badge]][downloads] [![Size][size-badge]][size] [![Sponsors][sponsors-badge]][collective] [![Backers][backers-badge]][collective] [![Chat][chat-badge]][chat] [micromark][] extensions to support math (`$C_L$`). ## Contents * [What is this?](#what-is-this) * [When to use this](#when-to-use-this) * [Install](#install) * [Use](#use) * [API](#api) * [`math(options?)`](#mathoptions) * [`mathHtml(options?)`](#mathhtmloptions) * [`HtmlOptions`](#htmloptions) * [`Options`](#options) * [Authoring](#authoring) * [HTML](#html) * [CSS](#css) * [Syntax](#syntax) * [Types](#types) * [Compatibility](#compatibility) * [Security](#security) * [Related](#related) * [Contribute](#contribute) * [License](#license) ## What is this? This package contains two extensions that add support for math syntax in markdown to [`micromark`][micromark]. As there is no spec for math in markdown, this extension follows how code (fenced and text) works in Commonmark, but uses dollars. ## When to use this This project is useful when you want to support math in markdown. Extending markdown with a syntax extension makes the markdown less portable. LaTeX equations are also quite hard. But this mechanism works well when you want authors, that have some LaTeX experience, to be able to embed rich diagrams of math in scientific text. You can use these extensions when you are working with [`micromark`][micromark] already. When you need a syntax tree, you can combine this package with [`mdast-util-math`][mdast-util-math]. All these packages are used [`remark-math`][remark-math], which focusses on making it easier to transform content by abstracting these internals away. ## Install This package is [ESM only][esm]. In Node.js (version 16+), install with [npm][]: [npm][]: ```sh npm install micromark-extension-math ``` In Deno with [`esm.sh`][esmsh]: ```js import {math, mathHtml} from 'https://esm.sh/micromark-extension-math@3' ``` In browsers with [`esm.sh`][esmsh]: ```html ``` ## Use Say our document `example.md` contains: ```markdown Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation. $$ L = \frac{1}{2} \rho v^2 S C_L $$ ``` …and our module `example.js` looks as follows: ```js import fs from 'node:fs/promises' import {micromark} from 'micromark' import {math, mathHtml} from 'micromark-extension-math' const output = micromark(await fs.readFile('example.md'), { extensions: [math()], htmlExtensions: [mathHtml()] }) console.log(output) ``` …now running `node example.js` yields (abbreviated): ```html
Lift(…) can be determined by Lift Coefficient (…) like the following equation.
Not math: $$x$.
Not math: $x$$.
Escapes work, this is math: $….
Escapes work, this is math: …$.
``` That is because, when turning markdown into HTML, the first and last space, if both exist and there is also a non-space in the math, are removed. Line endings, at that stage, are considered as spaces. As the math (flow) construct occurs in flow, like all flow constructs, it must be followed by an eol (line ending) or eof (end of file). The above grammar does not show how indentation of each line is handled. To parse math (flow), let `x` be the number of `space_or_tab` characters before the opening fence sequence, after interpreting tabs based on how many virtual spaces they represent. Each line of text is then allowed (not required) to be indented with up to `x` spaces or tabs, which are then ignored as an indent instead of being considered as part of the content. This indent does not affect the closing fence. It can be indented up to a separate 3 real or virtual spaces. A bigger indent makes it part of the content instead of a fence. The `meta` part is interpreted as the [string][micromark-content-types] content type. That means that character escapes and character references are allowed. The optional `meta` part is ignored: it is not used when parsing or rendering. ## Types This package is fully typed with [TypeScript][]. It exports the additional types [`HtmlOptions`][api-html-options] and [`Options`][api-options]. ## Compatibility Projects maintained by the unified collective are compatible with maintained versions of Node.js. When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, `micromark-extension-math@^3`, compatible with Node.js 16. This package works with `micromark` version `3` and later. ## Security This package is safe assuming that you trust KaTeX. Any vulnerability in it could open you to a [cross-site scripting (XSS)][xss] attack. ## Related * [`remark-math`][remark-math] — remark (and rehype) plugins to support math * [`mdast-util-math`][mdast-util-math] — mdast utility to support math ## Contribute See [`contributing.md` in `micromark/.github`][contributing] for ways to get started. See [`support.md`][support] for ways to get help. This project has a [code of conduct][coc]. By interacting with this repository, organization, or community you agree to abide by its terms. ## License [MIT][license] © [Titus Wormer][author] [build-badge]: https://github.com/micromark/micromark-extension-math/workflows/main/badge.svg [build]: https://github.com/micromark/micromark-extension-math/actions [coverage-badge]: https://img.shields.io/codecov/c/github/micromark/micromark-extension-math.svg [coverage]: https://codecov.io/github/micromark/micromark-extension-math [downloads-badge]: https://img.shields.io/npm/dm/micromark-extension-math.svg [downloads]: https://www.npmjs.com/package/micromark-extension-math [size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=micromark-extension-math [size]: https://bundlejs.com/?q=micromark-extension-math [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg [backers-badge]: https://opencollective.com/unified/backers/badge.svg [collective]: https://opencollective.com/unified [chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg [chat]: https://github.com/micromark/micromark/discussions [npm]: https://docs.npmjs.com/cli/install [esmsh]: https://esm.sh [license]: license [author]: https://wooorm.com [contributing]: https://github.com/micromark/.github/blob/main/contributing.md [support]: https://github.com/micromark/.github/blob/main/support.md [coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md [esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c [typescript]: https://www.typescriptlang.org [development]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions [micromark]: https://github.com/micromark/micromark [micromark-content-types]: https://github.com/micromark/micromark#content-types [micromark-html-extension]: https://github.com/micromark/micromark#htmlextension [micromark-extension]: https://github.com/micromark/micromark#syntaxextension [mdast-util-math]: https://github.com/syntax-tree/mdast-util-math [remark-math]: https://github.com/remarkjs/remark-math [katex]: https://katex.org [katex-options]: https://katex.org/docs/options.html [xss]: https://en.wikipedia.org/wiki/Cross-site_scripting [api-math]: #mathoptions [api-math-html]: #mathhtmloptions [api-options]: #options [api-html-options]: #htmloptions