mirror of
https://github.com/danbulant/api_docs
synced 2026-05-19 20:28:52 +00:00
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.
This commit is contained in:
parent
b6cf496445
commit
4f915e9cb2
2 changed files with 23 additions and 1 deletions
22
lib/nesting_unique_head.rb
Normal file
22
lib/nesting_unique_head.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -14,4 +14,4 @@ class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
|
|||
end
|
||||
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue