@pie-lib/math-rendering 2.5.14 → 2.5.15-next.138

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -8,7 +8,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
8
8
 
9
9
  ### Bug Fixes
10
10
 
11
- * **eslint:** fix eslint problems-AC-2732 ([9eb9976](https://github.com/pie-framework/pie-lib/commit/9eb9976749753da86e1057a07bd2cfc65bf64ae4))
11
+ * **eslint:** fix eslint problems-PD-2732 ([9eb9976](https://github.com/pie-framework/pie-lib/commit/9eb9976749753da86e1057a07bd2cfc65bf64ae4))
12
12
 
13
13
 
14
14
 
package/lib/index.js CHANGED
@@ -5,6 +5,12 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
+ Object.defineProperty(exports, "mmlToLatex", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _mmlToLatex["default"];
12
+ }
13
+ });
8
14
  Object.defineProperty(exports, "renderMath", {
9
15
  enumerable: true,
10
16
  get: function get() {
@@ -26,5 +32,7 @@ Object.defineProperty(exports, "wrapMath", {
26
32
 
27
33
  var _renderMath = _interopRequireDefault(require("./render-math"));
28
34
 
35
+ var _mmlToLatex = _interopRequireDefault(require("./mml-to-latex"));
36
+
29
37
  var _normalization = require("./normalization");
30
38
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA","sourcesContent":["import renderMath from './render-math';\nimport { wrapMath, unWrapMath } from './normalization';\n\nexport { renderMath, wrapMath, unWrapMath };\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA","sourcesContent":["import renderMath from './render-math';\nimport mmlToLatex from './mml-to-latex';\nimport { wrapMath, unWrapMath } from './normalization';\n\nexport { renderMath, wrapMath, unWrapMath, mmlToLatex };\n"],"file":"index.js"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _mathmlToLatex = _interopRequireDefault(require("mathml-to-latex"));
11
+
12
+ var _default = function _default(mathml) {
13
+ return _mathmlToLatex["default"].convert(mathml);
14
+ };
15
+
16
+ exports["default"] = _default;
17
+ //# sourceMappingURL=mml-to-latex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mml-to-latex.js"],"names":["mathml","Mathml2latex","convert"],"mappings":";;;;;;;;;AAAA;;eACe,kBAAAA,MAAM;AAAA,SAAIC,0BAAaC,OAAb,CAAqBF,MAArB,CAAJ;AAAA,C","sourcesContent":["import Mathml2latex from 'mathml-to-latex';\nexport default mathml => Mathml2latex.convert(mathml);\n"],"file":"mml-to-latex.js"}
@@ -110,7 +110,8 @@ var fixMathElement = function fixMathElement(element) {
110
110
  exports.fixMathElement = fixMathElement;
111
111
 
112
112
  var fixMathElements = function fixMathElements() {
113
- var mathElements = document.querySelectorAll('[data-latex]');
113
+ var el = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
114
+ var mathElements = el.querySelectorAll('[data-latex]');
114
115
  mathElements.forEach(function (item) {
115
116
  return fixMathElement(item);
116
117
  });
@@ -119,21 +120,15 @@ var fixMathElements = function fixMathElements() {
119
120
  exports.fixMathElements = fixMathElements;
120
121
 
121
122
  var adjustMathMLStyle = function adjustMathMLStyle() {
122
- var nodes = document.querySelectorAll('math');
123
+ var el = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
124
+ var nodes = el.querySelectorAll('math');
123
125
  nodes.forEach(function (node) {
124
126
  return node.setAttribute('displaystyle', 'true');
125
127
  });
126
128
  };
127
129
 
128
- var bootstrap = function bootstrap(opts) {
129
- if (typeof window === 'undefined') {
130
- return {
131
- Typeset: function Typeset() {
132
- return {};
133
- }
134
- };
135
- }
136
-
130
+ var createMathMLInstance = function createMathMLInstance(opts) {
131
+ var docProvided = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
137
132
  opts = opts || defaultOpts();
138
133
 
139
134
  if (opts.useSingleDollar) {
@@ -220,7 +215,7 @@ var bootstrap = function bootstrap(opts) {
220
215
  var mml = new _mathml.MathML(mmlConfig);
221
216
  var customMmlFactory = new _MmlFactory.MmlFactory(_objectSpread(_objectSpread({}, _MmlFactory.MmlFactory.defaultNodes), _mstack.mmlNodes));
222
217
 
223
- var html = _mathjax.mathjax.document(document, {
218
+ var html = _mathjax.mathjax.document(docProvided, {
224
219
  compileError: function compileError(mj, math, err) {
225
220
  // eslint-disable-next-line no-console
226
221
  console.log('bad math?:', math); // eslint-disable-next-line no-console
@@ -250,6 +245,19 @@ var bootstrap = function bootstrap(opts) {
250
245
 
251
246
 
252
247
  mml.setMmlFactory(customMmlFactory);
248
+ return html;
249
+ };
250
+
251
+ var bootstrap = function bootstrap(opts) {
252
+ if (typeof window === 'undefined') {
253
+ return {
254
+ Typeset: function Typeset() {
255
+ return {};
256
+ }
257
+ };
258
+ }
259
+
260
+ var html = createMathMLInstance(opts);
253
261
  return {
254
262
  version: _mathjax.mathjax.version,
255
263
  html: html,
@@ -281,9 +289,32 @@ var bootstrap = function bootstrap(opts) {
281
289
  };
282
290
 
283
291
  var renderMath = function renderMath(el, renderOpts) {
284
- //TODO: remove this - has nothing to do with math-rendering (it's from editable-html)
285
- fixMathElements();
286
- adjustMathMLStyle();
292
+ var isString = typeof el === 'string';
293
+ var executeOn = document.body;
294
+
295
+ if (isString) {
296
+ var div = document.createElement('div');
297
+ div.innerHTML = el;
298
+ executeOn = div;
299
+ }
300
+
301
+ fixMathElements(executeOn);
302
+ adjustMathMLStyle(executeOn);
303
+
304
+ if (isString) {
305
+ var html = createMathMLInstance(undefined, executeOn);
306
+ var updatedDocument = html.findMath().compile().getMetrics().typeset().updateDocument();
307
+ var list = updatedDocument.math.list;
308
+ var item = list.next;
309
+
310
+ if (!item) {
311
+ return '';
312
+ }
313
+
314
+ var mathMl = toMMl(item.data.root);
315
+ var parsedMathMl = mathMl.replaceAll('\n', '');
316
+ return parsedMathMl;
317
+ }
287
318
 
288
319
  if (!getGlobal().instance) {
289
320
  getGlobal().instance = bootstrap(renderOpts);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/render-math.js"],"names":["window","visitor","SerializedMmlVisitor","toMMl","node","visitTree","log","NEWLINE_BLOCK_REGEX","NEWLINE_LATEX","getGlobal","key","pkg","name","version","split","defaultOpts","opts","fixMathElement","element","dataset","mathHandled","property","textContent","unwrapped","replace","fixMathElements","mathElements","document","querySelectorAll","forEach","item","adjustMathMLStyle","nodes","setAttribute","bootstrap","Typeset","useSingleDollar","console","warn","packages","AllPackages","filter","push","macros","parallelogram","overarc","napprox","longdiv","texConfig","inlineMath","processEscapes","options","enableExplorer","enableAssistiveMml","a11y","speech","braille","subtitles","sre","domain","style","locale","mmlConfig","parseError","error","adaptor","fontURL","mathjax","htmlConfig","wrapperFactory","CHTMLWrapperFactory","defaultNodes","chtmlNodes","renderActions","assistiveMml","mml","MathML","customMmlFactory","MmlFactory","mmlNodes","html","compileError","mj","math","err","typesetError","doc","menuOptions","settings","collapsible","explorer","InputJax","TeX","OutputJax","CHTML","setMmlFactory","elements","updatedDocument","findMath","length","compile","getMetrics","typeset","updateDocument","list","next","data","mathMl","root","parsedMathMl","replaceAll","typesetRoot","e","toString","clear","renderMath","el","renderOpts","instance","Element","arr","Array","from","CHTMLmspace","styles","display","height"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA;;AACA;;AACA;;AACA;;AAMA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;A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hC;AACD;AACF,CApBD;AAsBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACAG,oBAAYC,MAAZ,GAAqB;AACnB,gBAAc;AACZC,IAAAA,OAAO,EAAE,OADG;AAEZ,kBAAc,QAFF;AAGZC,IAAAA,MAAM,EAAE;AAHI;AADK,CAArB;eAQeX,U","sourcesContent":["import { mathjax } from 'mathjax-full/js/mathjax';\nimport { MathML } from 'mathjax-full/js/input/mathml';\nimport { TeX } from 'mathjax-full/js/input/tex';\n\nimport { CHTML } from 'mathjax-full/js/output/chtml';\nimport { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html';\nimport { browserAdaptor } from 'mathjax-full/js/adaptors/browserAdaptor';\nimport { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';\n\nif (typeof window !== 'undefined') {\n RegisterHTMLHandler(browserAdaptor());\n}\n\nimport pkg from '../package.json';\nimport { mmlNodes, chtmlNodes } from './mstack';\nimport debug from 'debug';\nimport { wrapMath, unWrapMath } from './normalization';\nimport { MmlFactory } from 'mathjax-full/js/core/MmlTree/MmlFactory';\nimport { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';\nimport { CHTMLWrapperFactory } from 'mathjax-full/js/output/chtml/WrapperFactory';\nimport { CHTMLmspace } from 'mathjax-full/js/output/chtml/Wrappers/mspace';\n\nconst visitor = new SerializedMmlVisitor();\nconst toMMl = (node) => visitor.visitTree(node);\n\nconst log = debug('pie-lib:math-rendering');\n\nconst NEWLINE_BLOCK_REGEX = /\\\\embed\\{newLine\\}\\[\\]/g;\nconst NEWLINE_LATEX = '\\\\newline ';\n\nconst getGlobal = () => {\n const key = `${pkg.name}@${pkg.version.split('.')[0]}`;\n\n if (typeof window !== 'undefined') {\n if (!window[key]) {\n window[key] = {};\n }\n return window[key];\n } else {\n return {};\n }\n};\n\n/** Add temporary support for a global singleDollar override\n * <code>\n * // This will enable single dollar rendering\n * window.pie = window.pie || {};\n * window.pie.mathRendering = {useSingleDollar: true };\n * </code>\n */\nconst defaultOpts = () => getGlobal().opts || {};\n\nexport const fixMathElement = (element) => {\n if (element.dataset.mathHandled) {\n return;\n }\n\n let property = 'innerText';\n\n if (element.textContent) {\n property = 'textContent';\n }\n\n if (element[property]) {\n element[property] = wrapMath(unWrapMath(element[property]).unwrapped);\n // because mathquill doesn't understand line breaks, sometimes we end up with custom elements on prompts/rationale/etc.\n // we need to replace the custom embedded elements with valid latex that Mathjax can understand\n element[property] = element[property].replace(NEWLINE_BLOCK_REGEX, NEWLINE_LATEX);\n element.dataset.mathHandled = true;\n }\n};\n\nexport const fixMathElements = () => {\n const mathElements = document.querySelectorAll('[data-latex]');\n\n mathElements.forEach((item) => fixMathElement(item));\n};\n\nconst adjustMathMLStyle = () => {\n const nodes = document.querySelectorAll('math');\n nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));\n};\n\nconst bootstrap = (opts) => {\n if (typeof window === 'undefined') {\n return { Typeset: () => ({}) };\n }\n\n opts = opts || defaultOpts();\n\n if (opts.useSingleDollar) {\n // eslint-disable-next-line\n console.warn('[math-rendering] using $ is not advisable, please use $$..$$ or \\\\(...\\\\)');\n }\n\n const packages = AllPackages.filter((name) => name !== 'bussproofs'); // Bussproofs needs an output jax\n\n // The autoload extension predefines all the macros from the extensions that haven't been loaded already\n // so that they automatically load the needed extension when they are first used\n packages.push('autoload');\n\n const macros = {\n parallelogram: '\\\\lower.2em{\\\\Huge\\\\unicode{x25B1}}',\n overarc: '\\\\overparen',\n napprox: '\\\\not\\\\approx',\n longdiv: '\\\\enclose{longdiv}',\n };\n\n const texConfig = opts.useSingleDollar\n ? {\n packages,\n macros,\n inlineMath: [\n ['$', '$'],\n ['\\\\(', '\\\\)'],\n ],\n processEscapes: true,\n options: {\n enableExplorer: true,\n enableAssistiveMml: true,\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n sre: {\n domain: 'default',\n style: 'default',\n locale: 'en',\n },\n },\n }\n : {\n packages,\n macros,\n options: {\n enableExplorer: true,\n enableAssistiveMml: true,\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n sre: {\n domain: 'default',\n style: 'default',\n locale: 'en',\n },\n },\n };\n\n const mmlConfig = {\n options: {\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n },\n parseError: function(node) {\n // function to process parsing errors\n // eslint-disable-next-line no-console\n console.log('error:', node);\n this.error(this.adaptor.textContent(node).replace(/\\n.*/g, ''));\n },\n };\n\n const fontURL = `https://unpkg.com/mathjax-full@${mathjax.version}/ts/output/chtml/fonts/tex-woff-v2`;\n const htmlConfig = {\n fontURL,\n\n wrapperFactory: new CHTMLWrapperFactory({\n ...CHTMLWrapperFactory.defaultNodes,\n ...chtmlNodes,\n }),\n\n options: {\n renderActions: {\n assistiveMml: [['AssistiveMmlHandler']],\n },\n },\n };\n\n const mml = new MathML(mmlConfig);\n\n const customMmlFactory = new MmlFactory({\n ...MmlFactory.defaultNodes,\n ...mmlNodes,\n });\n\n const html = mathjax.document(document, {\n compileError: (mj, math, err) => {\n // eslint-disable-next-line no-console\n console.log('bad math?:', math);\n // eslint-disable-next-line no-console\n console.error(err);\n },\n typesetError: function(doc, math, err) {\n // eslint-disable-next-line no-console\n console.log('typeset error');\n // eslint-disable-next-line no-console\n console.error(err);\n doc.typesetError(math, err);\n },\n\n options: {\n enableAssistiveMml: true,\n menuOptions: {\n settings: {\n assistiveMml: true,\n collapsible: true,\n explorer: true,\n },\n },\n },\n\n InputJax: [new TeX(texConfig), mml],\n OutputJax: new CHTML(htmlConfig),\n });\n\n // Note: we must set this *after* mathjax.document (no idea why)\n mml.setMmlFactory(customMmlFactory);\n\n return {\n version: mathjax.version,\n html: html,\n Typeset: function(...elements) {\n const updatedDocument = this.html\n .findMath(elements.length ? { elements } : {})\n .compile()\n .getMetrics()\n .typeset()\n .updateDocument();\n\n try {\n const list = updatedDocument.math.list;\n\n for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {\n const mathMl = toMMl(item.data.root);\n const parsedMathMl = mathMl.replaceAll('\\n', '');\n\n item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e.toString());\n }\n\n updatedDocument.clear();\n },\n };\n};\n\nconst renderMath = (el, renderOpts) => {\n //TODO: remove this - has nothing to do with math-rendering (it's from editable-html)\n fixMathElements();\n adjustMathMLStyle();\n\n if (!getGlobal().instance) {\n getGlobal().instance = bootstrap(renderOpts);\n }\n\n if (!el) {\n log('el is undefined');\n return;\n }\n\n if (el instanceof Element) {\n getGlobal().instance.Typeset(el);\n } else if (el.length) {\n const arr = Array.from(el);\n getGlobal().instance.Typeset(...arr);\n }\n};\n\n/**\n * This style is added to overried default styling of mjx-mspace Mathjax tag\n * In mathjax src code \\newline latex gets parsed to <mjx-mspace></mjx-mspace>,\n * but has the default style\n * 'mjx-mspace': {\n \"display\": 'in-line',\n \"text-align\": 'left'\n} which prevents it from showing as a newline value\n */\nCHTMLmspace.styles = {\n 'mjx-mspace': {\n display: 'block',\n 'text-align': 'center',\n height: '5px',\n },\n};\n\nexport default renderMath;\n"],"file":"render-math.js"}
1
+ {"version":3,"sources":["../src/render-math.js"],"names":["window","visitor","SerializedMmlVisitor","toMMl","node","visitTree","log","NEWLINE_BLOCK_REGEX","NEWLINE_LATEX","getGlobal","key","pkg","name","version","split","defaultOpts","opts","fixMathElement","element","dataset","mathHandled","property","textContent","unwrapped","replace","fixMathElements","el","document","mathElements","querySelectorAll","forEach","item","adjustMathMLStyle","nodes","setAttribute","createMathMLInstance","docProvided","useSingleDollar","console","warn","packages","AllPackages","filter","push","macros","parallelogram","overarc","napprox","longdiv","texConfig","inlineMath","processEscapes","options","enableExplorer","enableAssistiveMml","a11y","speech","braille","subtitles","sre","domain","style","locale","mmlConfig","parseError","error","adaptor","fontURL","mathjax","htmlConfig","wrapperFactory","CHTMLWrapperFactory","defaultNodes","chtmlNodes","renderActions","assistiveMml","mml","MathML","customMmlFactory","MmlFactory","mmlNodes","html","compileError","mj","math","err","typesetError","doc","menuOptions","settings","collapsible","explorer","InputJax","TeX","OutputJax","CHTML","setMmlFactory","bootstrap","Typeset","elements","updatedDocument","findMath","length","compile","getMetrics","typeset","updateDocument","list","next","data","mathMl","root","parsedMathMl","replaceAll","typesetRoot","e","toString","clear","renderMath","renderOpts","isString","executeOn","body","div","createElement","innerHTML","undefined","instance","Element","arr","Array","from","CHTMLmspace","styles","display","height"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA;;AACA;;AACA;;AACA;;AAMA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;AAXA,IAAI,OAAOA,MAAP,KAAkB,WAAtB,EAAmC;AACjC,iCAAoB,qCAApB;AACD;;AAWD,IAAMC,OAAO,GAAG,IAAIC,0CAAJ,EAAhB;;AACA,IAAMC,KAAK,GAAG,SAARA,KAAQ,CAACC,IAAD;AAAA,SAAUH,OAAO,CAACI,SAAR,CAAkBD,IAAlB,CAAV;AAAA,CAAd;;AAEA,IAAME,GAAG,GAAG,uBAAM,wBAAN,CAAZ;AAEA,IAAMC,mBAAmB,GAAG,yBAA5B;AACA,IAAMC,aAAa,GAAG,YAAtB;;AAEA,IAAMC,SAAS,GAAG,SAAZA,SAAY,GAAM;AACtB,MAAMC,GAAG,aAAMC,oBAAIC,IAAV,cAAkBD,oBAAIE,OAAJ,CAAYC,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAlB,CAAT;;AAEA,MAAI,OAAOd,MAAP,KAAkB,WAAtB,EAAmC;AACjC,QAAI,CAACA,MAAM,CAACU,GAAD,CAAX,EAAkB;AAChBV,MAAAA,MAAM,CAACU,GAAD,CAAN,GAAc,EAAd;AACD;;AACD,WAAOV,MAAM,CAACU,GAAD,CAAb;AACD,GALD,MAKO;AACL,WAAO,EAAP;AACD;AACF,CAXD;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,IAAMK,WAAW,GAAG,SAAdA,WAAc;A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BAAD,CAAH;AACA;AACD;;AAED,MAAIoB,EAAE,YAAYsG,OAAlB,EAA2B;AACzBvH,IAAAA,SAAS,GAAGsH,QAAZ,CAAqB7B,OAArB,CAA6BxE,EAA7B;AACD,GAFD,MAEO,IAAIA,EAAE,CAAC4E,MAAP,EAAe;AAAA;;AACpB,QAAM2B,GAAG,GAAGC,KAAK,CAACC,IAAN,CAAWzG,EAAX,CAAZ;;AACA,2BAAAjB,SAAS,GAAGsH,QAAZ,EAAqB7B,OAArB,gEAAgC+B,GAAhC;AACD;AACF,CApDD;AAsDA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACAG,oBAAYC,MAAZ,GAAqB;AACnB,gBAAc;AACZC,IAAAA,OAAO,EAAE,OADG;AAEZ,kBAAc,QAFF;AAGZC,IAAAA,MAAM,EAAE;AAHI;AADK,CAArB;eAQejB,U","sourcesContent":["import { mathjax } from 'mathjax-full/js/mathjax';\nimport { MathML } from 'mathjax-full/js/input/mathml';\nimport { TeX } from 'mathjax-full/js/input/tex';\n\nimport { CHTML } from 'mathjax-full/js/output/chtml';\nimport { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html';\nimport { browserAdaptor } from 'mathjax-full/js/adaptors/browserAdaptor';\nimport { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';\n\nif (typeof window !== 'undefined') {\n RegisterHTMLHandler(browserAdaptor());\n}\n\nimport pkg from '../package.json';\nimport { mmlNodes, chtmlNodes } from './mstack';\nimport debug from 'debug';\nimport { wrapMath, unWrapMath } from './normalization';\nimport { MmlFactory } from 'mathjax-full/js/core/MmlTree/MmlFactory';\nimport { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';\nimport { CHTMLWrapperFactory } from 'mathjax-full/js/output/chtml/WrapperFactory';\nimport { CHTMLmspace } from 'mathjax-full/js/output/chtml/Wrappers/mspace';\n\nconst visitor = new SerializedMmlVisitor();\nconst toMMl = (node) => visitor.visitTree(node);\n\nconst log = debug('pie-lib:math-rendering');\n\nconst NEWLINE_BLOCK_REGEX = /\\\\embed\\{newLine\\}\\[\\]/g;\nconst NEWLINE_LATEX = '\\\\newline ';\n\nconst getGlobal = () => {\n const key = `${pkg.name}@${pkg.version.split('.')[0]}`;\n\n if (typeof window !== 'undefined') {\n if (!window[key]) {\n window[key] = {};\n }\n return window[key];\n } else {\n return {};\n }\n};\n\n/** Add temporary support for a global singleDollar override\n * <code>\n * // This will enable single dollar rendering\n * window.pie = window.pie || {};\n * window.pie.mathRendering = {useSingleDollar: true };\n * </code>\n */\nconst defaultOpts = () => getGlobal().opts || {};\n\nexport const fixMathElement = (element) => {\n if (element.dataset.mathHandled) {\n return;\n }\n\n let property = 'innerText';\n\n if (element.textContent) {\n property = 'textContent';\n }\n\n if (element[property]) {\n element[property] = wrapMath(unWrapMath(element[property]).unwrapped);\n // because mathquill doesn't understand line breaks, sometimes we end up with custom elements on prompts/rationale/etc.\n // we need to replace the custom embedded elements with valid latex that Mathjax can understand\n element[property] = element[property].replace(NEWLINE_BLOCK_REGEX, NEWLINE_LATEX);\n element.dataset.mathHandled = true;\n }\n};\n\nexport const fixMathElements = (el = document) => {\n const mathElements = el.querySelectorAll('[data-latex]');\n\n mathElements.forEach((item) => fixMathElement(item));\n};\n\nconst adjustMathMLStyle = (el = document) => {\n const nodes = el.querySelectorAll('math');\n nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));\n};\n\nconst createMathMLInstance = (opts, docProvided = document) => {\n opts = opts || defaultOpts();\n\n if (opts.useSingleDollar) {\n // eslint-disable-next-line\n console.warn('[math-rendering] using $ is not advisable, please use $$..$$ or \\\\(...\\\\)');\n }\n\n const packages = AllPackages.filter((name) => name !== 'bussproofs'); // Bussproofs needs an output jax\n\n // The autoload extension predefines all the macros from the extensions that haven't been loaded already\n // so that they automatically load the needed extension when they are first used\n packages.push('autoload');\n\n const macros = {\n parallelogram: '\\\\lower.2em{\\\\Huge\\\\unicode{x25B1}}',\n overarc: '\\\\overparen',\n napprox: '\\\\not\\\\approx',\n longdiv: '\\\\enclose{longdiv}',\n };\n\n const texConfig = opts.useSingleDollar\n ? {\n packages,\n macros,\n inlineMath: [\n ['$', '$'],\n ['\\\\(', '\\\\)'],\n ],\n processEscapes: true,\n options: {\n enableExplorer: true,\n enableAssistiveMml: true,\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n sre: {\n domain: 'default',\n style: 'default',\n locale: 'en',\n },\n },\n }\n : {\n packages,\n macros,\n options: {\n enableExplorer: true,\n enableAssistiveMml: true,\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n sre: {\n domain: 'default',\n style: 'default',\n locale: 'en',\n },\n },\n };\n\n const mmlConfig = {\n options: {\n a11y: {\n speech: true,\n braille: true,\n subtitles: true,\n },\n },\n parseError: function(node) {\n // function to process parsing errors\n // eslint-disable-next-line no-console\n console.log('error:', node);\n this.error(this.adaptor.textContent(node).replace(/\\n.*/g, ''));\n },\n };\n\n const fontURL = `https://unpkg.com/mathjax-full@${mathjax.version}/ts/output/chtml/fonts/tex-woff-v2`;\n const htmlConfig = {\n fontURL,\n\n wrapperFactory: new CHTMLWrapperFactory({\n ...CHTMLWrapperFactory.defaultNodes,\n ...chtmlNodes,\n }),\n\n options: {\n renderActions: {\n assistiveMml: [['AssistiveMmlHandler']],\n },\n },\n };\n\n const mml = new MathML(mmlConfig);\n\n const customMmlFactory = new MmlFactory({\n ...MmlFactory.defaultNodes,\n ...mmlNodes,\n });\n\n const html = mathjax.document(docProvided, {\n compileError: (mj, math, err) => {\n // eslint-disable-next-line no-console\n console.log('bad math?:', math);\n // eslint-disable-next-line no-console\n console.error(err);\n },\n typesetError: function(doc, math, err) {\n // eslint-disable-next-line no-console\n console.log('typeset error');\n // eslint-disable-next-line no-console\n console.error(err);\n doc.typesetError(math, err);\n },\n\n options: {\n enableAssistiveMml: true,\n menuOptions: {\n settings: {\n assistiveMml: true,\n collapsible: true,\n explorer: true,\n },\n },\n },\n\n InputJax: [new TeX(texConfig), mml],\n OutputJax: new CHTML(htmlConfig),\n });\n\n // Note: we must set this *after* mathjax.document (no idea why)\n mml.setMmlFactory(customMmlFactory);\n\n return html;\n};\n\nconst bootstrap = (opts) => {\n if (typeof window === 'undefined') {\n return { Typeset: () => ({}) };\n }\n\n const html = createMathMLInstance(opts);\n\n return {\n version: mathjax.version,\n html: html,\n Typeset: function(...elements) {\n const updatedDocument = this.html\n .findMath(elements.length ? { elements } : {})\n .compile()\n .getMetrics()\n .typeset()\n .updateDocument();\n\n try {\n const list = updatedDocument.math.list;\n\n for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {\n const mathMl = toMMl(item.data.root);\n const parsedMathMl = mathMl.replaceAll('\\n', '');\n\n item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e.toString());\n }\n\n updatedDocument.clear();\n },\n };\n};\n\nconst renderMath = (el, renderOpts) => {\n const isString = typeof el === 'string';\n let executeOn = document.body;\n\n if (isString) {\n const div = document.createElement('div');\n\n div.innerHTML = el;\n executeOn = div;\n }\n\n fixMathElements(executeOn);\n adjustMathMLStyle(executeOn);\n\n if (isString) {\n const html = createMathMLInstance(undefined, executeOn);\n\n const updatedDocument = html\n .findMath()\n .compile()\n .getMetrics()\n .typeset()\n .updateDocument();\n\n const list = updatedDocument.math.list;\n const item = list.next;\n\n if (!item) {\n return '';\n }\n\n const mathMl = toMMl(item.data.root);\n const parsedMathMl = mathMl.replaceAll('\\n', '');\n\n return parsedMathMl;\n }\n\n if (!getGlobal().instance) {\n getGlobal().instance = bootstrap(renderOpts);\n }\n\n if (!el) {\n log('el is undefined');\n return;\n }\n\n if (el instanceof Element) {\n getGlobal().instance.Typeset(el);\n } else if (el.length) {\n const arr = Array.from(el);\n getGlobal().instance.Typeset(...arr);\n }\n};\n\n/**\n * This style is added to overried default styling of mjx-mspace Mathjax tag\n * In mathjax src code \\newline latex gets parsed to <mjx-mspace></mjx-mspace>,\n * but has the default style\n * 'mjx-mspace': {\n \"display\": 'in-line',\n \"text-align\": 'left'\n} which prevents it from showing as a newline value\n */\nCHTMLmspace.styles = {\n 'mjx-mspace': {\n display: 'block',\n 'text-align': 'center',\n height: '5px',\n },\n};\n\nexport default renderMath;\n"],"file":"render-math.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/math-rendering",
3
- "version": "2.5.14",
3
+ "version": "2.5.15-next.138+1dd8f63f",
4
4
  "description": "math rendering utilities",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,8 +12,9 @@
12
12
  "dependencies": {
13
13
  "debug": "^4.1.1",
14
14
  "lodash": "^4.17.11",
15
- "mathjax-full": "3.2.2"
15
+ "mathjax-full": "3.2.2",
16
+ "mathml-to-latex": "^1.2.0"
16
17
  },
17
- "gitHead": "151723a15c9d7fc8d73a1bcb746e85628385bd6c",
18
+ "gitHead": "1dd8f63f22bc26363b4b47049a3d381a3df74605",
18
19
  "scripts": {}
19
20
  }
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import renderMath from './render-math';
2
+ import mmlToLatex from './mml-to-latex';
2
3
  import { wrapMath, unWrapMath } from './normalization';
3
4
 
4
- export { renderMath, wrapMath, unWrapMath };
5
+ export { renderMath, wrapMath, unWrapMath, mmlToLatex };
@@ -0,0 +1,2 @@
1
+ import Mathml2latex from 'mathml-to-latex';
2
+ export default mathml => Mathml2latex.convert(mathml);
@@ -70,22 +70,18 @@ export const fixMathElement = (element) => {
70
70
  }
71
71
  };
72
72
 
73
- export const fixMathElements = () => {
74
- const mathElements = document.querySelectorAll('[data-latex]');
73
+ export const fixMathElements = (el = document) => {
74
+ const mathElements = el.querySelectorAll('[data-latex]');
75
75
 
76
76
  mathElements.forEach((item) => fixMathElement(item));
77
77
  };
78
78
 
79
- const adjustMathMLStyle = () => {
80
- const nodes = document.querySelectorAll('math');
79
+ const adjustMathMLStyle = (el = document) => {
80
+ const nodes = el.querySelectorAll('math');
81
81
  nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));
82
82
  };
83
83
 
84
- const bootstrap = (opts) => {
85
- if (typeof window === 'undefined') {
86
- return { Typeset: () => ({}) };
87
- }
88
-
84
+ const createMathMLInstance = (opts, docProvided = document) => {
89
85
  opts = opts || defaultOpts();
90
86
 
91
87
  if (opts.useSingleDollar) {
@@ -188,7 +184,7 @@ const bootstrap = (opts) => {
188
184
  ...mmlNodes,
189
185
  });
190
186
 
191
- const html = mathjax.document(document, {
187
+ const html = mathjax.document(docProvided, {
192
188
  compileError: (mj, math, err) => {
193
189
  // eslint-disable-next-line no-console
194
190
  console.log('bad math?:', math);
@@ -221,6 +217,16 @@ const bootstrap = (opts) => {
221
217
  // Note: we must set this *after* mathjax.document (no idea why)
222
218
  mml.setMmlFactory(customMmlFactory);
223
219
 
220
+ return html;
221
+ };
222
+
223
+ const bootstrap = (opts) => {
224
+ if (typeof window === 'undefined') {
225
+ return { Typeset: () => ({}) };
226
+ }
227
+
228
+ const html = createMathMLInstance(opts);
229
+
224
230
  return {
225
231
  version: mathjax.version,
226
232
  html: html,
@@ -252,9 +258,41 @@ const bootstrap = (opts) => {
252
258
  };
253
259
 
254
260
  const renderMath = (el, renderOpts) => {
255
- //TODO: remove this - has nothing to do with math-rendering (it's from editable-html)
256
- fixMathElements();
257
- adjustMathMLStyle();
261
+ const isString = typeof el === 'string';
262
+ let executeOn = document.body;
263
+
264
+ if (isString) {
265
+ const div = document.createElement('div');
266
+
267
+ div.innerHTML = el;
268
+ executeOn = div;
269
+ }
270
+
271
+ fixMathElements(executeOn);
272
+ adjustMathMLStyle(executeOn);
273
+
274
+ if (isString) {
275
+ const html = createMathMLInstance(undefined, executeOn);
276
+
277
+ const updatedDocument = html
278
+ .findMath()
279
+ .compile()
280
+ .getMetrics()
281
+ .typeset()
282
+ .updateDocument();
283
+
284
+ const list = updatedDocument.math.list;
285
+ const item = list.next;
286
+
287
+ if (!item) {
288
+ return '';
289
+ }
290
+
291
+ const mathMl = toMMl(item.data.root);
292
+ const parsedMathMl = mathMl.replaceAll('\n', '');
293
+
294
+ return parsedMathMl;
295
+ }
258
296
 
259
297
  if (!getGlobal().instance) {
260
298
  getGlobal().instance = bootstrap(renderOpts);