Skip to content

Instantly share code, notes, and snippets.

@ashrewdmint
Created March 30, 2012 22:34
Show Gist options
  • Save ashrewdmint/2256560 to your computer and use it in GitHub Desktop.
Save ashrewdmint/2256560 to your computer and use it in GitHub Desktop.
module Jekyll
# This is probably bad. Should I be doing this?
class MarkdownConverter < Converter
alias :old_convert :convert
def convert(content)
old_convert(content).gsub(/<h([1-6])>(.+?<\/)/) do |match|
n = $1
text = $2
"<h#{n} id=\"#{slugify(text)}\">#{text}"
end
end
def slugify(text)
text.strip.downcase.
gsub(/[^a-z0-9]+/, '-'). # Replace non-alphanumeric characters with -
gsub(/-$|^-/, '') # Remove trailing and leading -'s
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment