feat: Set Auto-render to recognize AMS environments without $$…$$ delimiters. (#2701)

* feat: Set Auto-render to recognize AMS environments without $$…$$ delimiters.

* Replace tab with spaces

* Treat AMS environments correctly.
This commit is contained in:
Ron Kok
2020-12-27 15:29:55 -08:00
committed by GitHub
parent 26ed77f598
commit 5c44c47bd2
5 changed files with 38 additions and 6 deletions

View File

@@ -96,7 +96,14 @@ const renderMathInElement = function(elem, options) {
{left: "\\(", right: "\\)", display: false},
// LaTeX uses $…$, but it ruins the display of normal `$` in text:
// {left: "$", right: "$", display: false},
// $ must come after $$
// $ must come after $$
// Render AMS environments even if outside $$…$$ delimiters.
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
{left: "\\begin{align}", right: "\\end{align}", display: true},
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
{left: "\\[", right: "\\]", display: true},
];
optionsCopy.ignoredTags = optionsCopy.ignoredTags || [

View File

@@ -25,13 +25,15 @@
<div id="test">
This is some text $math \frac12$ other text $\unsupported$
<span class="blue">
Other node \[ displaymath \frac{1}{2} \] blah $$ \int_2^3 $$
Other node \[ \text{displaymath} \frac{1}{2} \] blah $$ \int_2^3 $$
</span>
and some <!-- comment --> more text \(and math\) blah. And $math with a
\$ sign$.
<pre>
Stuff in a $pre tag$
</pre>
<p>An AMS environment without <code>$$…$$</code> delimiters.</p>
<p>\begin{equation} \begin{split} a &=b+c\\ &=e+f \end{split} \end{equation}</p>
</div>
<script>
renderMathInElement(
@@ -39,9 +41,13 @@
{
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "\\[", right: "\\]", display: true},
{left: "$", right: "$", display: false},
{left: "\\(", right: "\\)", display: false}
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
{left: "\\begin{align}", right: "\\end{align}", display: true},
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "\\[", right: "\\]", display: true}
]
}
);

View File

@@ -31,6 +31,8 @@ const escapeRegex = function(string) {
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
};
const amsRegex = /^\\begin{/;
const splitAtDelimiters = function(text, delimiters) {
let index;
const data = [];
@@ -57,10 +59,14 @@ const splitAtDelimiters = function(text, delimiters) {
if (index === -1) {
break;
}
const rawData = text.slice(0, index + delimiters[i].right.length);
const math = amsRegex.test(rawData)
? rawData
: text.slice(delimiters[i].left.length, index);
data.push({
type: "math",
data: text.slice(delimiters[i].left.length, index),
rawData: text.slice(0, index + delimiters[i].right.length),
data: math,
rawData,
display: delimiters[i].display,
});
text = text.slice(index + delimiters[i].right.length);

View File

@@ -97,6 +97,15 @@ describe("A delimiter splitter", function() {
rawData: "[[ world ]]", display: false},
{type: "text", data: " boo"},
]);
expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
"\\begin{equation}", "\\end{equation}",
[
{type: "text", data: "hello "},
{type: "math", data: "\\begin{equation} world \\end{equation}",
rawData: "\\begin{equation} world \\end{equation}",
display: false},
{type: "text", data: " boo"},
]);
});
it("splits mutliple times", function() {

View File

@@ -84,6 +84,10 @@ in addition to two auto-render-specific keys:
[
{left: "$$", right: "$$", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
{left: "\\begin{align}", right: "\\end{align}", display: true},
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
{left: "\\[", right: "\\]", display: true}
]
```