Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/qiita/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
require "qiita/markdown/filters/html_toc"
require "qiita/markdown/filters/image_link"
require "qiita/markdown/filters/inline_code_color"
require "qiita/markdown/filters/inline_math"
require "qiita/markdown/filters/mention"
require "qiita/markdown/filters/qiita_marker"
require "qiita/markdown/filters/simplify"
Expand Down
43 changes: 43 additions & 0 deletions lib/qiita/markdown/filters/inline_math.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Qiita
module Markdown
module Filters
class InlineMath < HTML::Pipeline::Filter
def call
doc.search(".//code").each do |code|
opening = code.previous
closing = code.next
replace_with_math_span(code, opening, closing) if inline_math_code?(opening, closing)
end

doc
end

private

def inline_math_code?(opening, closing)
opening.present? && closing.present? && valid_opening?(opening) && valid_closing?(closing)
end

def valid_opening?(opening)
opening.text? && opening.content.end_with?("$") && !opening.content.end_with?("$$")
end

def valid_closing?(closing)
closing.text? && closing.content.start_with?("$") && !closing.content.start_with?("$$")
end

def replace_with_math_span(code, opening, closing)
span = Nokogiri::XML::Node.new("span", doc)
span.add_child(Nokogiri::XML::Text.new("$#{code.text}$", doc))
code.replace(span)
opening.content = opening.content.delete_suffix("$")
opening.remove if opening.content.empty?
closing.content = closing.content.delete_prefix("$")
closing.remove if closing.content.empty?
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/qiita/markdown/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def self.default_filters
Filters::GroupMention,
Filters::ExternalLink,
Filters::InlineCodeColor,
Filters::InlineMath,
Filters::FinalSanitizer,
]
end
Expand Down
77 changes: 77 additions & 0 deletions spec/qiita/markdown/filters/inline_math_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

describe Qiita::Markdown::Filters::InlineMath do
subject(:filter) do
described_class.new(html)
end

context "with dollar signs" do
let(:html) do
<<~HTML
<div>
$<code>A = B</code>$
</div>
HTML
end

it "replaces <code> to <span> with dollars" do
expect(filter.call.to_html).to eq(
<<~HTML,
<div>
<span>$A = B$</span>
</div>
HTML
)
end
end

context "with dollar signs with surrounding text" do
let(:html) do
<<~HTML
<div>
Some text before$<code>A = B</code>$Some text after
</div>
HTML
end

it "replaces <code> to <span> with dollars" do
expect(filter.call.to_html).to eq(
<<~HTML,
<div>
Some text before<span>$A = B$</span>Some text after
</div>
HTML
)
end
end

context "with double dollar signs" do
let(:html) do
<<~HTML
<div>
$$
<code>A = B</code>
$$
</div>
HTML
end

it "does not replace <code>" do
expect(filter.call.to_html).to eq(html)
end
end

context "without dollar signs" do
let(:html) do
<<~HTML
<div>
<code>A = B</code>
</div>
HTML
end

it "does not replace <code>" do
expect(filter.call.to_html).to eq(html)
end
end
end