summaryrefslogtreecommitdiff
path: root/lib/banzai
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 /lib/banzai
parent6992ac111713be14b050a72a462eb70d9be35ebc (diff)
downloadgitlab-ce-9ab1fe5e6536e311142d1daddd0d7c8e29eec20a.tar.gz
Working inline math filter
Diffstat (limited to 'lib/banzai')
-rw-r--r--lib/banzai/filter/inline_math_filter.rb29
-rw-r--r--lib/banzai/pipeline/gfm_pipeline.rb1
2 files changed, 30 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,