Class: AdminSuite::MarkdownRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_suite/markdown_renderer.rb

Overview

MarkdownRenderer converts markdown text into safe HTML with syntax highlighting.

Uses Redcarpet for markdown parsing, Rouge for syntax highlighting, and extracts a table of contents from headings.

Defined Under Namespace

Classes: HtmlRenderer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown) ⇒ MarkdownRenderer

Returns a new instance of MarkdownRenderer.



15
16
17
# File 'lib/admin_suite/markdown_renderer.rb', line 15

def initialize(markdown)
  @markdown = markdown.to_s
end

Instance Attribute Details

#markdownObject (readonly)

Returns the value of attribute markdown.



13
14
15
# File 'lib/admin_suite/markdown_renderer.rb', line 13

def markdown
  @markdown
end

Class Method Details

.markdown_extensionsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/admin_suite/markdown_renderer.rb', line 49

def self.markdown_extensions
  {
    autolink: true,
    tables: true,
    fenced_code_blocks: true,
    strikethrough: true,
    highlight: true,
    superscript: true,
    underline: true,
    no_intra_emphasis: true,
    space_after_headers: true,
    lax_spacing: true
  }
end

.render(text) ⇒ ActiveSupport::SafeBuffer

Parameters:

  • text (String)

Returns:

  • (ActiveSupport::SafeBuffer)


32
33
34
35
36
# File 'lib/admin_suite/markdown_renderer.rb', line 32

def self.render(text)
  renderer = HtmlRenderer.new
  md = Redcarpet::Markdown.new(renderer, markdown_extensions)
  md.render(text.to_s).html_safe
end

.render_with_toc(text) ⇒ Hash{Symbol=>Object}

Parameters:

  • text (String)

Returns:

  • (Hash{Symbol=>Object})


40
41
42
43
44
45
# File 'lib/admin_suite/markdown_renderer.rb', line 40

def self.render_with_toc(text)
  renderer = HtmlRenderer.new
  md = Redcarpet::Markdown.new(renderer, markdown_extensions)
  html = md.render(text.to_s).html_safe
  { html: html, toc: renderer.toc_items }
end

Instance Method Details

#renderHash{Symbol=>Object}

Returns:

  • (Hash{Symbol=>Object})


20
21
22
23
24
25
26
27
28
# File 'lib/admin_suite/markdown_renderer.rb', line 20

def render
  result = self.class.render_with_toc(markdown)

  {
    html: result[:html],
    toc: result[:toc],
    reading_time_minutes: reading_time_minutes
  }
end