1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Filter::MathFilter do
using RSpec::Parameterized::TableSyntax
include FilterSpecHelper
shared_examples 'inline math' do
it 'removes surrounding dollar signs and adds class code, math and js-render-math' do
doc = filter(text)
expected = result_template.gsub('<math>', '<code class="code math js-render-math" data-math-style="inline">')
expected.gsub!('</math>', '</code>')
expect(doc.to_s).to eq expected
end
end
shared_examples 'display math' do
let_it_be(:template_prefix_with_pre) { '<pre class="code math js-render-math" data-math-style="display"><code>' }
let_it_be(:template_prefix_with_code) { '<code class="code math js-render-math" data-math-style="display">' }
let(:use_pre_tags) { false }
it 'removes surrounding dollar signs and adds class code, math and js-render-math' do
doc = filter(text)
template_prefix = use_pre_tags ? template_prefix_with_pre : template_prefix_with_code
template_suffix = "</code>#{'</pre>' if use_pre_tags}"
expected = result_template.gsub('<math>', template_prefix)
expected.gsub!('</math>', template_suffix)
expect(doc.to_s).to eq expected
end
end
describe 'inline math using $...$ syntax' do
context 'with valid syntax' do
where(:text, :result_template) do
'$2+2$' | '<math>2+2</math>'
'$22+1$ and $22 + a^2$' | '<math>22+1</math> and <math>22 + a^2</math>'
'$22 and $2+2$' | '$22 and <math>2+2</math>'
'$2+2$ $22 and flightjs/Flight$22 $2+2$' | '<math>2+2</math> $22 and flightjs/Flight$22 <math>2+2</math>'
'$1/2$ <b>test</b>' | '<math>1/2</math> <b>test</b>'
'$a!$' | '<math>a!</math>'
'$x$' | '<math>x</math>'
end
with_them do
it_behaves_like 'inline math'
end
end
it 'does not handle dollar literals properly' do
doc = filter('$20+30\$$')
expected = '<code class="code math js-render-math" data-math-style="inline">20+30\\</code>$'
expect(doc.to_s).to eq expected
end
end
describe 'inline math using $`...`$ syntax' do
context 'with valid syntax' do
where(:text, :result_template) do
'$<code>2+2</code>$' | '<math>2+2</math>'
'$<code>22+1</code>$ and $<code>22 + a^2</code>$' | '<math>22+1</math> and <math>22 + a^2</math>'
'$22 and $<code>2+2</code>$' | '$22 and <math>2+2</math>'
'$<code>2+2</code>$ $22 and flightjs/Flight$22 $<code>2+2</code>$' | '<math>2+2</math> $22 and flightjs/Flight$22 <math>2+2</math>'
'test $$<code>2+2</code>$$ test' | 'test $<math>2+2</math>$ test'
end
with_them do
it_behaves_like 'inline math'
end
end
end
describe 'inline display math using $$...$$ syntax' do
context 'with valid syntax' do
where(:text, :result_template) do
'$$2+2$$' | '<math>2+2</math>'
'$$ 2+2 $$' | '<math>2+2</math>'
'$$22+1$$ and $$22 + a^2$$' | '<math>22+1</math> and <math>22 + a^2</math>'
'$22 and $$2+2$$' | '$22 and <math>2+2</math>'
'$$2+2$$ $22 and flightjs/Flight$22 $$2+2$$' | '<math>2+2</math> $22 and flightjs/Flight$22 <math>2+2</math>'
'flightjs/Flight$22 and $$a^2 + b^2 = c^2$$' | 'flightjs/Flight$22 and <math>a^2 + b^2 = c^2</math>'
'$$a!$$' | '<math>a!</math>'
'$$x$$' | '<math>x</math>'
'$$20,000 and $$30,000' | '<math>20,000 and</math>30,000'
end
with_them do
it_behaves_like 'display math'
end
end
end
describe 'block display math using $$\n...\n$$ syntax' do
context 'with valid syntax' do
where(:text, :result_template) do
"$$\n2+2\n$$" | "<math>2+2</math>"
end
with_them do
it_behaves_like 'display math' do
let(:use_pre_tags) { true }
end
end
end
end
describe 'display math using ```math...``` syntax' do
it 'adds data-math-style display attribute to display math' do
doc = filter('<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>')
pre = doc.xpath('descendant-or-self::pre').first
expect(pre['data-math-style']).to eq 'display'
end
it 'adds js-render-math class to display math' do
doc = filter('<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>')
pre = doc.xpath('descendant-or-self::pre').first
expect(pre[:class]).to include("js-render-math")
end
it 'ignores code blocks that are not math' do
input = '<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true"><code>2+2</code></pre>'
doc = filter(input)
expect(doc.to_s).to eq input
end
it 'requires the pre to contain both code and math' do
input = '<pre class="highlight js-syntax-highlight language-plaintext language-math" v-pre="true"><code>2+2</code></pre>'
doc = filter(input)
expect(doc.to_s).to eq input
end
it 'dollar signs around to display math' do
doc = filter('$<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>$')
before = doc.xpath('descendant-or-self::text()[1]').first
after = doc.xpath('descendant-or-self::text()[3]').first
expect(before.to_s).to eq '$'
expect(after.to_s).to eq '$'
end
end
describe 'unrecognized syntax' do
where(:text) do
[
'<code>2+2</code>',
'test $<code>2+2</code> test',
'test <code>2+2</code>$ test',
'<em>$</em><code>2+2</code><em>$</em>',
'$20,000 and $30,000',
'$20,000 in $USD',
'$ a^2 $',
"test $$\n2+2\n$$",
"$\n$",
'$$$'
]
end
with_them do
it 'is ignored' do
expect(filter(text).to_s).to eq text
end
end
end
it 'handles multiple styles in one text block' do
doc = filter('$<code>2+2</code>$ + $3+3$ + $$4+4$$')
expect(doc.search('.js-render-math').count).to eq(3)
expect(doc.search('[data-math-style="inline"]').count).to eq(2)
expect(doc.search('[data-math-style="display"]').count).to eq(1)
end
it 'limits how many elements can be marked as math' do
stub_const('Banzai::Filter::MathFilter::RENDER_NODES_LIMIT', 2)
doc = filter('$<code>2+2</code>$ + $<code>3+3</code>$ + $<code>4+4</code>$')
expect(doc.search('.js-render-math').count).to eq(2)
end
it 'does not recognize new syntax when feature flag is off' do
stub_feature_flags(markdown_dollar_math: false)
doc = filter('$1+2$')
expect(doc.to_s).to eq '$1+2$'
end
end
|