Skip to content

Commit

Permalink
Adds unique header creation (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyss Noland authored and lord committed Apr 6, 2017
1 parent 7b1e718 commit 39aaa0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unique header generation
require './lib/unique_head.rb'

# Markdown
set :markdown_engine, :redcarpet
set :markdown,
Expand All @@ -7,7 +10,8 @@
prettify: true,
tables: true,
with_toc_data: true,
no_intra_emphasis: true
no_intra_emphasis: true,
renderer: UniqueHeadCounter

# Assets
set :css_dir, 'stylesheets'
Expand Down
14 changes: 14 additions & 0 deletions lib/unique_head.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 39aaa0f

Please sign in to comment.