Created
January 28, 2017 03:28
-
-
Save timshadel/5737525ae68dbe7837eb39c462f164be to your computer and use it in GitHub Desktop.
Generate TOC for Github markdown docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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