Class: HTML::Pipeline::MarkdownFilter
- Inherits:
-
TextFilter
- Object
- Filter
- TextFilter
- HTML::Pipeline::MarkdownFilter
- Defined in:
- lib/html/pipeline/markdown_filter.rb
Overview
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 ] Commonmarker extensions to include
This filter does not write any additional information to the context hash.
Constant Summary collapse
- DEFAULT_COMMONMARKER_EXTENSIONS =
%i[table strikethrough tagfilter autolink].freeze
Instance Attribute Summary
Attributes inherited from TextFilter
Attributes inherited from Filter
Instance Method Summary collapse
-
#call ⇒ Object
Convert Markdown to HTML using the best available implementation and convert into a DocumentFragment.
-
#initialize(text, context = nil, result = nil) ⇒ MarkdownFilter
constructor
A new instance of MarkdownFilter.
Methods inherited from Filter
#base_url, call, #current_user, #doc, #has_ancestor?, #html, #needs, #parse_html, #repository, to_document, to_html, #validate
Constructor Details
#initialize(text, context = nil, result = nil) ⇒ MarkdownFilter
Returns a new instance of MarkdownFilter.
20 21 22 23 |
# File 'lib/html/pipeline/markdown_filter.rb', line 20 def initialize(text, context = nil, result = nil) super text, context, result @text = @text.delete "\r" end |
Instance Method Details
#call ⇒ Object
Convert Markdown to HTML using the best available implementation and convert into a DocumentFragment.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/html/pipeline/markdown_filter.rb', line 27 def call extensions = context.fetch( :commonmarker_extensions, DEFAULT_COMMONMARKER_EXTENSIONS ) html = if (renderer = context[:commonmarker_renderer]) unless renderer < CommonMarker::HtmlRenderer raise ArgumentError, "`commonmark_renderer` must be derived from `CommonMarker::HtmlRenderer`" end = :DEFAULT = [:UNSAFE] if context[:unsafe] = [:GITHUB_PRE_LANG] << :HARDBREAKS if context[:gfm] != false = [:UNSAFE] if context[:unsafe] doc = CommonMarker.render_doc(@text, , extensions) renderer.new(options: , extensions: extensions).render(doc) else = [:GITHUB_PRE_LANG] << :HARDBREAKS if context[:gfm] != false << :UNSAFE if context[:unsafe] CommonMarker.render_html(@text, , extensions) end html.rstrip! html end |