Skip to content
Markdown rendering with Latex extras (equations, tables, ...) for the scientific community
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
es5
examples/mathpix-markdown-it-node-examples
lib
src
.babelrc
.gitignore
.npmignore
.yarnignore
README.md
package.json
tsconfig.json
webpack.config.js
webpack.dev.js

README.md

mathpix-markdown-it

mathpix-markdown-it is an extended Markdown-it specification optimized for mathematics and science. It not only supports LaTeX math mode (e.g. standard Markdown), it also supports some aspects of LaTeX text mode, such as title, author, abstract, tabular, figure environment. Using marking Markdown, LaTeX, MathML you get a high-quality display of notations on HTML-pages.

See the documentation for more details

This repository contains mathpix-markdown-it source files written in TypeScript. They are compiled into JavaScript files.

Install

node.js

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

Usage examples

Sample for React Component

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>
    );
  }
}

props

prop type default description
text sting string that will be converted
alignMathBlock string center should align math-block
display string block block - the whole space, inline-block - renders in its actual size
showTimeLog boolean false should show execution time in console
isDisableFancy boolean false true - disables processing of special characters (Example: (c), +, - )
disableRules array of strings [] We 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.
htmlTags boolean;false Enable HTML tags in source
xhtmlOut boolean;false Use / to close single tags (<br />)
breaks boolean;true Convert \n in paragraphs into <br>
typographer boolean;false Enable some language-neutral replacement + quotes beautification
linkify boolean;false Autoconvert URL-like text to links
width number;1200 Text container width

Sample for non-React UI code

You can also use certain functions to get styles and rendering in html:

const isLoad = MathpixMarkdownModel.loadMathJax(); - If styles have not been added yet - add them to the style element. In case of an error, returns false.

const mathpixStyles = MathpixMarkdownModel.getMathpixStyleOnly(); - It returns styles as a string.

const mathpixFontsStyles = MathpixMarkdownModel.getMathpixFontsStyle(); - It returns fonts styles as a string.

const html = MathpixMarkdownModel.markdownToHTML(content, options: TMarkdownItOptions); - It returns a rendered html element as a string.

const htmlMM = MathpixMarkdownModel.render(text, options: optionsMathpixMarkdown); - Returns a rendered HTML element as a string and wraps it in a container. Should be used to render the entire document.

TMarkdownItOptions

type default description
htmlTags boolean;false Enable HTML tags in source
xhtmlOut boolean;false Use / to close single tags (<br />)
breaks boolean;true Convert \n in paragraphs into <br>
typographer boolean;true Enable some language-neutral replacement + quotes beautification
linkify boolean;true Autoconvert URL-like text to links
width number;1200 Text container width
lineNumbering boolean;false Recommended for synchronization with a text editor.

optionsMathpixMarkdown

type default description
alignMathBlock string center should align math-block
display string block block - the whole space, inline-block - renders in its actual size
showTimeLog boolean false should show execution time in console
isDisableFancy boolean false true - disables processing of special characters (Example: (c), +, - )
disableRules array of strings [] We 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.
htmlTags boolean;false Enable HTML tags in source
xhtmlOut boolean;false Use / to close single tags (<br />)
breaks boolean;true Convert \n in paragraphs into <br>
typographer boolean;true Enable some language-neutral replacement + quotes beautification
linkify boolean;true Autoconvert URL-like text to links
width number;1200 Text container width

Sample for node

Example to use mathpix-markdown-it in the node application

const {MathpixMarkdownModel} = require('mathpix-markdown-it'); 

or

const {MathpixMarkdownModel} = require('mathpix-markdown-it/es5');
const htmlMM = MathpixMarkdownModel.render(text, options);
const mathpixStyles = MathpixMarkdownModel.getMathpixStyleOnly();

Before using mathpix-markdown-it in node applications, should be to 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 to use mathpix-markdown-it 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>

Development

Get Started

- Install dependencies

$ npm install

- Compile TypeScript into JavaScript files.

$ npm run compile

- Build the es5 file for node.

$ npm run build
You can’t perform that action at this time.