Skip to content

Instantly share code, notes, and snippets.

Embed
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"
end
puts toc + "\n" + newmd
@JenniferMack

This comment has been minimized.

Copy link
Owner Author

commented Apr 1, 2015

Usage: cat myfile.md | md-toc.rb > newfile.md

@Akela2606

This comment has been minimized.

Copy link

commented Apr 8, 2019

Hi,
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,
Akela

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.