qiita-markdown 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a030fef1d6e7c96945f3f3f32039906074ed0d7ca7773b857939740c2bbe2c1
|
4
|
+
data.tar.gz: 1fb0ec10eb4849a69f49942565b6fd2250c1548244267f3a29643f8a3558a3b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7955e98dd0f03db994cd7e04060269aed6763bb17bf1742064098f193b53f22d0866a36cc1d18ac95502c667f807e7966a29144035e254d45578ca374a63cc6d
|
7
|
+
data.tar.gz: 7ef8f9c22b7159da5e02cf9366584d39cd375dafb6666dc364de22f204b724f391fecd3f16793388f2c090354204ef8cb6d6fc1f37282cf3abb4b11baa67b378
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qiita
|
4
|
+
module Markdown
|
5
|
+
module Filters
|
6
|
+
class InlineMath < HTML::Pipeline::Filter
|
7
|
+
def call
|
8
|
+
doc.search(".//code").each do |code|
|
9
|
+
opening = code.previous
|
10
|
+
closing = code.next
|
11
|
+
replace_with_math_span(code, opening, closing) if inline_math_code?(opening, closing)
|
12
|
+
end
|
13
|
+
|
14
|
+
doc
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def inline_math_code?(opening, closing)
|
20
|
+
opening.present? && closing.present? && valid_opening?(opening) && valid_closing?(closing)
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid_opening?(opening)
|
24
|
+
opening.text? && opening.content.end_with?("$") && !opening.content.end_with?("$$")
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid_closing?(closing)
|
28
|
+
closing.text? && closing.content.start_with?("$") && !closing.content.start_with?("$$")
|
29
|
+
end
|
30
|
+
|
31
|
+
def replace_with_math_span(code, opening, closing)
|
32
|
+
span = Nokogiri::XML::Node.new("span", doc)
|
33
|
+
span.add_child(Nokogiri::XML::Text.new("$#{code.text}$", doc))
|
34
|
+
code.replace(span)
|
35
|
+
opening.content = opening.content.delete_suffix("$")
|
36
|
+
opening.remove if opening.content.empty?
|
37
|
+
closing.content = closing.content.delete_prefix("$")
|
38
|
+
closing.remove if closing.content.empty?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/qiita/markdown.rb
CHANGED
@@ -36,6 +36,7 @@ require "qiita/markdown/filters/heading_anchor"
|
|
36
36
|
require "qiita/markdown/filters/html_toc"
|
37
37
|
require "qiita/markdown/filters/image_link"
|
38
38
|
require "qiita/markdown/filters/inline_code_color"
|
39
|
+
require "qiita/markdown/filters/inline_math"
|
39
40
|
require "qiita/markdown/filters/mention"
|
40
41
|
require "qiita/markdown/filters/qiita_marker"
|
41
42
|
require "qiita/markdown/filters/simplify"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Qiita::Markdown::Filters::InlineMath do
|
4
|
+
subject(:filter) do
|
5
|
+
described_class.new(html)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "with dollar signs" do
|
9
|
+
let(:html) do
|
10
|
+
<<~HTML
|
11
|
+
<div>
|
12
|
+
$<code>A = B</code>$
|
13
|
+
</div>
|
14
|
+
HTML
|
15
|
+
end
|
16
|
+
|
17
|
+
it "replaces <code> to <span> with dollars" do
|
18
|
+
expect(filter.call.to_html).to eq(
|
19
|
+
<<~HTML,
|
20
|
+
<div>
|
21
|
+
<span>$A = B$</span>
|
22
|
+
</div>
|
23
|
+
HTML
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with dollar signs with surrounding text" do
|
29
|
+
let(:html) do
|
30
|
+
<<~HTML
|
31
|
+
<div>
|
32
|
+
Some text before$<code>A = B</code>$Some text after
|
33
|
+
</div>
|
34
|
+
HTML
|
35
|
+
end
|
36
|
+
|
37
|
+
it "replaces <code> to <span> with dollars" do
|
38
|
+
expect(filter.call.to_html).to eq(
|
39
|
+
<<~HTML,
|
40
|
+
<div>
|
41
|
+
Some text before<span>$A = B$</span>Some text after
|
42
|
+
</div>
|
43
|
+
HTML
|
44
|
+
)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with double dollar signs" do
|
49
|
+
let(:html) do
|
50
|
+
<<~HTML
|
51
|
+
<div>
|
52
|
+
$$
|
53
|
+
<code>A = B</code>
|
54
|
+
$$
|
55
|
+
</div>
|
56
|
+
HTML
|
57
|
+
end
|
58
|
+
|
59
|
+
it "does not replace <code>" do
|
60
|
+
expect(filter.call.to_html).to eq(html)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "without dollar signs" do
|
65
|
+
let(:html) do
|
66
|
+
<<~HTML
|
67
|
+
<div>
|
68
|
+
<code>A = B</code>
|
69
|
+
</div>
|
70
|
+
HTML
|
71
|
+
end
|
72
|
+
|
73
|
+
it "does not replace <code>" do
|
74
|
+
expect(filter.call.to_html).to eq(html)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qiita-markdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -167,6 +167,7 @@ files:
|
|
167
167
|
- lib/qiita/markdown/filters/html_toc.rb
|
168
168
|
- lib/qiita/markdown/filters/image_link.rb
|
169
169
|
- lib/qiita/markdown/filters/inline_code_color.rb
|
170
|
+
- lib/qiita/markdown/filters/inline_math.rb
|
170
171
|
- lib/qiita/markdown/filters/mention.rb
|
171
172
|
- lib/qiita/markdown/filters/qiita_marker.rb
|
172
173
|
- lib/qiita/markdown/filters/simplify.rb
|
@@ -187,6 +188,7 @@ files:
|
|
187
188
|
- spec/qiita/markdown/filters/heading_anchor_spec.rb
|
188
189
|
- spec/qiita/markdown/filters/html_toc_spec.rb
|
189
190
|
- spec/qiita/markdown/filters/inline_code_color_spec.rb
|
191
|
+
- spec/qiita/markdown/filters/inline_math_spec.rb
|
190
192
|
- spec/qiita/markdown/filters/qiita_marker_spec.rb
|
191
193
|
- spec/qiita/markdown/processor_spec.rb
|
192
194
|
- spec/qiita/markdown/summary_processor_spec.rb
|