goessner-markdown-it-texmath-1513a20/0000755000175000017500000000000014244343111020632 5ustar supermariosupermariogoessner-markdown-it-texmath-1513a20/readme.md0000644000175000017500000003017414244343111022416 0ustar supermariosupermario[![License](https://img.shields.io/github/license/goessner/markdown-it-texmath.svg)](https://github.com/goessner/markdown-it-texmath/blob/master/licence.txt) [![npm](https://img.shields.io/npm/v/markdown-it-texmath.svg)](https://www.npmjs.com/package/markdown-it-texmath) [![npm](https://img.shields.io/npm/dt/markdown-it-texmath.svg)](https://www.npmjs.com/package/markdown-it-texmath) [![](https://data.jsdelivr.com/v1/package/npm/markdown-it-texmath/badge)](https://www.jsdelivr.com/package/npm/markdown-it-texmath) # markdown-it-texmath Add TeX math equations to your Markdown documents rendered by [markdown-it](https://github.com/markdown-it/markdown-it) parser. [KaTeX](https://github.com/Khan/KaTeX) is used as a fast math renderer. ## What's New? * `markdown-it-texmath` reached quite a stable state with version 1.0 . * Native `begin{...}` / `end{...}` environments are supported as delimiters itself ... thanks to [William Stein](https://github.com/williamstein) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/41). ``` \begin{equation} a^2+b^2=c^2 \end{equation} ``` They can even be nested. ``` \begin{equation} \begin{pmatrix} A & B \\ B & C \end{pmatrix} \end{equation} ``` * Different delimiters can be merged. Delimiters options property supports array notation for that. Example: `delimiters: ['dollars','beg_end']`. Thanks to [Liu YongLiang](https://github.com/tlylt) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/40). ## Features Simplify the process of authoring markdown documents containing math formulas. This extension is a comfortable tool for scientists, engineers and students with markdown as their first choice document format. * Macro support * Simple formula numbering * Inline math with tables, lists and blockquote. * User setting delimiters: * `'dollars'` (default) * inline: `$...$` or `$$...$$` * display: `$$...$$` * display + equation number: `$$...$$ (1)` * `'brackets'` * inline: `\(...\)` * display: `\[...\]` * display + equation number: `\[...\] (1)` * `'doxygen'` * inline: `\f$...$\f` * display: `\f[...\f]` * display + equation number: `\f[...\f] (1)` * `'gitlab'` * inline: ``$`...`$`` * display: `` ```math ... ``` `` * display + equation number: `` ```math ... ``` (1)`` * `'julia'` * inline: `$...$` or ``` ``...`` ``` * display: `` ```math ... ``` `` * display + equation number: `` ```math ... ``` (1)`` * `'kramdown'` * inline: ``$$...$$`` * display: `$$...$$` * display + equation number: `$$...$$ (1)` * `'beg_end'` (display math only) * inline: N/A * display: `begin{...}...end{...}` * display + equation number: N/A ## Show me View a [test table](https://goessner.github.io/markdown-it-texmath/index.html). [try it out ...](https://goessner.github.io/markdown-it-texmath/markdown-it-texmath-demo.html) ## Use with `node.js` Install the extension. Verify having `markdown-it` and `katex` already installed . ``` npm install markdown-it-texmath ``` Use it with JavaScript. ```js const tm = require('markdown-it-texmath'); const md = require('markdown-it')({html:true}) .use(tm, { engine: require('katex'), delimiters: 'dollars', katexOptions: { macros: {"\\RR": "\\mathbb{R}"} } }); const str = "Euler\'s identity $e^{i\\pi}+1=0$ is a beautiful formula in $\\RR^2$."; md.render(str); ``` ## Use in Browser ```html test
``` ## CDN Use following links for `texmath.js` and `texmath.css` * `https://cdn.jsdelivr.net/npm/markdown-it-texmath/texmath.min.js` * `https://cdn.jsdelivr.net/npm/markdown-it-texmath/css/texmath.min.css` ## Dependencies * [`markdown-it`](https://github.com/markdown-it/markdown-it): Markdown parser done right. Fast and easy to extend. * [`katex`](https://github.com/Khan/KaTeX): This is where credits for fast rendering TeX math in HTML go to. ## ToDo nothing yet ## FAQ * __Support of inline syntax of display math ?__ * Inline syntax of display math with `dollars` mode is supported starting from version "0.7.0". So `'This formula $$a+b=c$$ will result in display math presentation'`, i.e. gets displayed on a separate line. For *true* inline math use `$..$` mode like before. * __Multiline diplay math in `blockquote` block possible ?__ * Display math inside of `blockquote` blocks is able to span multiple lines with version "0.7.3". * __`markdown-it-texmath` with React Native does not work, why ?__ * `markdown-it-texmath` is using regular expressions with `y` [(sticky) property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) and cannot avoid this. The use of the `y` flag in regular expressions means the plugin is not compatible with React Native (which as of now doesn't support it and throws an error `Invalid flags supplied to RegExp constructor`). * __Why doesn't `markdown-it-texmath` work with other engines ?__ * `markdown-it-texmath` is a personal project of mine. As it does very well with KaTeX what I want it to do, I offer it to the public as an open source plugin. I do not have time or interest to integrate other math engines. But if someone wants to help here out, pull requests are always welcome. ## CHANGELOG ### [1.0.0] on May 28, 2022 * Update to KaTeX version `0.15.6`. * Update to `markdown-it` `13.0.1`. * [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/42) in level handling with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein). * [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/43) in mapping with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein). * Supporting native `begin{..}` / `end{...}` environments as delimiters itself. Thanks to [williamstein](https://github.com/williamstein) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/41). * Merging different delimiters for simultaneous use via `delimiters:[, ]`. Thanks to [tlylt](https://github.com/tlylt) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/40). ### [0.9.7] on December 07, 2021 * Redundant `` end-tag with display-mode equations removed. All modes were affected ... invisible effect though. Thanks to [yuanbug](https://github.com/yuanbug) for reporting. ### [0.9.6] on November 16, 2021 * Small bug in 'dollars' inline-display-mode regex fixed. ### [0.9.5] on November 12, 2021 * More Optimization done with the 'dollars' regexes. ### [0.9.4] on November 12, 2021 * Optimizing the 'dollars' regexes. Thanks to [Erik Demaine](https://github.com/edemaine). * Adding 'doxygen' delimiters support. ([#31](https://github.com/goessner/markdown-it-texmath/issues/31)). Thanks to [arwedus](https://github.com/arwedus). ### [0.9.3] on October 28, 2021 * Fixing newline bug in 'dollars' regexes. ([#32](https://github.com/goessner/markdown-it-texmath/issues/32)). ### [0.9.2] on October 27, 2021 * Fixing disability to include escaped dollar when using dollars delimiters ([#32](https://github.com/goessner/markdown-it-texmath/issues/32)). ### [0.9.1] on July 02, 2021 * potential XSS vulnerability with equation numbers fixed ([#29](https://github.com/goessner/markdown-it-texmath/pull/29)). ### [0.9.0] on May 26, 2021 * KaTeX options `{katexOptions:...}` within markdown-it-texmath options are directly handed over to katex. See [KaTeX options](https://katex.org/docs/options.html). Thanks to [Kirill](https://github.com/xuhcc) for [pull request](https://github.com/goessner/markdown-it-texmath/pull/19). * Potential [error message XSS vulnerability](https://github.com/goessner/markdown-it-texmath/pull/22) fixed. Thanks to [CatNose](https://github.com/catnose99). * Using new boolean markdown-it-texmath `outerSpace` option, inline rules `dollars` explicitly require surrounding spaces when set to `true` (default is `false` for backwards compatibility). This is primarily a guard against misinterpreting single `$`'s in normal markdown text. * Update to KaTeX version 0.13.11. ### [0.8.0] on July 10, 2020 * Infinite loop bug with `gitlab` mode and display math inside `blockquote` section removed. * Fundamental redesign of display math implementation. * Update to KaTeX version 0.12.0. ### [0.7.2] on June 22, 2020 * Regex bug with `gitlab` mode removed. ### [0.7.0] on June 14, 2020 * Experimental `pandoc` mode removed. Enhanced `dollars` mode now does, what `pandoc` mode was requiring. * With `dollars` mode inline math expression `$$..$$` will result in display math presentation now. Adding equation numbers `$$..$$(1)` is not supported in inline syntax. * Significant code redesign and regular expression optimization results in more compact code and performance gain ... not measured though. * Bug with display math inside of `blockquote` blocks removed. ### [0.6.9] on June 11, 2020 * Now display math inside of `blockquote` blocks can span multiple lines, provided that every line starts with a `>` character. * Possible cause of [blockquote bug(https://github.com/goessner/mdmath/issues/50)] presumably eliminated. * Update to `markdown-it` version 11.0.0 ### [0.6.7] on April 29, 2020 * Now supporting [katex options](https://katex.org/docs/options.html). Thanks goto [Kirill](https://github.com/xuhcc). ### [0.6.6] on April 07, 2020 * Removed a small bug in activation method. ### [0.6.5] on April 05, 2020 * Hand instance of `katex` over to `markdown-it-texmath` using `options.engine` object. Works with `node.js` and browsers. With `node.js` `options.engine` entry `{ engine:'katex' }` as a string also works. * As a consequence of the topic before, the `use` method of `markdown-it-texmath` is deprecated now. * Add beta support for [Pandoc](https://docs.julialang.org/en/v1/stdlib/Markdown/) syntax on [request](https://github.com/goessner/markdown-it-texmath/issues/18). Here waiting for test results. * Using [jsdelivr](https://www.jsdelivr.com/package/npm/markdown-it-texmath?path=css) as cdn from now on. ### [0.6.0] on October 04, 2019 * Add support for [Julia Markdown](https://docs.julialang.org/en/v1/stdlib/Markdown/) on [request](https://github.com/goessner/markdown-it-texmath/issues/15). ### [0.5.5] on February 07, 2019 * Remove [rendering bug with brackets delimiters](https://github.com/goessner/markdown-it-texmath/issues/9). ### [0.5.4] on January 20, 2019 * Remove pathological [bug within blockquotes](https://github.com/goessner/mdmath/issues/50). ### [0.5.3] on November 11, 2018 * Add support for Tex macros (https://katex.org/docs/supported.html#macros) . * Bug with [brackets delimiters](https://github.com/goessner/markdown-it-texmath/issues/9) . ### [0.5.2] on September 07, 2018 * Add support for [Kramdown](https://kramdown.gettalong.org/) . ### [0.5.0] on August 15, 2018 * Fatal blockquote bug investigated. Implemented workaround to vscode bug, which has finally gone with vscode 1.26.0 . ### [0.4.6] on January 05, 2018 * Escaped underscore bug removed. ### [0.4.5] on November 06, 2017 * Backslash bug removed. ### [0.4.4] on September 27, 2017 * Modifying the `block` mode regular expression with `gitlab` delimiters, so removing the `newline` bug. ## License `markdown-it-texmath` is licensed under the [MIT License](./license.txt) © [Stefan Gössner](https://github.com/goessner) goessner-markdown-it-texmath-1513a20/.gitignore0000644000175000017500000000001414244343111022615 0ustar supermariosupermarionode_modulesgoessner-markdown-it-texmath-1513a20/package-lock.json0000644000175000017500000001402114244343111024044 0ustar supermariosupermario{ "name": "markdown-it-texmath", "version": "0.9.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "markdown-it-texmath", "version": "0.9.7", "license": "MIT", "dependencies": { "mathjax": "^3.2.1" }, "devDependencies": { "katex": "^0.15.6", "markdown-it": "^13.0.1" } }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, "node_modules/commander": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", "dev": true, "engines": { "node": ">= 12" } }, "node_modules/entities": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/entities/-/entities-3.0.1.tgz", "integrity": "sha512-WiyBqoomrwMdFG1e0kqvASYfnlb0lp8M5o5Fw2OFq1hNZxxcNk8Ik0Xm7LxzBhuidnZB/UtBqVCgUz3kBOP51Q==", "dev": true, "engines": { "node": ">=0.12" }, "funding": { "url": "https://github.com/fb55/entities?sponsor=1" } }, "node_modules/katex": { "version": "0.15.6", "resolved": "https://registry.npmjs.org/katex/-/katex-0.15.6.tgz", "integrity": "sha512-UpzJy4yrnqnhXvRPhjEuLA4lcPn6eRngixW7Q3TJErjg3Aw2PuLFBzTkdUb89UtumxjhHTqL3a5GDGETMSwgJA==", "dev": true, "funding": [ "https://opencollective.com/katex", "https://github.com/sponsors/katex" ], "dependencies": { "commander": "^8.0.0" }, "bin": { "katex": "cli.js" } }, "node_modules/linkify-it": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-4.0.1.tgz", "integrity": "sha512-C7bfi1UZmoj8+PQx22XyeXCuBlokoyWQL5pWSP+EI6nzRylyThouddufc2c1NDIcP9k5agmN9fLpA7VNJfIiqw==", "dev": true, "dependencies": { "uc.micro": "^1.0.1" } }, "node_modules/markdown-it": { "version": "13.0.1", "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-13.0.1.tgz", "integrity": "sha512-lTlxriVoy2criHP0JKRhO2VDG9c2ypWCsT237eDiLqi09rmbKoUetyGHq2uOIRoRS//kfoJckS0eUzzkDR+k2Q==", "dev": true, "dependencies": { "argparse": "^2.0.1", "entities": "~3.0.1", "linkify-it": "^4.0.1", "mdurl": "^1.0.1", "uc.micro": "^1.0.5" }, "bin": { "markdown-it": "bin/markdown-it.js" } }, "node_modules/mathjax": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/mathjax/-/mathjax-3.2.1.tgz", "integrity": "sha512-blUch14trKnfQHjDjy1kdg5bN8jK0bdHbkerQBKCrZ3Anpb81zZ7xnj5J55vsqQoG+Irz3BHBDzRssjeehkzxg==" }, "node_modules/mdurl": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=", "dev": true }, "node_modules/uc.micro": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==", "dev": true } }, "dependencies": { "argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, "commander": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", "dev": true }, "entities": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/entities/-/entities-3.0.1.tgz", "integrity": "sha512-WiyBqoomrwMdFG1e0kqvASYfnlb0lp8M5o5Fw2OFq1hNZxxcNk8Ik0Xm7LxzBhuidnZB/UtBqVCgUz3kBOP51Q==", "dev": true }, "katex": { "version": "0.15.6", "resolved": "https://registry.npmjs.org/katex/-/katex-0.15.6.tgz", "integrity": "sha512-UpzJy4yrnqnhXvRPhjEuLA4lcPn6eRngixW7Q3TJErjg3Aw2PuLFBzTkdUb89UtumxjhHTqL3a5GDGETMSwgJA==", "dev": true, "requires": { "commander": "^8.0.0" } }, "linkify-it": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-4.0.1.tgz", "integrity": "sha512-C7bfi1UZmoj8+PQx22XyeXCuBlokoyWQL5pWSP+EI6nzRylyThouddufc2c1NDIcP9k5agmN9fLpA7VNJfIiqw==", "dev": true, "requires": { "uc.micro": "^1.0.1" } }, "markdown-it": { "version": "13.0.1", "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-13.0.1.tgz", "integrity": "sha512-lTlxriVoy2criHP0JKRhO2VDG9c2ypWCsT237eDiLqi09rmbKoUetyGHq2uOIRoRS//kfoJckS0eUzzkDR+k2Q==", "dev": true, "requires": { "argparse": "^2.0.1", "entities": "~3.0.1", "linkify-it": "^4.0.1", "mdurl": "^1.0.1", "uc.micro": "^1.0.5" } }, "mathjax": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/mathjax/-/mathjax-3.2.1.tgz", "integrity": "sha512-blUch14trKnfQHjDjy1kdg5bN8jK0bdHbkerQBKCrZ3Anpb81zZ7xnj5J55vsqQoG+Irz3BHBDzRssjeehkzxg==" }, "mdurl": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=", "dev": true }, "uc.micro": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==", "dev": true } } } goessner-markdown-it-texmath-1513a20/texmath.js0000644000175000017500000003260714244343111022652 0ustar supermariosupermario/*--------------------------------------------------------------------------------------------- * Copyright (c) Stefan Goessner - 2017-22. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; function escapeHTML(text) { return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function texmath(md, options) { const delimiters = texmath.mergeDelimiters(options && options.delimiters); const outerSpace = options && options.outerSpace || false; // inline rules, effectively `dollars` require surrounding spaces, i.e ` $\psi$ `, to be accepted as inline formulas. This is primarily a guard against misinterpreting single `$`'s in normal markdown text (relevant for inline math only. Default: `false`, for backwards compatibility). const katexOptions = options && options.katexOptions || {}; katexOptions.throwOnError = katexOptions.throwOnError || false; katexOptions.macros = katexOptions.macros || options && options.macros; // ensure backwards compatibility if (!texmath.katex) { // else ... deprecated `use` method was used ... if (options && typeof options.engine === 'object') { texmath.katex = options.engine; } else if (typeof module === "object") texmath.katex = require('katex'); else // artifical error object. texmath.katex = { renderToString() { return 'No math renderer found.' } }; } // inject inline rules to markdown-it for (const rule of delimiters.inline) { if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true; md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions)); } // inject block rules to markdown-it for (const rule of delimiters.block) { md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ? .replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions)); } } texmath.mergeDelimiters = function(delims) { const delimsArr = Array.isArray(delims) ? delims : typeof delims === "string" ? [delims] : ['dollars']; const delimiters = { inline:[], block:[]}; // target of merge process ... for (const delim of delimsArr) // merge them into delimiters ... if (delim in texmath.rules) { delimiters.inline.push(...texmath.rules[delim].inline); delimiters.block.push(...texmath.rules[delim].block); } return delimiters; } // texmath.inline = (rule) => dollar; // just for debugging/testing .. texmath.inline = (rule) => function(state, silent) { const pos = state.pos; const str = state.src; const pre = str.startsWith(rule.tag, rule.rex.lastIndex = pos) && (!rule.pre || rule.pre(str, rule.outerSpace, pos)); // valid pre-condition ... const match = pre && rule.rex.exec(str); const res = !!match && pos < rule.rex.lastIndex && (!rule.post || rule.post(str, rule.outerSpace, rule.rex.lastIndex - 1)); if (res) { if (!silent) { const token = state.push(rule.name, 'math', 0); token.content = match[1]; token.markup = rule.tag; } state.pos = rule.rex.lastIndex; } return res; } texmath.block = (rule) => function block(state, begLine, endLine, silent) { const pos = state.bMarks[begLine] + state.tShift[begLine]; const str = state.src; const pre = str.startsWith(rule.tag, rule.rex.lastIndex = pos) && (!rule.pre || rule.pre(str, false, pos)); // valid pre-condition .... const match = pre && rule.rex.exec(str); const res = !!match && pos < rule.rex.lastIndex && (!rule.post || rule.post(str, false, rule.rex.lastIndex - 1)); if (res && !silent) { // match and valid post-condition ... const endpos = rule.rex.lastIndex - 1; let curline; for (curline = begLine; curline < endLine; curline++) if (endpos >= state.bMarks[curline] + state.tShift[curline] && endpos <= state.eMarks[curline]) // line for end of block math found ... break; // "this will prevent lazy continuations from ever going past our end marker" // s. https://github.com/markdown-it/markdown-it-container/blob/master/index.js const lineMax = state.lineMax; const parentType = state.parentType; state.lineMax = curline; state.parentType = 'math'; if (parentType === 'blockquote') // remove all leading '>' inside multiline formula match[1] = match[1].replace(/(\n*?^(?:\s*>)+)/gm,''); // begin token let token = state.push(rule.name, 'math', 0); // 'math_block' token.block = true; token.tag = rule.tag; token.markup = ''; token.content = match[1]; token.info = match[match.length-1]; // eq.no token.map = [ begLine, curline+1 ]; // token.hidden = true; // end token ... superfluous ... state.parentType = parentType; state.lineMax = lineMax; state.line = curline+1; } return res; } texmath.render = function(tex,displayMode,options) { options.displayMode = displayMode; let res; try { res = texmath.katex.renderToString(tex, options); } catch(err) { res = escapeHTML(`${tex}:${err.message}`) } return res; } // ! deprecated ... use options ! texmath.use = function(katex) { // math renderer used ... texmath.katex = katex; // ... katex solely at current ... return texmath; } /* function dollar(state, silent) { var start, max, marker, matchStart, matchEnd, token, pos = state.pos, ch = state.src.charCodeAt(pos); if (ch !== 0x24) { return false; } // $ start = pos; pos++; max = state.posMax; while (pos < max && state.src.charCodeAt(pos) === 0x24) { pos++; } marker = state.src.slice(start, pos); matchStart = matchEnd = pos; while ((matchStart = state.src.indexOf('$', matchEnd)) !== -1) { matchEnd = matchStart + 1; while (matchEnd < max && state.src.charCodeAt(matchEnd) === 0x24) { matchEnd++; } if (matchEnd - matchStart === marker.length) { if (!silent) { token = state.push('math_inline', 'math', 0); token.markup = marker; token.content = state.src.slice(pos, matchStart) .replace(/[ \n]+/g, ' ') .trim(); } state.pos = matchEnd; return true; } } if (!silent) { state.pending += marker; } state.pos += marker.length; return true; }; */ // used for enable/disable math rendering by `markdown-it` texmath.inlineRuleNames = ['math_inline','math_inline_double']; texmath.blockRuleNames = ['math_block','math_block_eqno']; texmath.$_pre = (str,outerSpace,beg) => { const prv = beg > 0 ? str[beg-1].charCodeAt(0) : false; return outerSpace ? !prv || prv === 0x20 // space (avoiding regex's for performance reasons) : !prv || prv !== 0x5c // no backslash, && (prv < 0x30 || prv > 0x39); // no decimal digit .. before opening '$' } texmath.$_post = (str,outerSpace,end) => { const nxt = str[end+1] && str[end+1].charCodeAt(0); return outerSpace ? !nxt || nxt === 0x20 // space (avoiding regex's for performance reasons) || nxt === 0x2e // '.' || nxt === 0x2c // ',' || nxt === 0x3b // ';' : !nxt || nxt < 0x30 || nxt > 0x39; // no decimal digit .. after closing '$' } texmath.rules = { brackets: { inline: [ { name: 'math_inline', rex: /\\\((.+?)\\\)/gy, tmpl: '$1', tag: '\\(' } ], block: [ { name: 'math_block_eqno', rex: /\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)/gmy, tmpl: '
$1($2)
', tag: '\\[' }, { name: 'math_block', rex: /\\\[([\s\S]+?)\\\]/gmy, tmpl: '
$1
', tag: '\\[' } ] }, doxygen: { inline: [ { name: 'math_inline', rex: /\\f\$(.+?)\\f\$/gy, tmpl: '$1', tag: '\\f$' } ], block: [ { name: 'math_block_eqno', rex: /\\f\[([^]+?)\\f\]\s*?\(([^)\s]+?)\)/gmy, tmpl: '
$1($2)
', tag: '\\f[' }, { name: 'math_block', rex: /\\f\[([^]+?)\\f\]/gmy, tmpl: '
$1
', tag: '\\f[' } ] }, gitlab: { inline: [ { name: 'math_inline', rex: /\$`(.+?)`\$/gy, tmpl: '$1', tag: '$`' } ], block: [ { name: 'math_block_eqno', rex: /`{3}math\s*([^`]+?)\s*?`{3}\s*\(([^)\r\n]+?)\)/gm, tmpl: '
$1($2)
', tag: '```math' }, { name: 'math_block', rex: /`{3}math\s*([^`]*?)\s*`{3}/gm, tmpl: '
$1
', tag: '```math' } ] }, julia: { inline: [ { name: 'math_inline', rex: /`{2}([^`]+?)`{2}/gy, tmpl: '$1', tag: '``' }, { name: 'math_inline', rex: /\$((?:\S?)|(?:\S.*?\S))\$/gy, tmpl: '$1', tag: '$', spaceEnclosed: false, pre: texmath.$_pre, post: texmath.$_post, } ], block: [ { name: 'math_block_eqno', rex: /`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)/gmy, tmpl: '
$1($2)
', tag: '```math' }, { name: 'math_block', rex: /`{3}math\s+?([^`]+?)\s+?`{3}/gmy, tmpl: '
$1
', tag: '```math' } ] }, kramdown: { inline: [ { name: 'math_inline', rex: /\${2}(.+?)\${2}/gy, tmpl: '$1', tag: '$$' } ], block: [ { name: 'math_block_eqno', rex: /\${2}([^$]+?)\${2}\s*?\(([^)\s]+?)\)/gmy, tmpl: '
$1($2)
', tag: '$$' }, { name: 'math_block', rex: /\${2}([^$]+?)\${2}/gmy, tmpl: '
$1
', tag: '$$' } ] }, beg_end: { inline: [], block: [ { name: "math_block", rex: /(\\(?:begin)\{([a-z]+)\}[\s\S]+?\\(?:end)\{\2\})/gmy, // regexp to match \begin{...}...\end{...} environment. tmpl: "
$1
", tag: "\\" } ] }, dollars: { inline: [ { name: 'math_inline_double', rex: /\${2}([^$]*?[^\\])\${2}/gy, tmpl: '
$1
', tag: '$$', displayMode: true, pre: texmath.$_pre, post: texmath.$_post }, { name: 'math_inline', rex: /\$((?:[^\s\\])|(?:\S.*?[^\s\\]))\$/gy, tmpl: '$1', tag: '$', outerSpace: false, pre: texmath.$_pre, post: texmath.$_post } ], block: [ { name: 'math_block_eqno', rex: /\${2}([^$]*?[^\\])\${2}\s*?\(([^)\s]+?)\)/gmy, tmpl: '
$1($2)
', tag: '$$' }, { name: 'math_block', rex: /\${2}([^$]*?[^\\])\${2}/gmy, tmpl: '
$1
', tag: '$$' } ] } }; if (typeof module === "object" && module.exports) module.exports = texmath; goessner-markdown-it-texmath-1513a20/license.txt0000644000175000017500000000206314244343111023016 0ustar supermariosupermarioMIT License Copyright (c) 2013-17 Stefan Goessner Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. goessner-markdown-it-texmath-1513a20/package.json0000644000175000017500000000104714244343111023122 0ustar supermariosupermario{ "name": "markdown-it-texmath", "version": "1.0.0", "description": "markdown-it extension for rendering TeX Math", "keywords": [ "TeX", "LaTeX", "KaTeX", "markdown", "markdown-it", "markdown-it-plugin", "math", "math formulas" ], "author": "Stefan Goessner ", "main": "texmath.js", "repository": { "type": "git", "url": "git://github.com/goessner/markdown-it-texmath.git" }, "license": "MIT", "devDependencies": { "katex": "^0.15.6", "markdown-it": "^13.0.1" } } goessner-markdown-it-texmath-1513a20/test/0000755000175000017500000000000014244343111021611 5ustar supermariosupermariogoessner-markdown-it-texmath-1513a20/test/tests.js0000644000175000017500000001014414244343111023311 0ustar supermariosupermario"use strict"; const tests = [ { valid: true, src: "$a$", comment: "single character inline equation." }, { valid: true, src: "$\\varphi$", comment: "inline equation with single greek character" }, { valid: true, src: "$1+1=2$", comment: "simple equation starting and ending with numbers." }, { valid: true, src: "$1+1<3$", comment: "simple equation including special html character." }, { valid: true, src: "$a \\backslash$", comment: "equation including backslashes." }, { valid: true, src: "You get 3$ if you solve $1+2$", comment: "use of currency symbol" }, { valid: true, src: "If you solve $1+2$ you get $3", comment: "use of currency symbol" }, { valid: true, src: "$\\frac{1}{2}$", comment: "inline fraction" }, { valid: true, src: "$\\begin{pmatrix}x\\\\y\\end{pmatrix}$", comment: "inline column vector" }, { valid: true, src: "${\\tilde\\bold e}_\\alpha$", comment: "inline bold vector notation" }, { valid: true, src: "$a^{b}$", comment: "exponentiation" }, { valid: true, src: "$a^\*b$ with $a^\*$", comment: "conjugate complex" }, { valid: true, src: "$$e_\\alpha$$", comment: "single block equation, greek index" }, { valid: true, src: "$$1+1=2$$", comment: "display equation on its own single line." }, { valid: true, src: "${e}_x$\n\n$$e_\\alpha$$", comment: "inline equation followed by block equation." }, { valid: true, src: "$$c{\\bold e}_x = a{\\bold e}_\\alpha - b\\tilde{\\bold e}_\\alpha$$", comment: "underline tests" }, { valid: true, src: "a$1+1=2$\n$1+1=2$b\nc$x$d", comment: "non-numeric character before opening $ or\nafter closing $ or both is allowed." }, { valid: true, src: "$x$ $ ", comment: "following dollar character '$' is allowed." }, { valid: true, src: "$x$ $y$", comment: "consecutive inline equations." }, { valid: true, src: "so-what is $x$", comment: "inline equation after '-' sign in text." }, { valid: true, src: "$$\n1+1=2\n$$", comment: "display equation with line breaks." }, { valid: true, src: "$$\\begin{matrix}\n f & = & 2 + x + 3 \\\\\n & = & 5 + x \n\\end{matrix}$$", comment: "multiline equation." }, { valid: true, src: "$$\\begin{pmatrix}x_2 \\\\ y_2 \\end{pmatrix} = \n\\begin{pmatrix} A & B \\\\ C & D \\end{pmatrix}\\cdot\n\\begin{pmatrix} x_1 \\\\ y_1 \\end{pmatrix}$$", comment: "vector equation." }, { valid: true, src: "$$f(x) = x^2 - 1$$ (1)", comment: "display equation with equation number." }, { valid: true, src: "`code`$a-b$", comment: "inline equation following code section." }, { valid: true, src: "```\ncode\n```\n$$a+b$$", comment: "equation following code block." }, { valid: true, src: "```\ncode\n```\n$$a+b$$(1)", comment: "numbered equation following code block." }, { valid: true, src: "1. $1+2$\n2. $2+3$\n 1. $3+4$", comment: "Equations in list." }, { valid: true, src: "$\\sum\_{i=1}^n$", comment: "Inline sum." }, { valid: true, src: "$$\\sum\_{i=1}^n$$", comment: "Sum without equation number." }, { valid: true, src: "$$\\sum\_{i=1}\^n$$ \(2\)", comment: "Sum with equation number." }, { valid: true, src: "$${\\bold e}(\\varphi) = \\begin{pmatrix}\n\\cos\\varphi\\\\\\sin\\varphi\n\\end{pmatrix}$$ (3)", comment: "equation number always vertically aligned." }, { valid: true, src: "> see $a = b + c$ \n> $c^2=a^2+b^2$ (2) \n> $c^2=a^2+b^2$ ", comment: "inline equations in blockquote." }, { valid: true, src: "> formula\n>\n> $$ a+b=c$$ (2)\n>\n> in blockquote. ", comment: "display equation in blockquote." }, { valid: true, src: "\\$1+1=2$\n$1+1=2\\$", comment: "escaped dollars '\\$' are interpreted as\ndollar '$' characters." }, { valid: false, src: "> \$\$ a+b\n=c\$\$", comment: "new line in blockquote block." }, { valid: false, src: "some text\n \$\\$a+b=c\$\$", comment: "empty line between text and display formula is required." }, { valid: false, src: "$ $\n$ x$\n$x $", comment: "whitespace character after opening $\nor before closing $ is not allowed." }, { valid: false, src: "$1+1=\n2$", comment: "line break in inline equation is not allowed." } ] if (typeof module === "object" && module.exports) module.exports = tests; goessner-markdown-it-texmath-1513a20/test/brackets.md0000644000175000017500000000015114244343111023726 0ustar supermariosupermario# Scratchpad \$ $ \char"0024 \[\frac{1}{1}\] $$\frac{1}{1}$$ inline $a = \$ \char"0024 \§ = b$goessner-markdown-it-texmath-1513a20/test/bug-dollars.html0000644000175000017500000000172014244343111024712 0ustar supermariosupermario
goessner-markdown-it-texmath-1513a20/test/blockquote-bug.html0000644000175000017500000000253014244343111025422 0ustar supermariosupermario
goessner-markdown-it-texmath-1513a20/test/index.js0000644000175000017500000000443314244343111023262 0ustar supermariosupermario'use strict'; const fs = require('fs'); const kt = require('katex'); const tm = require('../texmath.js').use(kt); const md = require('markdown-it')({html:true}).use(tm); const tests = require('./tests.js'); const html = (content) => ` markdown-it-texmath Tests

markdown-it-texmath Tests

${content}
i valid src render comment
`; const row = (i, valid, src, comment) => ` ${i} ${valid ? "😊" : "😡"} ${src.replace("<","<")} ${md.render(src)} ${comment} `; let content = ""; //tm.render = function(tex,isblock) { return tex; } for (let i=0, n=tests.length; i markdown-it-texmath Tests

markdown-it-texmath Tests

i valid src render comment
0 😊 $a$

aa

single character inline equation.
1 😊 $\varphi$

φ\varphi

inline equation with single greek character
2 😊 $1+1=2$

1+1=21+1=2

simple equation starting and ending with numbers.
3 😊 $1+1<3$

1+1<31+1<3

simple equation including special html character.
4 😊 $a \backslash$

a\a \backslash

equation including backslashes.
5 😊 You get 3$ if you solve $1+2$

You get 3$ if you solve 1+21+2

use of currency symbol
6 😊 If you solve $1+2$ you get $3

If you solve 1+21+2 you get $3

use of currency symbol
7 😊 $\frac{1}{2}$

12\frac{1}{2}

inline fraction
8 😊 $\begin{pmatrix}x\\y\end{pmatrix}$

(xy)\begin{pmatrix}x\\y\end{pmatrix}

inline column vector
9 😊 ${\tilde\bold e}_\alpha$

e~α{\tilde\bold e}_\alpha

inline bold vector notation
10 😊 $a^{b}$

aba^{b}

exponentiation
11 😊 $a^*b$ with $a^*$

aba^*b with aa^*

conjugate complex
12 😊 $$e_\alpha$$
eαe_\alpha
single block equation, greek index
13 😊 $$1+1=2$$
1+1=21+1=2
display equation on its own single line.
14 😊 ${e}_x$ $$e_\alpha$$

ex{e}_x

eαe_\alpha
inline equation followed by block equation.
15 😊 $$c{\bold e}_x = a{\bold e}_\alpha - b\tilde{\bold e}_\alpha$$
cex=aeαbe~αc{\bold e}_x = a{\bold e}_\alpha - b\tilde{\bold e}_\alpha
underline tests
16 😊 a$1+1=2$ $1+1=2$b c$x$d

a1+1=21+1=2 1+1=21+1=2b cxxd

non-numeric character before opening $ or after closing $ or both is allowed.
17 😊 $x$ $

xx $

following dollar character '$' is allowed.
18 😊 $x$ $y$

xx yy

consecutive inline equations.
19 😊 so-what is $x$

so-what is xx

inline equation after '-' sign in text.
20 😊 $$ 1+1=2 $$
1+1=2 1+1=2
display equation with line breaks.
21 😊 $$\begin{matrix} f & = & 2 + x + 3 \\ & = & 5 + x \end{matrix}$$
f=2+x+3=5+x\begin{matrix} f & = & 2 + x + 3 \\ & = & 5 + x \end{matrix}
multiline equation.
22 😊 $$\begin{pmatrix}x_2 \\ y_2 \end{pmatrix} = \begin{pmatrix} A & B \\ C & D \end{pmatrix}\cdot \begin{pmatrix} x_1 \\ y_1 \end{pmatrix}$$
(x2y2)=(ABCD)(x1y1)\begin{pmatrix}x_2 \\ y_2 \end{pmatrix} = \begin{pmatrix} A & B \\ C & D \end{pmatrix}\cdot \begin{pmatrix} x_1 \\ y_1 \end{pmatrix}
vector equation.
23 😊 $$f(x) = x^2 - 1$$ (1)
f(x)=x21f(x) = x^2 - 1(1)
display equation with equation number.
24 😊 `code`$a-b$

codeaba-b

inline equation following code section.
25 😊 ``` code ``` $$a+b$$
code
a+ba+b
equation following code block.
26 😊 ``` code ``` $$a+b$$(1)
code
a+ba+b(1)
numbered equation following code block.
27 😊 1. $1+2$ 2. $2+3$ 1. $3+4$
  1. 1+21+2
  2. 2+32+3
    1. 3+43+4
Equations in list.
28 😊 $\sum_{i=1}^n$

i=1n\sum_{i=1}^n

Inline sum.
29 😊 $$\sum_{i=1}^n$$
i=1n\sum_{i=1}^n
Sum without equation number.
30 😊 $$\sum_{i=1}^n$$ (2)
i=1n\sum_{i=1}^n(2)
Sum with equation number.
31 😊 $${\bold e}(\varphi) = \begin{pmatrix} \cos\varphi\\\sin\varphi \end{pmatrix}$$ (3)
e(φ)=(cosφsinφ){\bold e}(\varphi) = \begin{pmatrix} \cos\varphi\\\sin\varphi \end{pmatrix}(3)
equation number always vertically aligned.
32 😊 > see $a = b + c$ > $c^2=a^2+b^2$ (2) > $c^2=a^2+b^2$

see a=b+ca = b + c c2=a2+b2c^2=a^2+b^2 (2) c2=a2+b2c^2=a^2+b^2

inline equations in blockquote.
33 😊 > formula > > $$ a+b=c$$ (2) > > in blockquote.

formula

a+b=c a+b=c(2)

in blockquote.

display equation in blockquote.
34 😊 \$1+1=2$ $1+1=2\$

$1+1=2$ $1+1=2$

escaped dollars '\$' are interpreted as dollar '$' characters.
35 😡 > $$ a+b =c$$

$$ a+b =c$$

new line in blockquote block.
36 😡 some text $\$a+b=c$$

some text $$a+b=c$$

empty line between text and display formula is required.
37 😡 $ $ $ x$ $x $

$ $ $ x$ $x $

whitespace character after opening $ or before closing $ is not allowed.
38 😡 $1+1= 2$

$1+1= 2$

line break in inline equation is not allowed.
goessner-markdown-it-texmath-1513a20/test/test.js0000644000175000017500000000116414244343111023130 0ustar supermariosupermario'use strict'; const tm = require('../texmath.js'); const md = require('markdown-it')({html:true}).use(tm, { engine: require('katex'), delimiters: 'dollars', outerSpace: true, katexOptions: { macros: {"\\RR": "\\mathbb{R}"} } }); const str = "Euler\'s identity $e^{i\\pi}+1=0$ is a beautiful formula in $\\RR^2$."; // overwrite texmath render function (suppressing katex) // tm.render = function(tex, isblock) { return tex; } console.log(md.render(str)); goessner-markdown-it-texmath-1513a20/test/test.html0000644000175000017500000000170414244343111023460 0ustar supermariosupermario test
goessner-markdown-it-texmath-1513a20/test/mdmath-endtag-bug.md0000644000175000017500000000006414244343111025420 0ustar supermariosupermarionotation as column vectors. $$1$$ \pi $$3$$ (2) goessner-markdown-it-texmath-1513a20/test/beg_end-test.html0000644000175000017500000000213514244343111025040 0ustar supermariosupermario beg_end test
goessner-markdown-it-texmath-1513a20/test/blockquote-bug.js0000644000175000017500000000052214244343111025071 0ustar supermariosupermario'use strict'; const tm = require('../texmath.js'); const md = require('markdown-it')({html:true}).use(tm,{engine:require('katex'), delimiters:'dollars'}); const str = ` With > $$ > x > $$ `; // overwrite texmath render function (suppressing katex) // tm.render = function(tex, isblock) { return tex; } console.log(md.render(str)); goessner-markdown-it-texmath-1513a20/css/0000755000175000017500000000000014244343111021422 5ustar supermariosupermariogoessner-markdown-it-texmath-1513a20/css/texmath.css0000644000175000017500000000064614244343111023614 0ustar supermariosupermario/* style for html inside of browsers */ .katex { font-size: 1em !important; } /* align KaTeX font-size to surrounding text */ eq { display: inline-block; } eqn { display: block} section.eqno { display: flex; flex-direction: row; align-content: space-between; align-items: center; } section.eqno > eqn { width: 100%; margin-left: 3em; } section.eqno > span { width:3em; text-align:right; }