Commit 5e1080c5 authored by Robert Speicher's avatar Robert Speicher Committed by Douglas Barbosa Alexandre

Merge branch 'inline-math-dollar' into 'master'

Don't render inline math when dollar signs are inside markup

See merge request !8259
parent 60ed1a25
...@@ -5,12 +5,6 @@ module Banzai ...@@ -5,12 +5,6 @@ module Banzai
# HTML filter that adds class="code math" and removes the dollar sign in $`2+2`$. # HTML filter that adds class="code math" and removes the dollar sign in $`2+2`$.
# #
class MathFilter < HTML::Pipeline::Filter class MathFilter < HTML::Pipeline::Filter
# This picks out <code>...</code>.
INLINE_MATH = 'descendant-or-self::code'.freeze
# Pick out a code block which is declared math
DISPLAY_MATH = "descendant-or-self::pre[contains(@class, 'math') and contains(@class, 'code')]".freeze
# Attribute indicating inline or display math. # Attribute indicating inline or display math.
STYLE_ATTRIBUTE = 'data-math-style'.freeze STYLE_ATTRIBUTE = 'data-math-style'.freeze
...@@ -22,13 +16,14 @@ module Banzai ...@@ -22,13 +16,14 @@ module Banzai
DOLLAR_SIGN = '$'.freeze DOLLAR_SIGN = '$'.freeze
def call def call
doc.xpath(INLINE_MATH).each do |code| doc.css('code').each do |code|
closing = code.next closing = code.next
opening = code.previous opening = code.previous
# We need a sibling before and after. # We need a sibling before and after.
# They should end and start with $ respectively. # They should end and start with $ respectively.
if closing && opening && if closing && opening &&
closing.text? && opening.text? &&
closing.content.first == DOLLAR_SIGN && closing.content.first == DOLLAR_SIGN &&
opening.content.last == DOLLAR_SIGN opening.content.last == DOLLAR_SIGN
...@@ -39,7 +34,7 @@ module Banzai ...@@ -39,7 +34,7 @@ module Banzai
end end
end end
doc.xpath(DISPLAY_MATH).each do |el| doc.css('pre.code.math').each do |el|
el[STYLE_ATTRIBUTE] = 'display' el[STYLE_ATTRIBUTE] = 'display'
el[:class] += " #{TAG_CLASS}" el[:class] += " #{TAG_CLASS}"
end end
......
...@@ -79,6 +79,13 @@ describe Banzai::Filter::MathFilter, lib: true do ...@@ -79,6 +79,13 @@ describe Banzai::Filter::MathFilter, lib: true do
expect(doc.to_s).to eq input expect(doc.to_s).to eq input
end end
it 'ignores dollar signs if they are inside another element' do
input = '<p>We check strictly <em>$</em><code>2+2</code><em>$</em></p>'
doc = filter(input)
expect(doc.to_s).to eq input
end
# Display math # Display math
it 'adds data-math-style display attribute to display math' do it 'adds data-math-style display attribute to display math' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment