Created
August 2, 2020 14:36
-
-
Save Merovex/59de9bf976dd8e5f18cdb7d682b8b59e to your computer and use it in GitHub Desktop.
Ruby Table of Contents generator for Markdown file in 6 lines
This file contains hidden or 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
puts "\nCreating Table of Contents" | |
# This TOC generator assumes you don't care to include the page header | |
toc = "## Contents\n\n" + content.scan(/^##\s?(.*)\n/iu).flatten.map do |header| | |
next if header == 'Contents' | |
indent = " " * header.count("#") | |
anchor = header.downcase.gsub(/\W+/,'-').chomp('-') | |
"%s* [%s](#%s)\n" % [indent,header,anchor] | |
end.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment