Sha256: 8c3d35d82068cb7840918d47b83d228372ad9bceeeb40918ab5b1d21946a2689

Contents?: true

Size: 867 Bytes

Versions: 5

Compression:

Stored size: 867 Bytes

Contents

# Unique header generation
require 'middleman-core/renderers/redcarpet'
require 'digest'
class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
  def initialize
    super
    @head_count = {}
  end
  def header(text, header_level)
    friendly_text = text.gsub(/<[^<]+>/,"").parameterize
    if friendly_text.strip.length == 0
      # Looks like parameterize removed the whole thing! It removes many unicode
      # characters like Chinese and Russian. To get a unique URL, let's just
      # URI escape the whole header
      friendly_text = Digest::SHA1.hexdigest(text)[0,10]
    end
    @head_count[friendly_text] ||= 0
    @head_count[friendly_text] += 1
    if @head_count[friendly_text] > 1
      friendly_text += "-#{@head_count[friendly_text]}"
    end
    return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

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