Skip to content

Instantly share code, notes, and snippets.

@rudrankriyam
Forked from JenniferMack/md-toc.rb
Created July 18, 2024 12:00
Show Gist options
  • Save rudrankriyam/bfc3d3c7b4aa6278c6a1cee7e3f640b8 to your computer and use it in GitHub Desktop.
Save rudrankriyam/bfc3d3c7b4aa6278c6a1cee7e3f640b8 to your computer and use it in GitHub Desktop.
Create a table of contents for Ulysses markdown
#!/usr/bin/env ruby
toc = "# Table of Contents\n"
newmd = ""
ARGF.each_line do |line|
newmd << line
next if !line.start_with?("#")
heading = line.gsub("#", "").strip
href = heading.gsub(" ", "-").downcase
newmd << "<a name=\"#{href}\"></a>\n"
toc << "\t" * (line.count("#")-1) + "- [#{heading}](\##{href})\n"
end
puts toc + "\n" + newmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment