Have ideas to improve npm?Join in the discussion! »

    micromark-extension-math

    0.1.2 • Public • Published

    micromark-extension-math

    Build Coverage Downloads Size Sponsors Backers Chat

    micromark extension to support math ($C_L$).

    As there is no spec for math in markdown, this extension stays as close to code in text and fenced code in flow in CommonMark, but using dollar signs.

    This package provides the low-level modules for integrating with the micromark tokenizer and the micromark HTML compiler.

    You probably shouldn’t use the HTML parts of this package directly, but instead use mdast-util-math with mdast.

    Math (text) can start with one or more dollar signs, so long as they match:
    With one: $\alpha$, two: $$\beta$$, or three: $$$\gamma$$$.
    
    This is useful, because like code, typical markdown escapes don’t work.
    For dollars inside math, use more dollars around it: $$\raisebox{0.25em}{$\frac
    a b$}$$.
    
    If the math starts and ends with a space (or EOL), those are removed: $$ \$ $$.
    
    Math (flow) starts at two or more dollars:
    
    $$
    \Delta
    $$
    
    You can hide some stuff in the meta of the opening fence (but no dollars):
    
    $$hidden information
    $$
    
    Math which doesn’t have a closing fence, still works, like fenced code:
    
    > $$
    > this is
    > all math
    
    …but at the end of their container (block quote, list item, or document), they
    are closed.

    Install

    npm:

    npm install micromark-extension-math

    Use

    Say we have the following file, example.md:

    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 script, example.js, looks as follows:

    var fs = require('fs')
    var micromark = require('micromark')
    var math = require('micromark-extension-math')
    var mathHtml = require('micromark-extension-math/html')
    
    var doc = fs.readFileSync('example.md')
    
    var result = micromark(doc, {
      extensions: [math],
      htmlExtensions: [mathHtml()]
    })
    
    console.log(result)

    Now, running node example yields (abbreviated):

    <p>Lift(<span class="math math-inline"><span class="katex"></span></span>) can be determined by Lift Coefficient (<span class="math math-inline"><span class="katex"></span></span>) like the following equation.</p>
    <div class="math math-display"><span class="katex-display"><span class="katex"></span></span></div>

    API

    html(options?)

    syntax

    Note: syntax is the default export of this module, html is available at micromark-extension-math/html.

    Support math. The export of syntax is an extension for the micromark parser (to tokenize math in text and flow; can be passed in extensions). The export of html is a function that can be called with options and returns an extension for the default HTML compiler (to compile math with KaTeX; can be passed in htmlExtensions).

    options

    Passed to katex.renderToString. displayMode is overwritten by this plugin, to false for math in text, and true for math in flow.

    Related

    Contribute

    See contributing.md in micromark/.github for ways to get started. See support.md for ways to get help.

    This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

    License

    MIT © Titus Wormer

    Install

    npm i micromark-extension-math

    DownloadsWeekly Downloads

    17,639

    Version

    0.1.2

    License

    MIT

    Unpacked Size

    17.6 kB

    Total Files

    10

    Last publish

    Collaborators

    • avatar