Skip to content

Commit 39aaa0f

Browse files
Alyss Nolandlord
authored andcommitted
Adds unique header creation (#743)
1 parent 7b1e718 commit 39aaa0f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

config.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unique header generation
2+
require './lib/unique_head.rb'
3+
14
# Markdown
25
set :markdown_engine, :redcarpet
36
set :markdown,
@@ -7,7 +10,8 @@
710
prettify: true,
811
tables: true,
912
with_toc_data: true,
10-
no_intra_emphasis: true
13+
no_intra_emphasis: true,
14+
renderer: UniqueHeadCounter
1115

1216
# Assets
1317
set :css_dir, 'stylesheets'

lib/unique_head.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Unique header generation
2+
require 'middleman-core/renderers/redcarpet'
3+
class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
4+
@@head_count = {}
5+
def header(text, header_level)
6+
friendly_text = text.parameterize
7+
@@head_count[friendly_text] ||= 0
8+
@@head_count[friendly_text] += 1
9+
if @@head_count[friendly_text] > 1
10+
friendly_text += "-#{@@head_count[friendly_text]}"
11+
end
12+
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
13+
end
14+
end

0 commit comments

Comments
 (0)