Sha256: 5fd1dcd289ef80bea6dedf602517f5a578954be2ba51d0eeedb531023ac8c11a

Contents?: true

Size: 704 Bytes

Versions: 5

Compression:

Stored size: 704 Bytes

Contents

require 'nokogiri'

def toc_data(page_content)
  html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content)

  # get a flat list of headers
  headers = []
  html_doc.css('h1, h2, h3').each do |header|
    headers.push({
      id: header.attribute('id').to_s,
      content: header.children,
      level: header.name[1].to_i,
      children: []
    })
  end

  [3,2].each do |header_level|
    header_to_nest = nil
    headers = headers.reject do |header|
      if header[:level] == header_level
        header_to_nest[:children].push header if header_to_nest
        true
      else
        header_to_nest = header if header[:level] < header_level
        false
      end
    end
  end
  headers
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
arctic-vendor-1.0.1 documentation/lib/toc_data.rb
arctic-vendor-1.0.0 documentation/lib/toc_data.rb
arctic-vendor-0.2.4 documentation/lib/toc_data.rb
arctic-vendor-0.2.3 documentation/lib/toc_data.rb
arctic-ui-0.1.0 documentation/lib/toc_data.rb