Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown_filter.rb « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 242e39f5495fa23d3e13f56249ac72399d85a97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

module Banzai
  module Filter
    class MarkdownFilter < HTML::Pipeline::TextFilter
      def initialize(text, context = nil, result = nil)
        super(text, context, result)

        @renderer = renderer(context[:markdown_engine]).new(context)
        @text = @text.delete("\r")
      end

      def call
        @renderer.render(@text).rstrip
      end

      private

      DEFAULT_ENGINE = :common_mark

      def engine(engine_from_context)
        engine_from_context ||= DEFAULT_ENGINE

        engine_from_context.to_s.classify
      end

      def renderer(engine_from_context)
        "Banzai::Filter::MarkdownEngines::#{engine(engine_from_context)}".constantize
      rescue NameError
        raise NameError, "`#{engine_from_context}` is unknown markdown engine"
      end
    end
  end
end