Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a0a579

Browse files
authored
Merge pull request#209 from increments/add-inline-math-filter
Add inline math filter
2 parents32910bf +aed13f4 commit9a0a579

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

‎lib/qiita/markdown.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
require"qiita/markdown/filters/html_toc"
3737
require"qiita/markdown/filters/image_link"
3838
require"qiita/markdown/filters/inline_code_color"
39+
require"qiita/markdown/filters/inline_math"
3940
require"qiita/markdown/filters/mention"
4041
require"qiita/markdown/filters/qiita_marker"
4142
require"qiita/markdown/filters/simplify"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
moduleQiita
4+
moduleMarkdown
5+
moduleFilters
6+
classInlineMath <HTML::Pipeline::Filter
7+
defcall
8+
doc.search(".//code").eachdo |code|
9+
opening=code.previous
10+
closing=code.next
11+
replace_with_math_span(code,opening,closing)ifinline_math_code?(opening,closing)
12+
end
13+
14+
doc
15+
end
16+
17+
private
18+
19+
definline_math_code?(opening,closing)
20+
opening.present? &&closing.present? &&valid_opening?(opening) &&valid_closing?(closing)
21+
end
22+
23+
defvalid_opening?(opening)
24+
opening.text? &&opening.content.end_with?("$") && !opening.content.end_with?("$$")
25+
end
26+
27+
defvalid_closing?(closing)
28+
closing.text? &&closing.content.start_with?("$") && !closing.content.start_with?("$$")
29+
end
30+
31+
defreplace_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.removeifopening.content.empty?
37+
closing.content=closing.content.delete_prefix("$")
38+
closing.removeifclosing.content.empty?
39+
end
40+
end
41+
end
42+
end
43+
end

‎lib/qiita/markdown/processor.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def self.default_filters
2424
Filters::GroupMention,
2525
Filters::ExternalLink,
2626
Filters::InlineCodeColor,
27+
Filters::InlineMath,
2728
Filters::FinalSanitizer,
2829
]
2930
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
describeQiita::Markdown::Filters::InlineMathdo
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).toeq(
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).toeq(
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).toeq(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).toeq(html)
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp