lib/wcc/contentful/app/markdown_renderer.rb in wcc-contentful-app-1.0.7 vs lib/wcc/contentful/app/markdown_renderer.rb in wcc-contentful-app-1.0.8
- old
+ new
@@ -1,35 +1,44 @@
# frozen_string_literal: true
require_relative './custom_markdown_render'
class WCC::Contentful::App::MarkdownRenderer
- def markdown(text)
- raise ArgumentError, 'markdown method requires text' unless text
+ attr_reader :options, :extensions
- markdown_links = links_within_markdown(text)
- links_with_classes, raw_classes = gather_links_with_classes_data(markdown_links)
+ def initialize(options = nil)
+ @extensions = {
+ autolink: true,
+ superscript: true,
+ disable_indented_code_blocks: true,
+ tables: true
+ }.merge!(options&.delete(:extensions) || {})
- options = {
+ @options = {
filter_html: true,
hard_wrap: true,
link_attributes: { target: '_blank' },
space_after_headers: true,
- fenced_code_blocks: true,
- links_with_classes: links_with_classes
- }
+ fenced_code_blocks: true
+ }.merge!(options || {})
+ end
- extensions = {
- autolink: true,
- superscript: true,
- disable_indented_code_blocks: true,
- tables: true
- }
+ def markdown(text)
+ raise ArgumentError, 'markdown method requires text' unless text
+ markdown_links = links_within_markdown(text)
+ links_with_classes, raw_classes = gather_links_with_classes_data(markdown_links)
+
+ options = @options.merge({
+ links_with_classes: links_with_classes
+ })
+
renderer = ::WCC::Contentful::App::CustomMarkdownRender.new(options)
markdown = ::Redcarpet::Markdown.new(renderer, extensions)
markdown.render(remove_markdown_href_class_syntax(raw_classes, text))
end
+
+ alias_method :call, :markdown
private
def remove_markdown_href_class_syntax(raw_classes, text)
text_without_markdown_class_syntax = text.dup