Class: WCC::Contentful::App::MarkdownRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/app/markdown_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ MarkdownRenderer

Returns a new instance of MarkdownRenderer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wcc/contentful/app/markdown_renderer.rb', line 8

def initialize(options = nil)
  options = options&.dup

  @extensions = {
    autolink: true,
    superscript: true,
    disable_indented_code_blocks: true,
    tables: true
  }.merge!(options&.delete(:extensions) || {})

  @options = {
    filter_html: true,
    hard_wrap: true,
    link_attributes: { target: '_blank' },
    space_after_headers: true,
    fenced_code_blocks: true
  }.merge!(options || {})
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



6
7
8
# File 'lib/wcc/contentful/app/markdown_renderer.rb', line 6

def extensions
  @extensions
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/wcc/contentful/app/markdown_renderer.rb', line 6

def options
  @options
end

Instance Method Details

#markdown(text) ⇒ Object Also known as: call

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wcc/contentful/app/markdown_renderer.rb', line 27

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