Skip to content

Commit d27746e

Browse files
committed
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.
1 parent d13ce70 commit d27746e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

docs/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ gem 'rb-fsevent'
1616

1717
# Redcarpet for Markdown
1818
gem 'redcarpet'
19+
20+
# For markdown header cleanup
21+
gem 'sanitize'

docs/Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ GEM
2424
liquid (2.5.0)
2525
maruku (0.6.1)
2626
syntax (>= 1.0.0)
27+
mini_portile (0.5.1)
28+
nokogiri (1.6.0)
29+
mini_portile (~> 0.5.0)
2730
posix-spawn (0.3.6)
2831
pygments.rb (0.5.0)
2932
posix-spawn (~> 0.3.6)
@@ -32,6 +35,8 @@ GEM
3235
rb-fsevent (0.9.3)
3336
redcarpet (2.2.2)
3437
safe_yaml (0.7.1)
38+
sanitize (2.0.6)
39+
nokogiri (>= 1.4.4)
3540
sass (3.2.9)
3641
syntax (1.0.0)
3742
yajl-ruby (1.1.0)
@@ -45,4 +50,5 @@ DEPENDENCIES
4550
rake
4651
rb-fsevent
4752
redcarpet
53+
sanitize
4854
sass

docs/_plugins/header_links.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

0 commit comments

Comments
 (0)