summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/math_filter.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/banzai/filter/math_filter.rb')
-rw-r--r--lib/banzai/filter/math_filter.rb96
1 files changed, 19 insertions, 77 deletions
diff --git a/lib/banzai/filter/math_filter.rb b/lib/banzai/filter/math_filter.rb
index 1d854d6599b..9b6fc71077a 100644
--- a/lib/banzai/filter/math_filter.rb
+++ b/lib/banzai/filter/math_filter.rb
@@ -1,55 +1,29 @@
# frozen_string_literal: true
-require 'uri'
-
# Generated HTML is transformed back to GFM by:
# - app/assets/javascripts/behaviors/markdown/marks/math.js
# - app/assets/javascripts/behaviors/markdown/nodes/code_block.js
module Banzai
module Filter
- # HTML filter that implements our math syntax, adding class="code math"
+ # HTML filter that implements the original GitLab math syntax, one of three filters:
+ # DollarMathPreFilter, DollarMathPostFilter, and MathFilter
#
class MathFilter < HTML::Pipeline::Filter
+ # Handle the $`...`$ and ```math syntax in this filter.
+ # Also add necessary classes any existing math blocks.
+
CSS_MATH = 'pre[lang="math"] > code'
XPATH_MATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_MATH).freeze
CSS_CODE = 'code'
XPATH_CODE = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_CODE).freeze
-
- # These are based on the Pandoc heuristics,
- # https://pandoc.org/MANUAL.html#extension-tex_math_dollars
- # Note: at this time, using a dollar sign literal, `\$` inside
- # a math statement does not work correctly.
- # Corresponds to the "$...$" syntax
- DOLLAR_INLINE_PATTERN = %r{
- (?<matched>\$(?<math>(?:\S[^$\n]*?\S|[^$\s]))\$)(?:[^\d]|$)
- }x.freeze
-
- # Corresponds to the "$$...$$" syntax
- DOLLAR_DISPLAY_INLINE_PATTERN = %r{
- (?<matched>\$\$\ *(?<math>[^$\n]+?)\ *\$\$)
- }x.freeze
-
- # Corresponds to the $$\n...\n$$ syntax
- DOLLAR_DISPLAY_BLOCK_PATTERN = %r{
- ^(?<matched>\$\$\ *\n(?<math>.*)\n\$\$\ *)$
- }mx.freeze
-
- # Order dependent. Handle the `$$` syntax before the `$` syntax
- DOLLAR_MATH_PIPELINE = [
- { pattern: DOLLAR_DISPLAY_INLINE_PATTERN, tag: :code, style: :display },
- { pattern: DOLLAR_DISPLAY_BLOCK_PATTERN, tag: :pre, style: :display },
- { pattern: DOLLAR_INLINE_PATTERN, tag: :code, style: :inline }
- ].freeze
-
- # Do not recognize math inside these tags
- IGNORED_ANCESTOR_TAGS = %w[pre code tt].to_set
+ CSS_INLINE_CODE = 'code[data-math-style]'
+ XPATH_INLINE_CODE = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_INLINE_CODE).freeze
# Attribute indicating inline or display math.
STYLE_ATTRIBUTE = 'data-math-style'
# Class used for tagging elements that should be rendered
TAG_CLASS = 'js-render-math'
-
MATH_CLASSES = "code math #{TAG_CLASS}"
DOLLAR_SIGN = '$'
@@ -61,47 +35,31 @@ module Banzai
def call
@nodes_count = 0
- process_dollar_pipeline if Feature.enabled?(:markdown_dollar_math, group)
-
+ process_existing
process_dollar_backtick_inline
process_math_codeblock
doc
end
- def process_dollar_pipeline
- doc.xpath('descendant-or-self::text()').each do |node|
- next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
-
- node_html = node.to_html
- next unless node_html.match?(DOLLAR_INLINE_PATTERN) ||
- node_html.match?(DOLLAR_DISPLAY_INLINE_PATTERN) ||
- node_html.match?(DOLLAR_DISPLAY_BLOCK_PATTERN)
-
- temp_doc = Nokogiri::HTML.fragment(node_html)
- DOLLAR_MATH_PIPELINE.each do |pipeline|
- temp_doc.xpath('child::text()').each do |temp_node|
- html = temp_node.to_html
- temp_node.content.scan(pipeline[:pattern]).each do |matched, math|
- html.sub!(matched, math_html(tag: pipeline[:tag], style: pipeline[:style], math: math))
-
- @nodes_count += 1
- break if @nodes_count >= RENDER_NODES_LIMIT
- end
+ private
- temp_node.replace(html)
+ # Add necessary classes to any existing math blocks
+ def process_existing
+ doc.xpath(XPATH_INLINE_CODE).each do |code|
+ break if @nodes_count >= RENDER_NODES_LIMIT
- break if @nodes_count >= RENDER_NODES_LIMIT
- end
- end
+ code[:class] = MATH_CLASSES
- node.replace(temp_doc)
+ @nodes_count += 1
end
end
# Corresponds to the "$`...`$" syntax
def process_dollar_backtick_inline
doc.xpath(XPATH_CODE).each do |code|
+ break if @nodes_count >= RENDER_NODES_LIMIT
+
closing = code.next
opening = code.previous
@@ -112,17 +70,16 @@ module Banzai
closing.content.first == DOLLAR_SIGN &&
opening.content.last == DOLLAR_SIGN
- code[:class] = MATH_CLASSES
code[STYLE_ATTRIBUTE] = 'inline'
+ code[:class] = MATH_CLASSES
closing.content = closing.content[1..]
opening.content = opening.content[0..-2]
@nodes_count += 1
- break if @nodes_count >= RENDER_NODES_LIMIT
end
end
- # corresponds to the "```math...```" syntax
+ # Corresponds to the "```math...```" syntax
def process_math_codeblock
doc.xpath(XPATH_MATH).each do |node|
pre_node = node.parent
@@ -130,21 +87,6 @@ module Banzai
pre_node[:class] = TAG_CLASS
end
end
-
- private
-
- def math_html(tag:, math:, style:)
- case tag
- when :code
- "<code class=\"#{MATH_CLASSES}\" data-math-style=\"#{style}\">#{math}</code>"
- when :pre
- "<pre class=\"#{MATH_CLASSES}\" data-math-style=\"#{style}\"><code>#{math}</code></pre>"
- end
- end
-
- def group
- context[:group] || context[:project]&.group
- end
end
end
end