Module: PageHub::Markdown::ToC

Defined in:
lib/pagehub-markdown/processors/toc_generator.rb

Defined Under Namespace

Classes: Heading

Class Method Summary collapse

Class Method Details

.from_markdown(markdown, threshold = 6) ⇒ Object

Builds a tree of headings from a given block of Markdown text, the returned list can be turned into HTML using ToC::to_html()



8
9
10
# File 'lib/pagehub-markdown/processors/toc_generator.rb', line 8

def self.from_markdown(markdown, threshold = 6)
  self.from_content(/(#+)\s([^\n]+)/, lambda { |l, t| return l.length, t }, markdown, threshold)
end

.to_html(toc) ⇒ Object

renders a table of content using nested <ol> list nodes from a given list of Heading objects produced by ToC::from_markdown()



14
15
16
17
18
19
# File 'lib/pagehub-markdown/processors/toc_generator.rb', line 14

def self.to_html(toc)
  html = "<ol>"
  toc.each { |heading| html << heading.to_html }
  html << "</ol>"
  html
end