Skip to content

Instantly share code, notes, and snippets.

@shitana
Forked from albertodebortoli/generate_toc.rb
Created February 17, 2019 20:24
Show Gist options
  • Save shitana/50ea3f800588a314b1b150fb6c8ddc10 to your computer and use it in GitHub Desktop.
Save shitana/50ea3f800588a314b1b150fb6c8ddc10 to your computer and use it in GitHub Desktop.
Generate Markdown TOC
#!/usr/bin/env ruby
File.open("your_file.md", 'r') do |f|
f.each_line do |line|
forbidden_words = ['Table of contents', 'define', 'pragma']
next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ }
title = line.gsub("#", "").strip
href = title.gsub(" ", "-").downcase
puts " " * (line.count("#")-1) + "* [#{title}](\##{href})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment