Sha256: 8f42e112f66f922dfc4a8c462b9a1f2f42d1a0e02599d8da3a3c0ebb4eb34f9b

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

HTML::Pipeline.require_dependency('commonmarker', 'MarkdownFilter')

module HTML
  class Pipeline
    # HTML Filter that converts Markdown text into HTML and converts into a
    # DocumentFragment. This is different from most filters in that it can take a
    # non-HTML as input. It must be used as the first filter in a pipeline.
    #
    # Context options:
    #   :gfm      => false    Disable GFM line-end processing
    #   :commonmarker_extensions => [ :table, :strikethrough,
    #      :tagfilter, :autolink ] Common marker extensions to include
    #
    # This filter does not write any additional information to the context hash.
    class MarkdownFilter < TextFilter
      def initialize(text, context = nil, result = nil)
        super text, context, result
        @text = @text.delete "\r"
      end

      # Convert Markdown to HTML using the best available implementation
      # and convert into a DocumentFragment.
      def call
        options = [:GITHUB_PRE_LANG]
        options << :HARDBREAKS if context[:gfm] != false
        options << :UNSAFE if context[:unsafe]
        extensions = context.fetch(
          :commonmarker_extensions,
          %i[table strikethrough tagfilter autolink]
        )
        html = CommonMarker.render_html(@text, options, extensions)
        html.rstrip!
        html
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
html-pipeline-2.11.0 lib/html/pipeline/markdown_filter.rb
html-pipeline-2.10.0 lib/html/pipeline/markdown_filter.rb
html-pipeline-2.9.2 lib/html/pipeline/markdown_filter.rb
html-pipeline-2.9.1 lib/html/pipeline/markdown_filter.rb
html-pipeline-2.9.0 lib/html/pipeline/markdown_filter.rb