Skip to content

Commit

Permalink
Update mathjax math engine to use code compatible with v2 and v3
Browse files Browse the repository at this point in the history
  • Loading branch information
gettalong committed Apr 17, 2020
1 parent 12dfc07 commit c3acf8d
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 144 deletions.
53 changes: 3 additions & 50 deletions doc/math_engine/mathjax.page
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,13 @@ title: MathJax
HTML pages. It allows for very fine-grained configuration, is widely used and works on all modern
browsers.

This engine marks up math formulas with HTML `<script type="math/tex">` tags that MathJax
understands. The only other thing to do is to include the MathJax library itself on the HTML page.
This engine marks up math formulas with the standard MathJax syntax of `\(...\)` for inline math and
`\[...\]` for block math (works for both version 2.x and 3.x of MathJax). The only other thing to do
is to include the MathJax library itself on the HTML page.

Note that kramdown does *not* ship with the MathJax library and that therefore the "default"
template does *not* include a link to it! The
[MathJax documentation](http://docs.mathjax.org/en/latest/start.html) describes how to add a link
to MathJax to your page.

## Using KaTeX instead of MathJax

To use [KaTeX](https://khan.github.io/KaTeX/) instead of MathJax, activate the MathJax engine in
kramdown, include the KaTeX Javascript files in the HTML file and put the following script at the
end of the HTML file (note that JQuery is used for some parts but it can probably be done without
it):

~~~ html
<script>
$("script[type='math/tex']").replaceWith(function() {
var tex = $(this).text();
return katex.renderToString(tex, \{displayMode: false});
});

$("script[type='math/tex; mode=display']").replaceWith(function() {
var tex = $(this).html();
return katex.renderToString(tex.replace(/%.*/g, ''), \{displayMode: true});
});
</script>
~~~

Note that including the KaTeX Javascript files and the above script is normally done using a
template file (see the [template option](../options.html).

See [this comment](https://github.com/gettalong/kramdown/issues/292#issuecomment-257770946) for more
details.


## Options

The MathJax engine supports the following keys of the option
['math_engine_opts'](../options.html#option-math-engine-opts):

preview
: Specifies whether a preview should be shown. It defaults to `false` but if set to `true`, a
preview with the LaTeX code itself is shown. If it is set to any value other than `false` or
`true`, that value is shown as the preview.

preview_as_code
: When `true`, wraps the LaTeX code in `code` / `pre > code` tags instead of `span` / `div` tags,
mirroring the structure of inline code / code blocks. This ensures the content is styled like
regular code and improves legibility in RSS readers, text browsers, etc.

To prevent styling issues after MathJax has loaded, consider adding
`.MathJax_Preview:empty { display:none }` to your CSS.

Defaults to `false` and only works if `preview` is `true`.

[MathJax]: http://www.mathjax.org
37 changes: 2 additions & 35 deletions lib/kramdown/converter/math_engine/mathjax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,8 @@ module Kramdown::Converter::MathEngine
module Mathjax

def self.call(converter, el, opts)
type = el.options[:category]
text = (el.value =~ /<|&/ ? "% <![CDATA[\n#{el.value} %]]>" : el.value).dup
text.gsub!(/<\/?script>?/, '')

preview = preview_string(converter, el, opts).dup

attr = {type: "math/tex#{type == :block ? '; mode=display' : ''}"}
preview << if type == :block
converter.format_as_block_html('script', attr, text, opts[:indent])
else
converter.format_as_span_html('script', attr, text)
end
end

def self.preview_string(converter, el, opts)
preview = converter.options[:math_engine_opts][:preview]
return '' unless preview

preview = (preview == true ? converter.escape_html(el.value) : preview.to_s)

preview_as_code = converter.options[:math_engine_opts][:preview_as_code]

if el.options[:category] == :block
if preview_as_code
converter.format_as_block_html('pre', {'class' => 'MathJax_Preview'},
converter.format_as_span_html('code', {}, preview),
opts[:indent])
else
converter.format_as_block_html('div', {'class' => 'MathJax_Preview'}, preview,
opts[:indent])
end
else
converter.format_as_span_html(preview_as_code ? 'code' : 'span',
{'class' => 'MathJax_Preview'}, preview)
end
value = converter.escape_html(el.value)
el.options[:category] == :block ? "\\[#{value}\\]\n" : "\\(#{value}\\)"
end

end
Expand Down
6 changes: 3 additions & 3 deletions test/test_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ def tidy_output(out)
'test/testcases/block/16_toc/toc_exclude.html', # bc of different attribute ordering
'test/testcases/span/autolinks/url_links.html', # bc of quot entity being converted to char
'test/testcases/block/14_table/empty_tag_in_cell.html', # bc of tidy
'test/testcases/block/15_math/mathjax_preview.html', # bc of mathjax preview
'test/testcases/block/15_math/mathjax_preview_simple.html', # bc of mathjax preview
'test/testcases/block/15_math/mathjax_preview_as_code.html', # bc of mathjax preview
'test/testcases/span/01_link/link_defs_with_ial.html', # bc of attribute ordering
'test/testcases/span/05_html/mark_element.html', # bc of tidy
'test/testcases/block/09_html/xml.html', # bc of tidy
'test/testcases/span/05_html/xml.html', # bc of tidy
'test/testcases/block/03_paragraph/standalone_image.html', # bc of standalone image
'test/testcases/block/15_math/normal.html', # bc of mathjax and HTML parser
'test/testcases/block/15_math/gh_128.html', # bc of mathjax and HTML parser
'test/testcases/span/04_footnote/backlink_inline.html', # bc of mathjax
].compact
Dir[File.dirname(__FILE__) + '/testcases/**/*.html'].each do |html_file|
next if EXCLUDE_HTML_KD_FILES.any? {|f| html_file =~ /#{f}$/ }
Expand Down
3 changes: 1 addition & 2 deletions test/testcases/block/15_math/gh_128.html
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<script type="math/tex; mode=display">% <![CDATA[
alert('a') alert('b<') %]]></script>
\[&lt;script&gt;alert('a')&lt;/script&gt; &lt;script&gt;alert('b&lt;')&lt;/script&gt;\]
4 changes: 0 additions & 4 deletions test/testcases/block/15_math/mathjax_preview.html

This file was deleted.

2 changes: 0 additions & 2 deletions test/testcases/block/15_math/mathjax_preview.options

This file was deleted.

5 changes: 0 additions & 5 deletions test/testcases/block/15_math/mathjax_preview.text

This file was deleted.

4 changes: 0 additions & 4 deletions test/testcases/block/15_math/mathjax_preview_as_code.html

This file was deleted.

3 changes: 0 additions & 3 deletions test/testcases/block/15_math/mathjax_preview_as_code.options

This file was deleted.

5 changes: 0 additions & 5 deletions test/testcases/block/15_math/mathjax_preview_as_code.text

This file was deleted.

4 changes: 0 additions & 4 deletions test/testcases/block/15_math/mathjax_preview_simple.html

This file was deleted.

2 changes: 0 additions & 2 deletions test/testcases/block/15_math/mathjax_preview_simple.options

This file was deleted.

5 changes: 0 additions & 5 deletions test/testcases/block/15_math/mathjax_preview_simple.text

This file was deleted.

29 changes: 14 additions & 15 deletions test/testcases/block/15_math/normal.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
<p>This is a para.
<script type="math/tex">\text{LaTeX} \lambda_5</script></p>
\(\text{LaTeX} \lambda_5\)</p>

<script type="math/tex; mode=display">\lambda_5 = \alpha + 4</script>
\[\lambda_5 = \alpha + 4\]

<p><script type="math/tex">\lambda_\alpha > 5</script>
<p>\(\lambda_\alpha &gt; 5\)
This is a para.</p>

<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
&=5 \\
&=6 \\
\end{align*} %]]></script>
\[\begin{align*}
&amp;=5 \\
&amp;=6 \\
\end{align*}\]

<script type="math/tex; mode=display">5+5</script>
\[5+5\]

<script type="math/tex; mode=display">5+5</script>
\[5+5\]

<script type="math/tex; mode=display">5+5</script>
\[5+5\]

<script type="math/tex; mode=display">5+5</script>
\[5+5\]

<pre><code>$$5+5$$
</code></pre>

<script type="math/tex; mode=display">5+5</script>
<script type="math/tex; mode=display">5+5</script>
\[5+5\]
\[5+5\]

<script type="math/tex; mode=display">|x| = 5</script>
\[|x| = 5\]
2 changes: 1 addition & 1 deletion test/testcases/span/04_footnote/backlink_inline.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h1 id="header">header <a href="#fnref:list" class="reversefootnote">&#8617;</a
</li>
<li id="fn:mathblock">

<script type="math/tex; mode=display">x + 2</script>
\[x + 2\]
<p><a href="#fnref:mathblock" class="reversefootnote">&#8617;</a></p>
</li>
<li id="fn:html">
Expand Down
8 changes: 4 additions & 4 deletions test/testcases/span/math/normal.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<p>This is <script type="math/tex">\lambda_\alpha > 5</script> some math. With <script type="math/tex">1
+ 1</script> new line characters in between.</p>
<p>This is \(\lambda_\alpha &gt; 5\) some math. With \(1
+ 1\) new line characters in between.</p>

<p><script type="math/tex">5+5</script> inline math, $5.00 $$no math$$</p>
<p>\(5+5\) inline math, $5.00 $$no math$$</p>

<p>$$5+5$$ inline math</p>

<p><script type="math/tex">5+5</script></p>
<p>\(5+5\)</p>

<p>$$5+5$$</p>

0 comments on commit c3acf8d

Please sign in to comment.