class HTMLPipeline::NodeFilter::TableOfContentsFilter

Examples

TocPipeline =
  HTMLPipeline.new [
    HTMLPipeline::TableOfContentsFilter
  ]
# => #<HTMLPipeline:0x007fc13c4528d8...>
orig = %(<h1>Ice cube</h1><p>is not for the pop chart</p>)
# => "<h1>Ice cube</h1><p>is not for the pop chart</p>"
result = {}
# => {}
TocPipeline.call(orig, {}, result)
# => {:toc=> ...}
result[:toc]
# => "{:href=>"#ice-cube", :text=>"Ice cube"}"
result[:output].to_s
# => "<h1>\n<a id=\"ice-cube\" class=\"anchor\" href=\"#ice-cube\">..."

Constants

SELECTOR

Public Instance Methods

after_initialize() click to toggle source
# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 46
def after_initialize
  result[:toc] = []
end
anchor_html() click to toggle source

The icon that will be placed next to an anchored rendered markdown header

# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 37
def anchor_html
  @context[:anchor_html] || %(<span aria-hidden="true" class="anchor"></span>)
end
classes() click to toggle source

The class that will be attached on the anchored rendered markdown header

# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 42
def classes
  context[:classes] || "anchor"
end
handle_element(element) click to toggle source
# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 50
def handle_element(element)
  header_href = element["href"]

  return unless header_href.start_with?("#")

  header_id = header_href[1..-1]

  element["id"] = header_id
  element["class"] = classes

  element.set_inner_content(anchor_html, as: :html)

  result[:toc] << { href: header_href }
end
handle_text_chunk(text) click to toggle source
# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 65
def handle_text_chunk(text)
  result[:toc].last[:text] = text.to_s
end
selector() click to toggle source
# File lib/html_pipeline/node_filter/table_of_contents_filter.rb, line 32
def selector
  SELECTOR
end