FIX: don't wrap inline math (#17)

* adds CSS classes for styling

* don't wrap inline-math

* adds comment about selectors
This commit is contained in:
Joe 2019-09-10 12:56:59 +08:00 committed by Sam
parent 9615034d78
commit 595fc8675b
3 changed files with 89 additions and 54 deletions

View File

@ -21,8 +21,10 @@ function decorate(elem) {
$elem.data("applied-katex", true); $elem.data("applied-katex", true);
if ($elem.hasClass("math")) { if ($elem.hasClass("math")) {
const tag = elem.tagName === "DIV" ? "div" : "span";
const displayClass = tag === "div" ? "block-math" : "inline-math";
const text = $elem.text(); const text = $elem.text();
$elem.addClass("math-container").text(""); $elem.addClass(`math-container ${displayClass} katex-math`).text("");
window.katex.render(text, elem, { displayMode }); window.katex.render(text, elem, { displayMode });
} }
} }

View File

@ -48,16 +48,30 @@ function decorate(elem, isPreview) {
if ($elem.hasClass("math")) { if ($elem.hasClass("math")) {
const tag = elem.tagName === "DIV" ? "div" : "span"; const tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : ""; const display = tag === "div" ? "; mode=display" : "";
const displayClass = tag === "div" ? "block-math" : "inline-math";
const type = `math/tex${display}`;
const classList = `math-container ${displayClass} mathjax-math`;
$mathWrapper = $( $mathWrapper = $(
`<${tag} class="math-container" style="display: none;"><script type="math/tex${display}"></script></${tag}>` `<${tag} class="${classList}" style="display: none;">
<script type="${type}"></script>
</${tag}>`
); );
$math = $mathWrapper.children(); $math = $mathWrapper.children();
$math.text($elem.text()); $math.text($elem.text());
$elem.after($mathWrapper); $elem.after($mathWrapper);
} else if ($elem.hasClass("asciimath")) { } else if ($elem.hasClass("asciimath")) {
// asciimath is always inline
const classList = `math-container inline-math ascii-math`;
const type = `math/asciimath`;
$mathWrapper = $( $mathWrapper = $(
`<span class="math-container" style="display: none;"><script type="math/asciimath"></script></span>` `<span class="${classList}" style="display: none;">
<script type="${type}"></script>
</span>`
); );
$math = $mathWrapper.children(); $math = $mathWrapper.children();
$math.text($elem.text()); $math.text($elem.text());
$elem.after($mathWrapper); $elem.after($mathWrapper);

View File

@ -1,16 +1,27 @@
.math-container { .math-container {
&.block-math,
&.ascii-math {
display: block; display: block;
> div:nth-child(2), // we use tag selectors because MathJax does not provide helpfull classes
> span:nth-child(2), // and the possible combinations a bit absurd. The styles are scoped by the
> .katex-display { // parent selector. So they don't affect any other elements.
> {
div,
span {
// Mathjax ships with !important inline; // Mathjax ships with !important inline;
display: block !important; display: block !important;
max-width: 100%; max-width: 100%;
margin-bottom: 1em; margin-bottom: 1em;
// Inner containers for all renderers // Inner containers for all renderers
> :first-child:not(.katex), // mathjax generic
[id*="-Frame"],
> nobr,
> span,
// special handeling for SVG
&.MathJax_SVG,
// special handeling for katex
.katex-html { .katex-html {
display: block; display: block;
overflow: auto hidden; overflow: auto hidden;
@ -19,6 +30,7 @@
box-sizing: border-box; box-sizing: border-box;
} }
} }
}
// On mobile we add a slight fade on either side to indicate that the user can // On mobile we add a slight fade on either side to indicate that the user can
// scroll. However, this should not be added to the composer since mathjax does // scroll. However, this should not be added to the composer since mathjax does
@ -26,6 +38,7 @@
.cooked & { .cooked & {
.mobile-view & { .mobile-view & {
position: relative; position: relative;
&:before { &:before {
content: ""; content: "";
position: absolute; position: absolute;
@ -54,6 +67,12 @@
rgba($secondary, 1) 100% rgba($secondary, 1) 100%
); );
} }
.MJXp-mtable,
.MJXp-mstyle {
padding: 0 0.75em;
}
}
} }
} }
} }