summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunken <mm.munk@gmail.com>2016-12-08 21:52:57 +0000
committerMunken <mm.munk@gmail.com>2016-12-08 21:52:57 +0000
commit9ab1fe5e6536e311142d1daddd0d7c8e29eec20a (patch)
tree45bd9014f00ba62f206fe490c3266d5181571e93
parent6992ac111713be14b050a72a462eb70d9be35ebc (diff)
downloadgitlab-ce-9ab1fe5e6536e311142d1daddd0d7c8e29eec20a.tar.gz
Working inline math filter
-rw-r--r--lib/banzai/filter/inline_math_filter.rb29
-rw-r--r--lib/banzai/pipeline/gfm_pipeline.rb1
-rw-r--r--spec/lib/banzai/filter/inline_math_filter_spec.rb31
3 files changed, 61 insertions, 0 deletions
diff --git a/lib/banzai/filter/inline_math_filter.rb b/lib/banzai/filter/inline_math_filter.rb
new file mode 100644
index 00000000000..a63116c12ce
--- /dev/null
+++ b/lib/banzai/filter/inline_math_filter.rb
@@ -0,0 +1,29 @@
+require 'uri'
+
+module Banzai
+ module Filter
+ # HTML filter that adds class="code math" and removes the dolar sign in $`2+2`$.
+ #
+ class InlineMathFilter < HTML::Pipeline::Filter
+ def call
+ doc.xpath("descendant-or-self::text()[substring(., string-length(.)) = '$']"\
+ "/following-sibling::*[name() = 'code']"\
+ "/following-sibling::text()[starts-with(.,'$')]").each do |el|
+ closing = el
+ code = el.previous
+ code[:class] = 'code math'
+ opening = code.previous
+
+ closing.content = closing.content[1..-1]
+ opening.content = opening.content[0..-2]
+
+ closing
+ end
+
+ puts doc
+
+ doc
+ end
+ end
+ end
+end
diff --git a/lib/banzai/pipeline/gfm_pipeline.rb b/lib/banzai/pipeline/gfm_pipeline.rb
index 5da2d0b008c..2c81cbe56b3 100644
--- a/lib/banzai/pipeline/gfm_pipeline.rb
+++ b/lib/banzai/pipeline/gfm_pipeline.rb
@@ -6,6 +6,7 @@ module Banzai
Filter::SyntaxHighlightFilter,
Filter::SanitizationFilter,
+ Filter::InlineMathFilter,
Filter::UploadLinkFilter,
Filter::VideoLinkFilter,
Filter::ImageLinkFilter,
diff --git a/spec/lib/banzai/filter/inline_math_filter_spec.rb b/spec/lib/banzai/filter/inline_math_filter_spec.rb
new file mode 100644
index 00000000000..1edec83a6f2
--- /dev/null
+++ b/spec/lib/banzai/filter/inline_math_filter_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Banzai::Filter::InlineMathFilter, lib: true do
+ include FilterSpecHelper
+
+ it 'leaves regular inline code unchanged' do
+ doc = filter("<code>2+2</code>")
+ expect(doc.to_s).to eq "<code>2+2</code>"
+ end
+
+ it 'removes surrounding dollar signs and adds class' do
+ doc = filter("$<code>2+2</code>$")
+ expect(doc.to_s).to eq '<code class="code math">2+2</code>'
+ end
+
+ it 'only removes surrounding dollar signs' do
+ doc = filter("test $<code>2+2</code>$ test")
+ expect(doc.to_s).to eq 'test <code class="code math">2+2</code> test'
+ end
+
+ it 'only removes surrounding single dollar sign' do
+ doc = filter("test $$<code>2+2</code>$$ test")
+ expect(doc.to_s).to eq 'test $<code class="code math">2+2</code>$ test'
+ end
+
+ it 'ignores cases with missing dolar sign' do
+ doc = filter("test $<code>2+2</code> test")
+ expect(doc.to_s).to eq 'test $<code>2+2</code> test'
+ end
+
+end