Skip to content

Commit 4f915e9

Browse files
greenhost87lord
authored andcommitted
Add optional NestingUniqueHeadCounter header ID generator
* Move nesting generator to separate class * Major bug fix, code simplification * Get rid of global value, use class variable instead.
1 parent b6cf496 commit 4f915e9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/nesting_unique_head.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Nested unique header generation
2+
require 'middleman-core/renderers/redcarpet'
3+
4+
class NestingUniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
5+
def initialize
6+
super
7+
@@headers_history = {} if !defined?(@@headers_history)
8+
end
9+
10+
def header(text, header_level)
11+
friendly_text = text.parameterize
12+
@@headers_history[header_level] = text.parameterize
13+
14+
if header_level > 1
15+
for i in (header_level - 1).downto(1)
16+
friendly_text.prepend("#{@@headers_history[i]}-") if @@headers_history.key?(i)
17+
end
18+
end
19+
20+
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
21+
end
22+
end

lib/unique_head.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ def header(text, header_level)
1414
end
1515
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
1616
end
17-
end
17+
end

0 commit comments

Comments
 (0)