File tree Expand file tree Collapse file tree 4 files changed +126
-0
lines changed
spec/qiita/markdown/filters Expand file tree Collapse file tree 4 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change
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
+ next unless inline_math_code? ( code )
10
+
11
+ replace_with_math_span ( code )
12
+ end
13
+
14
+ doc
15
+ end
16
+
17
+ private
18
+
19
+ def inline_math_code? ( code )
20
+ opening = code . previous
21
+ closing = code . next
22
+
23
+ opening . present? && closing . present? && valid_opening? ( opening ) && valid_closing? ( closing )
24
+ end
25
+
26
+ def valid_opening? ( opening )
27
+ opening . content . end_with? ( "$" ) && !opening . content . end_with? ( "$$" )
28
+ end
29
+
30
+ def valid_closing? ( closing )
31
+ closing . content . start_with? ( "$" ) && !closing . content . start_with? ( "$$" )
32
+ end
33
+
34
+ def replace_with_math_span ( code )
35
+ opening = code . previous
36
+ closing = code . next
37
+
38
+ span = Nokogiri ::XML ::Node . new ( "span" , doc )
39
+ span . add_child ( Nokogiri ::XML ::Text . new ( "$#{ code . text } $" , doc ) )
40
+ code . replace ( span )
41
+ opening . content = opening . content . delete_suffix ( "$" )
42
+ closing . content = closing . content . delete_prefix ( "$" )
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ def self.default_filters
24
24
Filters ::GroupMention ,
25
25
Filters ::ExternalLink ,
26
26
Filters ::InlineCodeColor ,
27
+ Filters ::InlineMath ,
27
28
Filters ::FinalSanitizer ,
28
29
]
29
30
end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments