Last active
July 18, 2024 12:00
-
-
Save JenniferMack/2f3edf7df90740f345e5 to your computer and use it in GitHub Desktop.
Create a table of contents for Ulysses markdown
This file contains 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
#!/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
Usage: cat myfile.md | md-toc.rb > newfile.md