Adds unique header creation (#743)

This commit is contained in:
Alyss Noland 2017-04-06 15:33:46 -07:00 committed by Robert Lord
parent 7b1e718672
commit 39aaa0fca5
2 changed files with 19 additions and 1 deletions

View file

@ -1,3 +1,6 @@
# Unique header generation
require './lib/unique_head.rb'
# Markdown
set :markdown_engine, :redcarpet
set :markdown,
@ -7,7 +10,8 @@ set :markdown,
prettify: true,
tables: true,
with_toc_data: true,
no_intra_emphasis: true
no_intra_emphasis: true,
renderer: UniqueHeadCounter
# Assets
set :css_dir, 'stylesheets'

14
lib/unique_head.rb Normal file
View file

@ -0,0 +1,14 @@
# Unique header generation
require 'middleman-core/renderers/redcarpet'
class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
@@head_count = {}
def header(text, header_level)
friendly_text = text.parameterize
@@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