Docs: Give headers ids for easy linking
This gives markdown headers an id so that we can link directly to sections of our docs. This is better than the alternative of adding them all ourselves.
Paul O’Shannessy committed
Sep 18, 2013 at 17:16 UTC
d27746ee0b34ef23df483ff63abc8f89eb664f41
3 files changed
+26
docs/Gemfile
+3
@@ -16,3 +16,6 @@ gem 'rb-fsevent'
16
17
# Redcarpet for Markdown
18
gem 'redcarpet'
19
+
20
+# For markdown header cleanup
21
+gem 'sanitize'
docs/Gemfile.lock
+6
@@ -24,6 +24,9 @@ GEM
24
liquid (2.5.0)
25
maruku (0.6.1)
26
syntax (>= 1.0.0)
27
+ mini_portile (0.5.1)
28
+ nokogiri (1.6.0)
29
+ mini_portile (~> 0.5.0)
30
posix-spawn (0.3.6)
31
pygments.rb (0.5.0)
32
posix-spawn (~> 0.3.6)
@@ -32,6 +35,8 @@ GEM
35
rb-fsevent (0.9.3)
36
redcarpet (2.2.2)
37
safe_yaml (0.7.1)
38
+ sanitize (2.0.6)
39
+ nokogiri (>= 1.4.4)
40
sass (3.2.9)
41
syntax (1.0.0)
42
yajl-ruby (1.1.0)
@@ -45,4 +50,5 @@ DEPENDENCIES
50
rake
51
rb-fsevent
52
redcarpet
53
+ sanitize
54
sass
docs/_plugins/header_links.rb
new
+17
@@ -0,0 +1,17 @@
1
+require 'redcarpet'
2
+require 'sanitize'
3
+
4
+# Simple converter that is probably better than RedCarpet's built in TOC id
5
+# generator (which ends up with things lik id="toc_1"... terrible).
6
+
7
+class Redcarpet::Render::HTML
8
+ def header(title, level)
9
+ clean_title = Sanitize.clean(title)
10
+ .downcase
11
+ .gsub(/\s+/, "-")
12
+ .gsub(/[^A-Za-z0-9\-_.]/, "")
13
+
14
+ return "<h#{level} id=\"#{clean_title}\">#{title}</h#{level}>"
15
+ end
16
+end
17
+