Class: Middleman::HashiCorp::RedcarpetHTML
- Inherits:
-
Renderers::MiddlemanRedcarpetHTML
- Object
- Renderers::MiddlemanRedcarpetHTML
- Middleman::HashiCorp::RedcarpetHTML
- Defined in:
- lib/middleman-hashicorp/redcarpet.rb
Overview
Our custom Markdown parser - extends middleman’s customer parser so we pick up all the magic.
Constant Summary collapse
- REDCARPET_OPTIONS =
Custom RedCarpet options.
{ autolink: true, fenced_code_blocks: true, tables: true, no_intra_emphasis: true, with_toc_data: true, xhtml: true, strikethrough: true, superscript: true, }.freeze
Instance Method Summary collapse
-
#block_html(raw) ⇒ Object
Override block_html to support parsing nested markdown blocks.
-
#header(title, level) ⇒ Object
Override headers to add custom links.
-
#initialize(options = {}) ⇒ RedcarpetHTML
constructor
A new instance of RedcarpetHTML.
-
#list_item(text, list_type) ⇒ Object
Override list_item to automatically add links for documentation.
-
#paragraph(text) ⇒ String
Override paragraph to support custom alerts.
Constructor Details
#initialize(options = {}) ⇒ RedcarpetHTML
Returns a new instance of RedcarpetHTML.
21 22 23 |
# File 'lib/middleman-hashicorp/redcarpet.rb', line 21 def initialize( = {}) super(.merge(REDCARPET_OPTIONS)) end |
Instance Method Details
#block_html(raw) ⇒ Object
Override block_html to support parsing nested markdown blocks.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/middleman-hashicorp/redcarpet.rb', line 67 def block_html(raw) raw = unindent(raw) if md = raw.match(/\<(.+?)\>(.*)\<(\/.+?)\>/m) open_tag, content, close_tag = md.captures rendered_content = recursive_render(content) # If the recursive render did more than simply wrap # the content within a paragraph, assume this is valid # markdown content and return the result if rendered_content != paragraph(content) return "<#{open_tag}>\n#{rendered_content}<#{close_tag}>" end end raw end |
#header(title, level) ⇒ Object
Override headers to add custom links.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/middleman-hashicorp/redcarpet.rb', line 28 def header(title, level) anchor = anchor_link(title) return <<-EOH.gsub(/^ {6}/, "") <h#{level} id="#{anchor}"> <a name="#{anchor}" class="anchor" href="##{anchor}">»</a> #{title} </h#{level}> EOH end |
#list_item(text, list_type) ⇒ Object
Override list_item to automatically add links for documentation
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/middleman-hashicorp/redcarpet.rb', line 45 def list_item(text, list_type) @anchors ||= {} md = text.match(/\A(?:<p>)?(<code>(.+?)<\/code>)/) linked = !text.match(/\A(<p>)?<a(.+?)>(.+?)<\/a>\s*?[-:]?/).nil? if !md.nil? && !linked container, name = md.captures anchor = anchor_link(name) replace = %|<a name="#{anchor}" /><a href="##{anchor}">#{container}</a>| text.sub!(container, replace) end "<li>#{text}</li>\n" end |
#paragraph(text) ⇒ String
Override paragraph to support custom alerts.
89 90 91 |
# File 'lib/middleman-hashicorp/redcarpet.rb', line 89 def paragraph(text) add_alerts("<p>#{text.strip}</p>\n") end |