Sha256: 54b5ee19cd327c93cb17b91f67bb421f9177848d7baeea10605cc383f7545977

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

require "cgi"

class VimwikiTOCFilter < HTML::Pipeline::TableOfContentsFilter
  def call
    result[:toc] = String.new('')

    headers = Hash.new(1)
    doc.css('h1, h2, h3, h4, h5, h6').each do |node|
      text = node.text
      id = ascii_downcase(text)
      id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
      id.tr!(' ', '-') # replace spaces with dash

      uniq = headers[id] > 1 ? "-#{headers[id]}" : ''
      headers[id] += 1
      if header_content = node.children.first
        result[:toc] << %(<li><a href="##{id}#{uniq}">#{CGI.escape_html(text)}</a></li>\n)
        header_content.add_previous_sibling(%(<a id="#{id}#{uniq}" class="anchor" href="##{id}#{uniq}" aria-hidden="true">#{anchor_icon}</a>))
      end
    end
    result[:toc] = %(<ul class="section-nav">\n#{result[:toc]}</ul>) unless result[:toc].empty?
    doc
  end

  if RUBY_VERSION >= '2.4'
    def ascii_downcase(str)
      str.downcase(:ascii)
    end
  else
    def ascii_downcase(str)
      str.downcase
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vimwiki_markdown-0.9.2 lib/vimwiki_markdown/vimwiki_toc_filter.rb
vimwiki_markdown-0.9.1 lib/vimwiki_markdown/vimwiki_toc_filter.rb
vimwiki_markdown-0.9.0 lib/vimwiki_markdown/vimwiki_toc_filter.rb
vimwiki_markdown-0.8.2 lib/vimwiki_markdown/vimwiki_toc_filter.rb