{"id":13832311,"url":"https://github.com/pkra/mathjax-node-page","last_synced_at":"2025-10-02T21:30:26.842Z","repository":{"id":57093712,"uuid":"70059922","full_name":"pkra/mathjax-node-page","owner":"pkra","description":"mathjax-node for pages","archived":true,"fork":false,"pushed_at":"2020-09-07T14:24:42.000Z","size":295,"stargazers_count":42,"open_issues_count":10,"forks_count":20,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-01-10T06:34:59.934Z","etag":null,"topics":["cli","html","latex","mathjax","mathjax-node","nodejs","svg","tex"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pkra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-05T12:51:11.000Z","updated_at":"2023-01-28T12:56:54.000Z","dependencies_parsed_at":"2022-08-22T21:40:30.139Z","dependency_job_id":null,"html_url":"https://github.com/pkra/mathjax-node-page","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkra%2Fmathjax-node-page","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkra%2Fmathjax-node-page/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkra%2Fmathjax-node-page/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkra%2Fmathjax-node-page/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pkra","download_url":"https://codeload.github.com/pkra/mathjax-node-page/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234883314,"owners_count":18901365,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","html","latex","mathjax","mathjax-node","nodejs","svg","tex"],"created_at":"2024-08-04T10:01:59.009Z","updated_at":"2025-10-02T21:30:26.511Z","avatar_url":"https://github.com/pkra.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# mathjax-node-page [![Build Status](https://travis-ci.org/pkra/mathjax-node-page.svg?branch=master)](https://travis-ci.org/pkra/mathjax-node-page)\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/pkra/mathjax-node-page.svg)](https://greenkeeper.io/)\n\nThis Node.js module builds on [mathjax-node](https://github.com/mathjax/mathjax-node) and provides processing of larger content fragments\n\n## installation\n\nUse\n\n```\nnpm install mathjax-node-page\n```\n\nto install mathjax-node-page and its dependencies.\n\n## Usage\n\nmathjax-node-page exports `mjpage` which expects four parameters:\n\n```javascript\nmjpage(input, mjpageConfig, mjnodeConfig, callback)\n```\n\nWhere `input` is a string with HTML or `jsdom` object (`JSDOM` class should be acquired via exported `JSDOM`), `pageConfig` specifies page-wide options, and `mjnodeConfig` expects mathjax-node configuration options.\n\nThe defaults for `pageConfig` are\n\n```javascript\n{\n    format: [\"MathML\", \"TeX\", \"AsciiMath\"], // determines type of pre-processors to run\n    output: '', // global override for output option; 'svg', 'html' or 'mml'\n    tex: {}, // configuration options for tex pre-processor, cf. lib/tex.js\n    ascii: {}, // configuration options for ascii pre-processor, cf. lib/ascii.js\n    singleDollars: false, // allow single-dollar delimiter for inline TeX\n    fragment: false, // return body.innerHTML instead of full document\n    cssInline: true,  // determines whether inline css should be added\n    jsdom: {...}, // jsdom-related options\n    displayMessages: false, // determines whether Message.Set() calls are logged\n    displayErrors: false, // determines whether error messages are shown on the console\n    undefinedCharError: false, // determines whether unknown characters are saved in the error array\n    extensions: '', // a convenience option to add MathJax extensions\n    fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/fonts/HTML-CSS', // for webfont urls in the CSS for HTML output\n    MathJax: {}, // options MathJax configuration, see https://docs.mathjax.org\n    errorHandler: (id, wrapperNode, sourceFormula, sourceFormat, errors) =\u003e {...} // function to handle rendering error\n}\n```\nand where `mjnodeConfig` represents mathjax-node configuration options, the defaults are.\n\n```javascript\n{\n  ex: 6, // ex-size in pixels\n  width: 100, // width of math container (in ex) for linebreaking and tags\n  useFontCache: true, // use \u003cdefs\u003e and \u003cuse\u003e in svg output?\n  useGlobalCache: false, // use common \u003cdefs\u003e for all equations?\n  state: mjstate, // track global state\n  linebreaks: false, // do linebreaking?\n  equationNumbers: \"none\", // or \"AMS\" or \"all\"\n  math: \"\", // the math to typeset\n  html: false, // generate HTML output?\n  css: false, // generate CSS for HTML output?\n  mml: false, // generate mml output?\n  svg: false, // generate svg output?\n  speakText: true, // add spoken annotations to output?\n  timeout: 10 * 1000, // 10 second timeout before restarting MathJax\n}\n```\n\n## Advanced usage\n### mathjax-node customization\nmathjax-node-page exports `init` function that allows you to pass in a custom `mathjax-node`  (for example, [mathjax-node-svg2png](https://github.com/pkra/mathjax-node-svg2png)).\n```javascript\nconst mjnode = require('mathjax-node-svg2png');\nmjpage.init(mjnode);\n```\n\nIf your custom mathjax-node provides new output options, you can add them by calling `addOutput`. As a second parameter, you can pass custom output handler, which is a function that modifies a DOM element with the conversion result.  The default output handler behavior is to write contents to `wrapper.innerHTML`.\n```javascript\nmjpage.addOutput('png', (wrapper, data) =\u003e {\n\twrapper.innerHTML = `\u003cimg src=\"${data}\"\u003e`;\n});\n// ...now you can use standard mathjax-node-page API\n```\n\nReset to default mathjax-node behavior by calling `init` with empty parameters. Ensure that all your current mathjax-node-page tasks have been completed before calling it.\n```javascript\nmjpage.init();  // reset back to default mathjax-node\n```\n\n### Events\n`mjpage` runs jobs which inherit `EventEmitter` and provide the following event hooks.\nAdd the corresponding event handlers to manipulate the input/output and DOM before/after conversion.\n\nAll the event handlers are destroyed when job ends to prevent memory leaks.\n\n#### Formula conversion events\n* `beforeConversion` -\u003e `handler(parsedFormula)`: runs before individual formula conversion started, but after initial DOM processing. All the formulas are wrapped in `\u003cscript type=\"...\"\u003e` tags, where `@type` is one of the following:\n```javascript\nconst scripts = document.querySelectorAll(`\n    script[type=\"math/TeX\"],\n    script[type=\"math/inline-TeX\"],\n    script[type=\"math/AsciiMath\"],\n    script[type=\"math/MathML\"],\n    script[type=\"math/MathML-block\"]`\n);\n```\n* `afterConersion` -\u003e `handler(parsedFormula)`: runs after individual formula conversion completed and DOM was changed. Formula DOM node is a `\u003cspan class=\"mjpage...\"\u003e` wrapper whose contents are the conversion result.\n\nAll formula conversion events pass `ParsedFormula` instance to the event handler.\n\n```javascript\n{\n    id, // index of formula on the page\n    jobID, // mjpage job ID; formulas belonging to the same page run have the same jobID\n    node, // DOM node with the formula (contents change before and after conversion)\n    sourceFormula, // the source formula\n    sourceFormat, // the source formula format (e.g. \"inline-TeX\")\n    outputFormula, // the converted formula result from mathjax-node typeset function; use outputFormula[outputFormat] to get the resulting formula string\n    outputFormat // the resulting formula format (e.g. \"svg\")\n}\n```\n\n#### Page conversion events\n* `beforeSerialiation` -\u003e `handler(document, css`): runs when converted page DOM was prepared immediately before serialization. Use to manipulate resulting page DOM. The event handler receives `document` node (jsdom) and page `css`. Won't trigger if `input` is a `jsdom` object.\n\nIf `input` is a HTML string, `mjpage` function callback receives result after the DOM serialization.  \nIf `input` is a `jsdom` object, `mjpage` function callback receives `jsdom` object itself.\n\n#### Error handling\nWhen a rendering error occurs, `config.errorHandler` will be called. These arguments are passed:\n\n* `id`: index of formula on the page.\n* `wrapperNode`: The jsdom HTMLElement object where the rendered math should be put.\n* `sourceFormula`: The input math code.\n* `sourceFormat`: The format of input math code -- e.g. `inline-TeX` or `TeX`.\n* `errors`: A array of strings of MathJax-Node returned errors.\n\nModify the `wrapperNode` object to show some error message to user. The default error handling function is printing the error with `console.log`.\n\n#### Example\n```javascript\nmjpage(input, {\n    format: [\"TeX\"]\n}, {\n    svg: true\n}, function(output) {\n    // output is your final result\n})\n.on('afterConversion', function(parsedFormula) {\n    // manipulate parsed result and DOM at your will\n    // see description of parsedFormula object above\n});\n```\n\n\n\n## CLI\n\nmathjax-node-page installs a CLI tool. Run `mjpage` to print usage instructions.\n\n### Example\n\n```javascript\nconst mjpage = require('../lib/main.js').mjpage;\nconst fs = require('fs');\nconst input = fs.readFileSync('input.html');\n\nmjpage(input, {format: [\"TeX\"]}, {svg: true}, function(output) {\n    console.log(output); // resulting HTML string\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkra%2Fmathjax-node-page","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpkra%2Fmathjax-node-page","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkra%2Fmathjax-node-page/lists"}