Skip to content

Commit

Permalink
Add optional NestingUniqueHeadCounter header ID generator
Browse files Browse the repository at this point in the history

* Move nesting generator to separate class

* Major bug fix, code simplification

* Get rid of global value, use class variable instead.
  • Loading branch information
greenhost87 authored and lord committed Nov 9, 2017
1 parent b6cf496 commit 4f915e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/nesting_unique_head.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Nested unique header generation
require 'middleman-core/renderers/redcarpet'

class NestingUniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
def initialize
super
@@headers_history = {} if !defined?(@@headers_history)
end

def header(text, header_level)
friendly_text = text.parameterize
@@headers_history[header_level] = text.parameterize

if header_level > 1
for i in (header_level - 1).downto(1)
friendly_text.prepend("#{@@headers_history[i]}-") if @@headers_history.key?(i)
end
end

return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
end
end
2 changes: 1 addition & 1 deletion lib/unique_head.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def header(text, header_level)
end
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
end
end
end

0 comments on commit 4f915e9

Please sign in to comment.