Skip to content

Instantly share code, notes, and snippets.

@timshadel
Created January 28, 2017 03:28
Show Gist options
  • Save timshadel/5737525ae68dbe7837eb39c462f164be to your computer and use it in GitHub Desktop.
Save timshadel/5737525ae68dbe7837eb39c462f164be to your computer and use it in GitHub Desktop.
Generate TOC for Github markdown docs
#!/usr/bin/env ruby
# Generate a correct table of contents for Github markdown documents
# See https://gist.github.com/asabaylus/3071099#gistcomment-1593627
doc = IO.readlines(ARGV[0])
lines = doc.select { |l| l.start_with? "#" }.map do |l|
indent = l.chomp.gsub(/^# .*/, '* ').gsub(/^## .*/, ' * ').gsub(/^### .*/, ' * ')
link = l.gsub(/[^a-zA-Z\- ]/u,'').strip().downcase.gsub(' ','-')
indent + l.chomp.gsub(/^([\# ]*)(.*)$/, '[\2]') + "(##{link})"
end
puts lines.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment