Skip to content

Commit

Permalink
Fix permalinks for all markdown sources
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Feb 4, 2016
1 parent a865c19 commit fdde8d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'lib/gfm_ids.rb'

activate :bh

set :css_dir, 'stylesheets'
Expand All @@ -18,6 +20,7 @@
set :markdown, { fenced_code_blocks: true, with_toc_data: true }
set :markdown_engine, :redcarpet
activate :rouge_syntax
activate :gfm_ids

page '/documentation/*', :layout => 'documentation'

Expand Down
30 changes: 30 additions & 0 deletions lib/gfm_ids.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'middleman-core'
require 'nokogiri'


# Updates IDs from Markdown headings to be GFM compatible
class GFMIDs < Middleman::Extension
def initialize(app, options={})
super

app.after_render do |content, path, locs, template_class|
if template_class == Middleman::Renderers::RedcarpetTemplate
document = Nokogiri::HTML(content)

document.css('h1, h2, h3, h4, h5, h6').each do |node|
node.attributes['id'].value = node.content
.downcase
.gsub(/[^\p{Alnum} -]/, '') # Remove non-alpha numeric characters
.tr(' ', '-') # Replace spaces with dashes
end

document.to_s
else
nil
end
end
end
end


::Middleman::Extensions.register(:gfm_ids, GFMIDs)

0 comments on commit fdde8d8

Please sign in to comment.