temml 0.10.34 → 0.11.0
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/README.md +1 -1
- package/dist/temml.cjs +270 -4
- package/dist/temml.d.ts +3 -0
- package/dist/temml.js +270 -4
- package/dist/temml.min.js +1 -1
- package/dist/temml.mjs +270 -4
- package/dist/temmlPostProcess.js +1 -1
- package/package.json +2 -2
- package/src/auto-render.js +263 -0
- package/src/linebreaking.js +1 -1
- package/src/postProcess.js +1 -1
- package/temml.js +6 -0
- package/contrib/auto-render/README.md +0 -89
- package/contrib/auto-render/auto-render.js +0 -128
- package/contrib/auto-render/dist/auto-render.js +0 -214
- package/contrib/auto-render/dist/auto-render.min.js +0 -1
- package/contrib/auto-render/splitAtDelimiters.js +0 -84
- package/contrib/auto-render/test/auto-render-spec.js +0 -234
- package/contrib/auto-render/test/auto-render.js +0 -214
- package/contrib/auto-render/test/test_page.html +0 -59
package/README.md
CHANGED
package/dist/temml.cjs
CHANGED
@@ -2010,7 +2010,7 @@ function setLineBreaks(expression, wrapMode, isDisplayMode) {
|
|
2010
2010
|
}
|
2011
2011
|
block.push(node);
|
2012
2012
|
if (node.type && node.type === "mo" && node.children.length === 1 &&
|
2013
|
-
!Object.
|
2013
|
+
!Object.prototype.hasOwnProperty.call(node.attributes, "movablelimits")) {
|
2014
2014
|
const ch = node.children[0].text;
|
2015
2015
|
if (openDelims.indexOf(ch) > -1) {
|
2016
2016
|
level += 1;
|
@@ -13677,7 +13677,7 @@ class Style {
|
|
13677
13677
|
* https://mit-license.org/
|
13678
13678
|
*/
|
13679
13679
|
|
13680
|
-
const version = "0.
|
13680
|
+
const version = "0.11.00";
|
13681
13681
|
|
13682
13682
|
function postProcess(block) {
|
13683
13683
|
const labelMap = {};
|
@@ -13742,6 +13742,267 @@ function postProcess(block) {
|
|
13742
13742
|
});
|
13743
13743
|
}
|
13744
13744
|
|
13745
|
+
const findEndOfMath = function(delimiter, text, startIndex) {
|
13746
|
+
// Adapted from
|
13747
|
+
// https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
|
13748
|
+
let index = startIndex;
|
13749
|
+
let braceLevel = 0;
|
13750
|
+
|
13751
|
+
const delimLength = delimiter.length;
|
13752
|
+
|
13753
|
+
while (index < text.length) {
|
13754
|
+
const character = text[index];
|
13755
|
+
|
13756
|
+
if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
|
13757
|
+
return index;
|
13758
|
+
} else if (character === "\\") {
|
13759
|
+
index++;
|
13760
|
+
} else if (character === "{") {
|
13761
|
+
braceLevel++;
|
13762
|
+
} else if (character === "}") {
|
13763
|
+
braceLevel--;
|
13764
|
+
}
|
13765
|
+
|
13766
|
+
index++;
|
13767
|
+
}
|
13768
|
+
|
13769
|
+
return -1;
|
13770
|
+
};
|
13771
|
+
|
13772
|
+
const escapeRegex = function(string) {
|
13773
|
+
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
13774
|
+
};
|
13775
|
+
|
13776
|
+
const amsRegex = /^\\(?:begin|(?:eq)?ref){/;
|
13777
|
+
|
13778
|
+
const splitAtDelimiters = function(text, delimiters) {
|
13779
|
+
let index;
|
13780
|
+
const data = [];
|
13781
|
+
|
13782
|
+
const regexLeft = new RegExp(
|
13783
|
+
"(" + delimiters.map((x) => escapeRegex(x.left)).join("|") + ")"
|
13784
|
+
);
|
13785
|
+
|
13786
|
+
while (true) {
|
13787
|
+
index = text.search(regexLeft);
|
13788
|
+
if (index === -1) {
|
13789
|
+
break;
|
13790
|
+
}
|
13791
|
+
if (index > 0) {
|
13792
|
+
data.push({
|
13793
|
+
type: "text",
|
13794
|
+
data: text.slice(0, index)
|
13795
|
+
});
|
13796
|
+
text = text.slice(index); // now text starts with delimiter
|
13797
|
+
}
|
13798
|
+
// ... so this always succeeds:
|
13799
|
+
const i = delimiters.findIndex((delim) => text.startsWith(delim.left));
|
13800
|
+
index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
|
13801
|
+
if (index === -1) {
|
13802
|
+
break;
|
13803
|
+
}
|
13804
|
+
const rawData = text.slice(0, index + delimiters[i].right.length);
|
13805
|
+
const math = amsRegex.test(rawData)
|
13806
|
+
? rawData
|
13807
|
+
: text.slice(delimiters[i].left.length, index);
|
13808
|
+
data.push({
|
13809
|
+
type: "math",
|
13810
|
+
data: math,
|
13811
|
+
rawData,
|
13812
|
+
display: delimiters[i].display
|
13813
|
+
});
|
13814
|
+
text = text.slice(index + delimiters[i].right.length);
|
13815
|
+
}
|
13816
|
+
|
13817
|
+
if (text !== "") {
|
13818
|
+
data.push({
|
13819
|
+
type: "text",
|
13820
|
+
data: text
|
13821
|
+
});
|
13822
|
+
}
|
13823
|
+
|
13824
|
+
return data;
|
13825
|
+
};
|
13826
|
+
|
13827
|
+
const defaultDelimiters = [
|
13828
|
+
{ left: "$$", right: "$$", display: true },
|
13829
|
+
{ left: "\\(", right: "\\)", display: false },
|
13830
|
+
// LaTeX uses $…$, but it ruins the display of normal `$` in text:
|
13831
|
+
// {left: "$", right: "$", display: false},
|
13832
|
+
// $ must come after $$
|
13833
|
+
|
13834
|
+
// Render AMS environments even if outside $$…$$ delimiters.
|
13835
|
+
{ left: "\\begin{equation}", right: "\\end{equation}", display: true },
|
13836
|
+
{ left: "\\begin{equation*}", right: "\\end{equation*}", display: true },
|
13837
|
+
{ left: "\\begin{align}", right: "\\end{align}", display: true },
|
13838
|
+
{ left: "\\begin{align*}", right: "\\end{align*}", display: true },
|
13839
|
+
{ left: "\\begin{alignat}", right: "\\end{alignat}", display: true },
|
13840
|
+
{ left: "\\begin{alignat*}", right: "\\end{alignat*}", display: true },
|
13841
|
+
{ left: "\\begin{gather}", right: "\\end{gather}", display: true },
|
13842
|
+
{ left: "\\begin{gather*}", right: "\\end{gather*}", display: true },
|
13843
|
+
{ left: "\\begin{CD}", right: "\\end{CD}", display: true },
|
13844
|
+
// Ditto \ref & \eqref
|
13845
|
+
{ left: "\\ref{", right: "}", display: false },
|
13846
|
+
{ left: "\\eqref{", right: "}", display: false },
|
13847
|
+
|
13848
|
+
{ left: "\\[", right: "\\]", display: true }
|
13849
|
+
];
|
13850
|
+
|
13851
|
+
const firstDraftDelimiters = {
|
13852
|
+
"$": [
|
13853
|
+
{ left: "$$", right: "$$", display: true },
|
13854
|
+
{ left: "$`", right: "`$", display: false },
|
13855
|
+
{ left: "$", right: "$", display: false }
|
13856
|
+
],
|
13857
|
+
"(": [
|
13858
|
+
{ left: "\\[", right: "\\]", display: true },
|
13859
|
+
{ left: "\\(", right: "\\)", display: false }
|
13860
|
+
]
|
13861
|
+
};
|
13862
|
+
|
13863
|
+
const amsDelimiters = [
|
13864
|
+
{ left: "\\begin{equation}", right: "\\end{equation}", display: true },
|
13865
|
+
{ left: "\\begin{equation*}", right: "\\end{equation*}", display: true },
|
13866
|
+
{ left: "\\begin{align}", right: "\\end{align}", display: true },
|
13867
|
+
{ left: "\\begin{align*}", right: "\\end{align*}", display: true },
|
13868
|
+
{ left: "\\begin{alignat}", right: "\\end{alignat}", display: true },
|
13869
|
+
{ left: "\\begin{alignat*}", right: "\\end{alignat*}", display: true },
|
13870
|
+
{ left: "\\begin{gather}", right: "\\end{gather}", display: true },
|
13871
|
+
{ left: "\\begin{gather*}", right: "\\end{gather*}", display: true },
|
13872
|
+
{ left: "\\begin{CD}", right: "\\end{CD}", display: true },
|
13873
|
+
{ left: "\\ref{", right: "}", display: false },
|
13874
|
+
{ left: "\\eqref{", right: "}", display: false }
|
13875
|
+
];
|
13876
|
+
|
13877
|
+
const delimitersFromKey = key => {
|
13878
|
+
if (key === "$" || key === "(") {
|
13879
|
+
return firstDraftDelimiters[key];
|
13880
|
+
} else if (key === "$+" || key === "(+") {
|
13881
|
+
const firstDraft = firstDraftDelimiters[key.slice(0, 1)];
|
13882
|
+
return firstDraft.concat(amsDelimiters)
|
13883
|
+
} else if (key === "ams") {
|
13884
|
+
return amsDelimiters
|
13885
|
+
} else if (key === "all") {
|
13886
|
+
return (firstDraftDelimiters["("]).concat(firstDraftDelimiters["$"]).concat(amsDelimiters)
|
13887
|
+
} else {
|
13888
|
+
return defaultDelimiters
|
13889
|
+
}
|
13890
|
+
};
|
13891
|
+
|
13892
|
+
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
|
13893
|
+
* API, we should copy it before mutating.
|
13894
|
+
*/
|
13895
|
+
const renderMathInText = function(text, optionsCopy) {
|
13896
|
+
const data = splitAtDelimiters(text, optionsCopy.delimiters);
|
13897
|
+
if (data.length === 1 && data[0].type === "text") {
|
13898
|
+
// There is no formula in the text.
|
13899
|
+
// Let's return null which means there is no need to replace
|
13900
|
+
// the current text node with a new one.
|
13901
|
+
return null;
|
13902
|
+
}
|
13903
|
+
|
13904
|
+
const fragment = document.createDocumentFragment();
|
13905
|
+
|
13906
|
+
for (let i = 0; i < data.length; i++) {
|
13907
|
+
if (data[i].type === "text") {
|
13908
|
+
fragment.appendChild(document.createTextNode(data[i].data));
|
13909
|
+
} else {
|
13910
|
+
const span = document.createElement("span");
|
13911
|
+
let math = data[i].data;
|
13912
|
+
// Override any display mode defined in the settings with that
|
13913
|
+
// defined by the text itself
|
13914
|
+
optionsCopy.displayMode = data[i].display;
|
13915
|
+
try {
|
13916
|
+
if (optionsCopy.preProcess) {
|
13917
|
+
math = optionsCopy.preProcess(math);
|
13918
|
+
}
|
13919
|
+
// Importing render() from temml.js would be a circular dependency.
|
13920
|
+
// So call the global version.
|
13921
|
+
// eslint-disable-next-line no-undef
|
13922
|
+
temml.render(math, span, optionsCopy);
|
13923
|
+
} catch (e) {
|
13924
|
+
if (!(e instanceof ParseError)) {
|
13925
|
+
throw e;
|
13926
|
+
}
|
13927
|
+
optionsCopy.errorCallback(
|
13928
|
+
"Temml auto-render: Failed to parse `" + data[i].data + "` with ",
|
13929
|
+
e
|
13930
|
+
);
|
13931
|
+
fragment.appendChild(document.createTextNode(data[i].rawData));
|
13932
|
+
continue;
|
13933
|
+
}
|
13934
|
+
fragment.appendChild(span);
|
13935
|
+
}
|
13936
|
+
}
|
13937
|
+
|
13938
|
+
return fragment;
|
13939
|
+
};
|
13940
|
+
|
13941
|
+
const renderElem = function(elem, optionsCopy) {
|
13942
|
+
for (let i = 0; i < elem.childNodes.length; i++) {
|
13943
|
+
const childNode = elem.childNodes[i];
|
13944
|
+
if (childNode.nodeType === 3) {
|
13945
|
+
// Text node
|
13946
|
+
const frag = renderMathInText(childNode.textContent, optionsCopy);
|
13947
|
+
if (frag) {
|
13948
|
+
i += frag.childNodes.length - 1;
|
13949
|
+
elem.replaceChild(frag, childNode);
|
13950
|
+
}
|
13951
|
+
} else if (childNode.nodeType === 1) {
|
13952
|
+
// Element node
|
13953
|
+
const className = " " + childNode.className + " ";
|
13954
|
+
const shouldRender =
|
13955
|
+
optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 &&
|
13956
|
+
optionsCopy.ignoredClasses.every((x) => className.indexOf(" " + x + " ") === -1);
|
13957
|
+
|
13958
|
+
if (shouldRender) {
|
13959
|
+
renderElem(childNode, optionsCopy);
|
13960
|
+
}
|
13961
|
+
}
|
13962
|
+
// Otherwise, it's something else, and ignore it.
|
13963
|
+
}
|
13964
|
+
};
|
13965
|
+
|
13966
|
+
const renderMathInElement = function(elem, options) {
|
13967
|
+
if (!elem) {
|
13968
|
+
throw new Error("No element provided to render");
|
13969
|
+
}
|
13970
|
+
|
13971
|
+
const optionsCopy = {};
|
13972
|
+
|
13973
|
+
// Object.assign(optionsCopy, option)
|
13974
|
+
for (const option in options) {
|
13975
|
+
if (Object.prototype.hasOwnProperty.call(options, option)) {
|
13976
|
+
optionsCopy[option] = options[option];
|
13977
|
+
}
|
13978
|
+
}
|
13979
|
+
|
13980
|
+
if (optionsCopy.fences) {
|
13981
|
+
optionsCopy.delimiters = delimitersFromKey(optionsCopy.fences);
|
13982
|
+
} else {
|
13983
|
+
optionsCopy.delimiters = optionsCopy.delimiters || defaultDelimiters;
|
13984
|
+
}
|
13985
|
+
optionsCopy.ignoredTags = optionsCopy.ignoredTags || [
|
13986
|
+
"script",
|
13987
|
+
"noscript",
|
13988
|
+
"style",
|
13989
|
+
"textarea",
|
13990
|
+
"pre",
|
13991
|
+
"code",
|
13992
|
+
"option"
|
13993
|
+
];
|
13994
|
+
optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
|
13995
|
+
// eslint-disable-next-line no-console
|
13996
|
+
optionsCopy.errorCallback = optionsCopy.errorCallback || console.error;
|
13997
|
+
|
13998
|
+
// Enable sharing of global macros defined via `\gdef` between different
|
13999
|
+
// math elements within a single call to `renderMathInElement`.
|
14000
|
+
optionsCopy.macros = optionsCopy.macros || {};
|
14001
|
+
|
14002
|
+
renderElem(elem, optionsCopy);
|
14003
|
+
postProcess(elem);
|
14004
|
+
};
|
14005
|
+
|
13745
14006
|
/* eslint no-console:0 */
|
13746
14007
|
/**
|
13747
14008
|
* This is the main entry point for Temml. Here, we expose functions for
|
@@ -13861,7 +14122,7 @@ const renderToMathMLTree = function(expression, options) {
|
|
13861
14122
|
};
|
13862
14123
|
|
13863
14124
|
/** @type {import('./temml').default} */
|
13864
|
-
var temml = {
|
14125
|
+
var temml$1 = {
|
13865
14126
|
/**
|
13866
14127
|
* Current Temml version
|
13867
14128
|
*/
|
@@ -13876,6 +14137,11 @@ var temml = {
|
|
13876
14137
|
* for sending to the client.
|
13877
14138
|
*/
|
13878
14139
|
renderToString,
|
14140
|
+
/**
|
14141
|
+
* Finds all the math delimiters in a given element of a running HTML document
|
14142
|
+
* and converts the contents of each instance into a <math> element.
|
14143
|
+
*/
|
14144
|
+
renderMathInElement,
|
13879
14145
|
/**
|
13880
14146
|
* Post-process an entire HTML block.
|
13881
14147
|
* Writes AMS auto-numbers and implements \ref{}.
|
@@ -13918,4 +14184,4 @@ var temml = {
|
|
13918
14184
|
__defineMacro: defineMacro
|
13919
14185
|
};
|
13920
14186
|
|
13921
|
-
module.exports = temml;
|
14187
|
+
module.exports = temml$1;
|
package/dist/temml.d.ts
CHANGED
@@ -22,6 +22,8 @@ export function render(
|
|
22
22
|
|
23
23
|
export function renderToString(expression: string, options?: Options): string;
|
24
24
|
|
25
|
+
export function renderMathInElement(block: any): void;
|
26
|
+
|
25
27
|
export function generateParseTree(expression: string, options?: Options): any;
|
26
28
|
|
27
29
|
export function definePreamble(expression: string, options?: Options): any;
|
@@ -48,6 +50,7 @@ declare const Temml: {
|
|
48
50
|
version: string;
|
49
51
|
render: typeof render;
|
50
52
|
renderToString: typeof renderToString;
|
53
|
+
renderMathInElement: typeof renderMathInElement;
|
51
54
|
postProcess: typeof postProcess;
|
52
55
|
ParseError: typeof ParseError;
|
53
56
|
definePreamble: typeof definePreamble;
|
package/dist/temml.js
CHANGED
@@ -2011,7 +2011,7 @@ var temml = (function () {
|
|
2011
2011
|
}
|
2012
2012
|
block.push(node);
|
2013
2013
|
if (node.type && node.type === "mo" && node.children.length === 1 &&
|
2014
|
-
!Object.
|
2014
|
+
!Object.prototype.hasOwnProperty.call(node.attributes, "movablelimits")) {
|
2015
2015
|
const ch = node.children[0].text;
|
2016
2016
|
if (openDelims.indexOf(ch) > -1) {
|
2017
2017
|
level += 1;
|
@@ -11764,7 +11764,7 @@ var temml = (function () {
|
|
11764
11764
|
* https://mit-license.org/
|
11765
11765
|
*/
|
11766
11766
|
|
11767
|
-
const version = "0.
|
11767
|
+
const version = "0.11.00";
|
11768
11768
|
|
11769
11769
|
function postProcess(block) {
|
11770
11770
|
const labelMap = {};
|
@@ -11829,6 +11829,267 @@ var temml = (function () {
|
|
11829
11829
|
});
|
11830
11830
|
}
|
11831
11831
|
|
11832
|
+
const findEndOfMath = function(delimiter, text, startIndex) {
|
11833
|
+
// Adapted from
|
11834
|
+
// https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
|
11835
|
+
let index = startIndex;
|
11836
|
+
let braceLevel = 0;
|
11837
|
+
|
11838
|
+
const delimLength = delimiter.length;
|
11839
|
+
|
11840
|
+
while (index < text.length) {
|
11841
|
+
const character = text[index];
|
11842
|
+
|
11843
|
+
if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
|
11844
|
+
return index;
|
11845
|
+
} else if (character === "\\") {
|
11846
|
+
index++;
|
11847
|
+
} else if (character === "{") {
|
11848
|
+
braceLevel++;
|
11849
|
+
} else if (character === "}") {
|
11850
|
+
braceLevel--;
|
11851
|
+
}
|
11852
|
+
|
11853
|
+
index++;
|
11854
|
+
}
|
11855
|
+
|
11856
|
+
return -1;
|
11857
|
+
};
|
11858
|
+
|
11859
|
+
const escapeRegex = function(string) {
|
11860
|
+
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
11861
|
+
};
|
11862
|
+
|
11863
|
+
const amsRegex = /^\\(?:begin|(?:eq)?ref){/;
|
11864
|
+
|
11865
|
+
const splitAtDelimiters = function(text, delimiters) {
|
11866
|
+
let index;
|
11867
|
+
const data = [];
|
11868
|
+
|
11869
|
+
const regexLeft = new RegExp(
|
11870
|
+
"(" + delimiters.map((x) => escapeRegex(x.left)).join("|") + ")"
|
11871
|
+
);
|
11872
|
+
|
11873
|
+
while (true) {
|
11874
|
+
index = text.search(regexLeft);
|
11875
|
+
if (index === -1) {
|
11876
|
+
break;
|
11877
|
+
}
|
11878
|
+
if (index > 0) {
|
11879
|
+
data.push({
|
11880
|
+
type: "text",
|
11881
|
+
data: text.slice(0, index)
|
11882
|
+
});
|
11883
|
+
text = text.slice(index); // now text starts with delimiter
|
11884
|
+
}
|
11885
|
+
// ... so this always succeeds:
|
11886
|
+
const i = delimiters.findIndex((delim) => text.startsWith(delim.left));
|
11887
|
+
index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
|
11888
|
+
if (index === -1) {
|
11889
|
+
break;
|
11890
|
+
}
|
11891
|
+
const rawData = text.slice(0, index + delimiters[i].right.length);
|
11892
|
+
const math = amsRegex.test(rawData)
|
11893
|
+
? rawData
|
11894
|
+
: text.slice(delimiters[i].left.length, index);
|
11895
|
+
data.push({
|
11896
|
+
type: "math",
|
11897
|
+
data: math,
|
11898
|
+
rawData,
|
11899
|
+
display: delimiters[i].display
|
11900
|
+
});
|
11901
|
+
text = text.slice(index + delimiters[i].right.length);
|
11902
|
+
}
|
11903
|
+
|
11904
|
+
if (text !== "") {
|
11905
|
+
data.push({
|
11906
|
+
type: "text",
|
11907
|
+
data: text
|
11908
|
+
});
|
11909
|
+
}
|
11910
|
+
|
11911
|
+
return data;
|
11912
|
+
};
|
11913
|
+
|
11914
|
+
const defaultDelimiters = [
|
11915
|
+
{ left: "$$", right: "$$", display: true },
|
11916
|
+
{ left: "\\(", right: "\\)", display: false },
|
11917
|
+
// LaTeX uses $…$, but it ruins the display of normal `$` in text:
|
11918
|
+
// {left: "$", right: "$", display: false},
|
11919
|
+
// $ must come after $$
|
11920
|
+
|
11921
|
+
// Render AMS environments even if outside $$…$$ delimiters.
|
11922
|
+
{ left: "\\begin{equation}", right: "\\end{equation}", display: true },
|
11923
|
+
{ left: "\\begin{equation*}", right: "\\end{equation*}", display: true },
|
11924
|
+
{ left: "\\begin{align}", right: "\\end{align}", display: true },
|
11925
|
+
{ left: "\\begin{align*}", right: "\\end{align*}", display: true },
|
11926
|
+
{ left: "\\begin{alignat}", right: "\\end{alignat}", display: true },
|
11927
|
+
{ left: "\\begin{alignat*}", right: "\\end{alignat*}", display: true },
|
11928
|
+
{ left: "\\begin{gather}", right: "\\end{gather}", display: true },
|
11929
|
+
{ left: "\\begin{gather*}", right: "\\end{gather*}", display: true },
|
11930
|
+
{ left: "\\begin{CD}", right: "\\end{CD}", display: true },
|
11931
|
+
// Ditto \ref & \eqref
|
11932
|
+
{ left: "\\ref{", right: "}", display: false },
|
11933
|
+
{ left: "\\eqref{", right: "}", display: false },
|
11934
|
+
|
11935
|
+
{ left: "\\[", right: "\\]", display: true }
|
11936
|
+
];
|
11937
|
+
|
11938
|
+
const firstDraftDelimiters = {
|
11939
|
+
"$": [
|
11940
|
+
{ left: "$$", right: "$$", display: true },
|
11941
|
+
{ left: "$`", right: "`$", display: false },
|
11942
|
+
{ left: "$", right: "$", display: false }
|
11943
|
+
],
|
11944
|
+
"(": [
|
11945
|
+
{ left: "\\[", right: "\\]", display: true },
|
11946
|
+
{ left: "\\(", right: "\\)", display: false }
|
11947
|
+
]
|
11948
|
+
};
|
11949
|
+
|
11950
|
+
const amsDelimiters = [
|
11951
|
+
{ left: "\\begin{equation}", right: "\\end{equation}", display: true },
|
11952
|
+
{ left: "\\begin{equation*}", right: "\\end{equation*}", display: true },
|
11953
|
+
{ left: "\\begin{align}", right: "\\end{align}", display: true },
|
11954
|
+
{ left: "\\begin{align*}", right: "\\end{align*}", display: true },
|
11955
|
+
{ left: "\\begin{alignat}", right: "\\end{alignat}", display: true },
|
11956
|
+
{ left: "\\begin{alignat*}", right: "\\end{alignat*}", display: true },
|
11957
|
+
{ left: "\\begin{gather}", right: "\\end{gather}", display: true },
|
11958
|
+
{ left: "\\begin{gather*}", right: "\\end{gather*}", display: true },
|
11959
|
+
{ left: "\\begin{CD}", right: "\\end{CD}", display: true },
|
11960
|
+
{ left: "\\ref{", right: "}", display: false },
|
11961
|
+
{ left: "\\eqref{", right: "}", display: false }
|
11962
|
+
];
|
11963
|
+
|
11964
|
+
const delimitersFromKey = key => {
|
11965
|
+
if (key === "$" || key === "(") {
|
11966
|
+
return firstDraftDelimiters[key];
|
11967
|
+
} else if (key === "$+" || key === "(+") {
|
11968
|
+
const firstDraft = firstDraftDelimiters[key.slice(0, 1)];
|
11969
|
+
return firstDraft.concat(amsDelimiters)
|
11970
|
+
} else if (key === "ams") {
|
11971
|
+
return amsDelimiters
|
11972
|
+
} else if (key === "all") {
|
11973
|
+
return (firstDraftDelimiters["("]).concat(firstDraftDelimiters["$"]).concat(amsDelimiters)
|
11974
|
+
} else {
|
11975
|
+
return defaultDelimiters
|
11976
|
+
}
|
11977
|
+
};
|
11978
|
+
|
11979
|
+
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
|
11980
|
+
* API, we should copy it before mutating.
|
11981
|
+
*/
|
11982
|
+
const renderMathInText = function(text, optionsCopy) {
|
11983
|
+
const data = splitAtDelimiters(text, optionsCopy.delimiters);
|
11984
|
+
if (data.length === 1 && data[0].type === "text") {
|
11985
|
+
// There is no formula in the text.
|
11986
|
+
// Let's return null which means there is no need to replace
|
11987
|
+
// the current text node with a new one.
|
11988
|
+
return null;
|
11989
|
+
}
|
11990
|
+
|
11991
|
+
const fragment = document.createDocumentFragment();
|
11992
|
+
|
11993
|
+
for (let i = 0; i < data.length; i++) {
|
11994
|
+
if (data[i].type === "text") {
|
11995
|
+
fragment.appendChild(document.createTextNode(data[i].data));
|
11996
|
+
} else {
|
11997
|
+
const span = document.createElement("span");
|
11998
|
+
let math = data[i].data;
|
11999
|
+
// Override any display mode defined in the settings with that
|
12000
|
+
// defined by the text itself
|
12001
|
+
optionsCopy.displayMode = data[i].display;
|
12002
|
+
try {
|
12003
|
+
if (optionsCopy.preProcess) {
|
12004
|
+
math = optionsCopy.preProcess(math);
|
12005
|
+
}
|
12006
|
+
// Importing render() from temml.js would be a circular dependency.
|
12007
|
+
// So call the global version.
|
12008
|
+
// eslint-disable-next-line no-undef
|
12009
|
+
temml.render(math, span, optionsCopy);
|
12010
|
+
} catch (e) {
|
12011
|
+
if (!(e instanceof ParseError)) {
|
12012
|
+
throw e;
|
12013
|
+
}
|
12014
|
+
optionsCopy.errorCallback(
|
12015
|
+
"Temml auto-render: Failed to parse `" + data[i].data + "` with ",
|
12016
|
+
e
|
12017
|
+
);
|
12018
|
+
fragment.appendChild(document.createTextNode(data[i].rawData));
|
12019
|
+
continue;
|
12020
|
+
}
|
12021
|
+
fragment.appendChild(span);
|
12022
|
+
}
|
12023
|
+
}
|
12024
|
+
|
12025
|
+
return fragment;
|
12026
|
+
};
|
12027
|
+
|
12028
|
+
const renderElem = function(elem, optionsCopy) {
|
12029
|
+
for (let i = 0; i < elem.childNodes.length; i++) {
|
12030
|
+
const childNode = elem.childNodes[i];
|
12031
|
+
if (childNode.nodeType === 3) {
|
12032
|
+
// Text node
|
12033
|
+
const frag = renderMathInText(childNode.textContent, optionsCopy);
|
12034
|
+
if (frag) {
|
12035
|
+
i += frag.childNodes.length - 1;
|
12036
|
+
elem.replaceChild(frag, childNode);
|
12037
|
+
}
|
12038
|
+
} else if (childNode.nodeType === 1) {
|
12039
|
+
// Element node
|
12040
|
+
const className = " " + childNode.className + " ";
|
12041
|
+
const shouldRender =
|
12042
|
+
optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 &&
|
12043
|
+
optionsCopy.ignoredClasses.every((x) => className.indexOf(" " + x + " ") === -1);
|
12044
|
+
|
12045
|
+
if (shouldRender) {
|
12046
|
+
renderElem(childNode, optionsCopy);
|
12047
|
+
}
|
12048
|
+
}
|
12049
|
+
// Otherwise, it's something else, and ignore it.
|
12050
|
+
}
|
12051
|
+
};
|
12052
|
+
|
12053
|
+
const renderMathInElement = function(elem, options) {
|
12054
|
+
if (!elem) {
|
12055
|
+
throw new Error("No element provided to render");
|
12056
|
+
}
|
12057
|
+
|
12058
|
+
const optionsCopy = {};
|
12059
|
+
|
12060
|
+
// Object.assign(optionsCopy, option)
|
12061
|
+
for (const option in options) {
|
12062
|
+
if (Object.prototype.hasOwnProperty.call(options, option)) {
|
12063
|
+
optionsCopy[option] = options[option];
|
12064
|
+
}
|
12065
|
+
}
|
12066
|
+
|
12067
|
+
if (optionsCopy.fences) {
|
12068
|
+
optionsCopy.delimiters = delimitersFromKey(optionsCopy.fences);
|
12069
|
+
} else {
|
12070
|
+
optionsCopy.delimiters = optionsCopy.delimiters || defaultDelimiters;
|
12071
|
+
}
|
12072
|
+
optionsCopy.ignoredTags = optionsCopy.ignoredTags || [
|
12073
|
+
"script",
|
12074
|
+
"noscript",
|
12075
|
+
"style",
|
12076
|
+
"textarea",
|
12077
|
+
"pre",
|
12078
|
+
"code",
|
12079
|
+
"option"
|
12080
|
+
];
|
12081
|
+
optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
|
12082
|
+
// eslint-disable-next-line no-console
|
12083
|
+
optionsCopy.errorCallback = optionsCopy.errorCallback || console.error;
|
12084
|
+
|
12085
|
+
// Enable sharing of global macros defined via `\gdef` between different
|
12086
|
+
// math elements within a single call to `renderMathInElement`.
|
12087
|
+
optionsCopy.macros = optionsCopy.macros || {};
|
12088
|
+
|
12089
|
+
renderElem(elem, optionsCopy);
|
12090
|
+
postProcess(elem);
|
12091
|
+
};
|
12092
|
+
|
11832
12093
|
/* eslint no-console:0 */
|
11833
12094
|
/**
|
11834
12095
|
* This is the main entry point for Temml. Here, we expose functions for
|
@@ -11948,7 +12209,7 @@ var temml = (function () {
|
|
11948
12209
|
};
|
11949
12210
|
|
11950
12211
|
/** @type {import('./temml').default} */
|
11951
|
-
var temml = {
|
12212
|
+
var temml$1 = {
|
11952
12213
|
/**
|
11953
12214
|
* Current Temml version
|
11954
12215
|
*/
|
@@ -11963,6 +12224,11 @@ var temml = (function () {
|
|
11963
12224
|
* for sending to the client.
|
11964
12225
|
*/
|
11965
12226
|
renderToString,
|
12227
|
+
/**
|
12228
|
+
* Finds all the math delimiters in a given element of a running HTML document
|
12229
|
+
* and converts the contents of each instance into a <math> element.
|
12230
|
+
*/
|
12231
|
+
renderMathInElement,
|
11966
12232
|
/**
|
11967
12233
|
* Post-process an entire HTML block.
|
11968
12234
|
* Writes AMS auto-numbers and implements \ref{}.
|
@@ -12005,6 +12271,6 @@ var temml = (function () {
|
|
12005
12271
|
__defineMacro: defineMacro
|
12006
12272
|
};
|
12007
12273
|
|
12008
|
-
return temml;
|
12274
|
+
return temml$1;
|
12009
12275
|
|
12010
12276
|
})();
|