Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/docs/filters/core/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ module Docs
# Replaces Sphinx/MathJax math markup with native MathML, rendered at
# scrape time by MathRenderer (KaTeX):
# <span class="math">\(...\)</span> and <div class="math">\[...\]</div>
# Display environments such as align* are emitted without delimiters.
class MathFilter < Filter
TEX_DELIMITERS_RGX = /\\\((.*?)\\\)|\\\[(.*?)\\\]/m
TEX_ENVIRONMENT_RGX = /\A\s*\\begin\{(\w+\*?)\}.*\\end\{\1\}\s*\z/m

def call
items = css('span.math', 'div.math').filter_map do |node|
node.css('.eqno').remove
content = node.content
next unless content =~ TEX_DELIMITERS_RGX
[node, split(content)]
if content =~ TEX_DELIMITERS_RGX
[node, split(content)]
elsif content =~ TEX_ENVIRONMENT_RGX
[node, [[content.strip, true]]]
end
end
render(items)
doc
Expand Down
8 changes: 8 additions & 0 deletions test/lib/docs/filters/core/math_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def tex_of(node)
assert_match %r{\Asee <math.+</math> and <math.+</math>\z}, filter_output_string
end

it "renders a bare environment as display math" do
@body = %(<div class="math notranslate nohighlight">\n\\begin{align*} a &amp;= b \\\\ c &amp;= d \\end{align*}</div>)
assert_equal 1, math_nodes.length
assert_equal 'block', math_nodes.first['display']
assert_equal '\begin{align*} a &= b \\\\ c &= d \end{align*}', tex_of(math_nodes.first)
assert_includes math_nodes.first.to_s, '<mtable'
end

it "keeps the equation id and drops the equation number" do
@body = '<div class="math" id="equation-foo"><span class="eqno">(1)<a class="headerlink" href="#equation-foo">露</a></span>\[a\]</div>'
assert_equal 'equation-foo', math_nodes.first['id']
Expand Down
Loading