Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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"
puts toc + "\n" + newmd
Copy link

JenniferMack commented Apr 1, 2015

Usage: cat | md-toc.rb >

Copy link

Akela2606 commented Apr 8, 2019

thanks for sharing this script.
Sorry to bother you here but I don't know how else I could contact you on this site.
Do you know how I can use it with the latest version of Ulysses?
I cannot find any .md file in my Ulysses folder...
I am going crazy here, trying to get a table of contents for a poetry book with about 120 poems... :-(
Thanks a lot for your help.
Kindest regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment