Sha256: 7880ab949ac7ff082352e205e3831c40c90abe9861a54ae9f274655bbe4af5d6

Contents?: true

Size: 619 Bytes

Versions: 1

Compression:

Stored size: 619 Bytes

Contents

require 'html/pipeline'

class PageTocFilter < HTML::Pipeline::Filter

  def call
    doc.search('p').each do |node|
      content = node.to_html
      next unless content == '<p>{:toc}</p>'
      html = page_toc_filter(doc)
      node.replace(html)
    end

    doc
  end

  def page_toc_filter(doc)
    toc = ''
    levels = context[:toc_levels] || 'h2'

    doc.search(levels).each do |node|
      text = node.text
      id = node.child['id']
      toc << %(<li><a href="##{id}" id="markdown-toc-#{id}">#{text}</a></li>\n)
    end
    toc = %(<ul id="markdown-toc">\n#{toc}</ul>) unless toc.empty?
    toc
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page-toc-filter-0.0.1 lib/page-toc-filter.rb