Socket
Socket
Sign inDemoInstall

mathpix-markdown-it

Package Overview
Dependencies
Maintainers
1
Versions
143
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mathpix-markdown-it

Mathpix-markdown-it is an open source implementation of the mathpix-markdown spec written in Typescript. It relies on the following open source libraries: MathJax v3 (to render math with SVGs), markdown-it (for standard Markdown parsing)


Version published
Weekly downloads
3.1K
increased by6.88%
Maintainers
1
Weekly downloads
 
Created
Source

What is Mathpix Markdown?

mathpix-markdown is a Markdown dialect which addresses limitations inherent in standard Markdown that appear when communicating scientific and mathematical information:

  • lack of standardized support for math equations and math equation numbering and references
  • poor support for tables
  • no support for document elements common in academic publishing, such as abstracts, titles, and author lists
  • limited support for linking to tables, equations, and sections within a document

How is Mathpix Markdown different from regular Markdown?

Mathpix Markdown addresses these limitations by adding support for the following standard Latex syntax elements which are already familiar to the scientific community:

  • inline math via \( <latex math> \)
  • block math via \[ <latex math> \] or $$ <math> $$
  • tables via \begin{tabular} ... \end{tabular}
  • figures and figure captions via \begin{figure} \caption{...} ... \end{figure}
  • numbered and unnumbered equation enviornments \begin{elem} ... \end{elem} and \begin{elem*} ... \end{elem*} where elem=equation|align|split|gather
  • equation, table, and figure references via \label, \ref, \eqref, \tag
  • text formatting options \title{...}, \author{...}, \begin{abstract}...\end{abstract}, \section{Section Title}, \subsection{Section Title}, \subsubsection{Section Title}, \textit{italicized text}, \textbf{bold text}, \url{link}

What is mathpix-markdown-it?

mathpix-markdown-it is an open source implementation of the mathpix-markdown spec written in Typescript.

It relies on the following open source libraries:

  • MathJax v3 (to render math with SVGs)
  • markdown-it (for standard Markdown parsing)

Quickstart

Installation

npm usage:

$ npm install https://github.com/Mathpix/mathpix-markdown-it.git

yarn usage:

$ yarn add https://github.com/Mathpix/mathpix-markdown-it.git

How to use

React usage

mathpix-markdown-it components usage

We provide React components which make rendering of mathpix-markdown-it easy for React applications: Full example

import {MathpixMarkdown, MathpixLoader} from 'mathpix-markdown-it';


class App extends Component {
  render() {
    return (
      <MathpixLoader>
          <MathpixMarkdown text="\\(ax^2 + bx + c = 0\\)"/>
          <MathpixMarkdown text="$x = \frac { - b \pm \sqrt { b ^ { 2 } - 4 a c } } { 2 a }$"/>
          ...
      </MathpixLoader>
    );
  }
}

MathpixMarkdownModel methods usage

Example of render() method usage
import * as React from 'react';
import { MathpixMarkdownModel as MM } from 'mathpix-markdown-it';

class App extends React.Component {
  componentDidMount() {
    const elStyle = document.getElementById('Mathpix-styles');
    if (!elStyle) {
      const style = document.createElement("style");
      style.setAttribute("id", "Mathpix-styles");
      style.innerHTML = MM.getMathpixFontsStyle() + MM.getMathpixStyle(true);
      document.head.appendChild(style);
    }
  }
  render() {
    const html = MM.render('$x = \\frac { - b \\pm \\sqrt { b ^ { 2 } - 4 a c } } { 2 a }$');
    return (
      <div className="App">
        <div className="content" dangerouslySetInnerHTML={{__html: html}}></div>
      </div>
    )
  }
}

export default App;

Example of markdownToHTML() method usage
class ConvertForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '\\[\n' +
        'y = \\frac { \\sum _ { i } w _ { i } y _ { i } } { \\sum _ { i } w _ { i } } , i = 1,2 \\ldots k\n' +
        '\\]',
      result: ''
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    event.preventDefault();
    this.setState({result: MM.markdownToHTML(this.state.value)})
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <h1>Input Text with Latex:</h1>
          <textarea value={this.state.value} onChange={this.handleChange} />
          <input type="submit" value="Convert" />
        </form>
        <div id='preview-content' dangerouslySetInnerHTML={{__html: this.state.result}}/>
      </div>
    );
  }
}
Example of Latex to mathml/asciimath/tsv conversion

Rendering methods have the ability to convert Latex representation to such formats as: mathml, asciimath, tsv

const options = {
      outMath: {
        include_mathml: true,
        include_asciimath: true,
        include_latex: true,
        include_svg: true,
        include_tsv: true,
      }
    };
    const html = MM.markdownToHTML(`$x^x$`, options);

markdownToHTML() returns an HTML string that will contain the formats specified in the options.

For Latex formulas, the result will be:

<span class="math-block">
    <mathml style="display: none">...</mathml>
    <asciimath style="display: none">...</asciimath>
    <latex style="display: none">...</latex>
    <mjx-comtainer class="MathJax" jax="SVG">..</mjx-comtainer>
</span>

For tabular, the result will be:

<div class="table_ tabular">
    <table id="tabular">...</table>
    <tsv style="display: none">...</tsv>
</div>

Then calling the parseMarkdownByHTML(html) method will return all formats as a list from the incoming html string.

[
    {
      "type": "mathml",
      "value": "<math>...</math>"
    },
    {
       "type": "asciimath",
       "value": "x^(x)"
     },
    {
       "type": "latex",
       "value": "x^x
     },
    {
       "type": "html",
       "value": "<mjx-container>...</mjx-container>"
     }
]

NodeJS

MathpixMarkdownModel methods usage

Example of mathpix-markdown-it usage in the node application
const {MathpixMarkdownModel} = require('mathpix-markdown-it'); 

const htmlMM = MathpixMarkdownModel.render(text, options);
const mathpixStyles = MathpixMarkdownModel.getMathpixStyleOnly();

Before using mathpix-markdown-it in node applications, you should define global variables

const Window = require('window');
const window = new Window();
global.window = window;
global.document = window.document;

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.DOMParser = new JSDOM().window.DOMParser;
Simple example of mathpix-markdown-it usage in node app.

It prints html to the console for string \\(ax^2 + bx + c = 0\\)

  1. Install packages:
npm install https://github.com/Mathpix/mathpix-markdown-it.git jsdom window
  1. Node app.js:
const {MathpixMarkdownModel} = require('mathpix-markdown-it');

const Window = require('window');
const window = new Window();
global.window = window;
global.document = window.document;

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.DOMParser = new JSDOM().window.DOMParser;

const text = `\\(ax^2 + bx + c = 0\\)`;
const options = {
  htmlTags: true,
  width: 800
};
const htmlMM = MathpixMarkdownModel.markdownToHTML(text, options);

console.log(htmlMM);
  1. Start:
node app.js
  1. Result:
<div><span  class="math-inline " ><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.186ex" xmlns="http://www.w3.org/2000/svg" width="16.328ex" height="2.072ex" role="img" focusable="false" viewBox="0 -833.9 7217 915.9"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path></g><g data-mml-node="msup" transform="translate(529, 0)"><g data-mml-node="mi"><path data-c="78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mn" transform="translate(572, 363) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="mo" transform="translate(1726.8, 0)"><path data-c="2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path></g><g data-mml-node="mi" transform="translate(2727, 0)"><path data-c="62" d="M73 647Q73 657 77 670T89 683Q90 683 161 688T234 694Q246 694 246 685T212 542Q204 508 195 472T180 418L176 399Q176 396 182 402Q231 442 283 442Q345 442 383 396T422 280Q422 169 343 79T173 -11Q123 -11 82 27T40 150V159Q40 180 48 217T97 414Q147 611 147 623T109 637Q104 637 101 637H96Q86 637 83 637T76 640T73 647ZM336 325V331Q336 405 275 405Q258 405 240 397T207 376T181 352T163 330L157 322L136 236Q114 150 114 114Q114 66 138 42Q154 26 178 26Q211 26 245 58Q270 81 285 114T318 219Q336 291 336 325Z"></path></g><g data-mml-node="mi" transform="translate(3156, 0)"><path data-c="78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(3950.2, 0)"><path data-c="2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path></g><g data-mml-node="mi" transform="translate(4950.4, 0)"><path data-c="63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path></g><g data-mml-node="mo" transform="translate(5661.2, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(6717, 0)"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g></svg></mjx-container></span></div>

Documentation

React components

React componentspropsdescription
MathpixLoaderLoads styles
MathpixMarkdownpropsRenders input text to html element

The MathpixMarkdown React element accepts the following props:

MathpixMarkdown props

proptype defaultdescription
textstingstring that will be converted
alignMathBlockstring centeraligns math-block by this params
displaystring blockblock - the whole space, inline-block - renders in its actual size
showTimeLogboolean falseshows execution time in console
isDisableFancyboolean falsetrue - disables processing of special characters (Example: (c), +, - )
disableRulesarray of strings []You can pass a list of rules for markdown rendering that should be disabled but only if isDisableFancy is not true.
Example: disableRules = ['replacements'] will disable fancy characters processing.
htmlTagsboolean;falseEnables HTML tags in source
xhtmlOutboolean;falseUses / to close single tags (<br />)
breaksboolean;trueConverts \n in paragraphs into <br>
typographerboolean;falseEnables some language-neutral replacement + quotes beautification
linkifyboolean;falseAutoconverts URL-like text to links
widthnumber;1200Sets text container width
outMathTOutputMath;{}Sets options to output html

MathpixMarkdownModel methods

returnsdescription
Style methods:
loadMathJax()booleanAdds a style element into the head of the document and returns true. In case of an error, returns false.example
getMathpixStyleOnly()stringreturns styles as a string.
getMathpixStyle(true)stringreturns styles as a string including styles for container.example
getMathpixFontsStyle()booleanreturns fonts styles as a string.example
Render methods:
markdownToHTML(str, options: TMarkdownItOptions)stringRenders input text to html element as a string.example
render(str, options: optionsMathpixMarkdown)stringRenders input text to HTML element as a string and wraps it in a container. Should be used to render the entire document.example
Parser methods:
parseMarkdownByHTML(htmlString)Arrayparses input html string and returns array of formats.examples
parseMarkdownByElement(htmlElement)Arrayparses input html element and returns array of formats.

TMarkdownItOptions

type defaultdescription
htmlTagsboolean;falseEnables HTML tags in source
xhtmlOutboolean;falseUses / to close single tags (<br />)
breaksboolean;trueConverts \n in paragraphs into <br>
typographerboolean;trueEnables some language-neutral replacement + quotes beautification
linkifyboolean;trueAutoconverts URL-like text to links
widthnumber;1200Sets text container width
lineNumberingboolean;falseSets line numbers. Recommended for synchronization with a text editor.
outMathTOutputMath;{}Sets options to output html

optionsMathpixMarkdown

type defaultdescription
alignMathBlockstring centeraligns math-block
displaystring blockblock - the whole space, inline-block - renders in its actual size
showTimeLogboolean falseshows execution time in console
isDisableFancyboolean falsetrue - disables processing of special characters (Example: (c), +, - )
disableRulesarray of strings []You can pass a list of rules for markdown rendering that should be disabled but only if isDisableFancy is not true.
Example: disableRules = ['replacements'] will disable fancy characters processing.
htmlTagsboolean;falseEnables HTML tags in source
xhtmlOutboolean;falseUses / to close single tags (<br />)
breaksboolean;trueConverts \n in paragraphs into <br>
typographerboolean;trueEnables some language-neutral replacement + quotes beautification
linkifyboolean;trueAutoconverts URL-like text to links
widthnumber;1200Sets text container width
outMathTOutputMath;{}Sets options to output html

TOutputMath

type defaultdescription
include_mathmlboolean falseoutputs mathml <mathml style="display: none"><math>...</math></mathml>
include_asciimathboolean falseoutputs asciimath <asciimath style="display: none">...</asciimath>
include_latexboolean trueoutputs latex <latex style="display: none">...</latex>
include_svgboolean trueoutputs svg <mjx-container class="MathJax" jax="SVG"><svg>...</svg></mjx-container>
include_tsvboolean falseoutputs tsv <tsv style="display: none">...</tsv>
tsv_separators{column: '\t', row: '\n'}Separators for tsv tables

Development

Install dependencies:

$ npm install

Compile TypeScript into JavaScript files.

$ npm run compile

Build the es5 file for node.

$ npm run build

Testing

$ npm test

Keywords

FAQs

Package last updated on 05 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc