Setting Up MongoDB with Authentication MongoDB Update Nightmare

Markdown Table of Contents Generator

07 May 2014

I've been writing everything in Markdown for quite a while (including this very post), and I'm a real fan. I like that I can edit from my phone, that i can open from the terminal, or from a text editor like sublime text.

Currently working through some documentation and using Marked, and feeling some pain around generating/updating a linked table of contents. Every doc needs them, but it's a challenge to maintain in markdown.

So, I made one.

1/2 way through realized I had set Automator to be ruby not python, which explained some syntax issues I was having.

toc = ''

ARGF.each do |f|
    if f =~ /(#+)(.*?)(\[.*\]|$)/
        indents = (($1.length) -1)*4
        toc+= (' '*indents)+"* ["+$2+"]"+($3?$3:'')+"\n"

IO.popen('pbcopy', 'w') { |f| f << toc }


  1. open Automator
  2. create a new workflow named MDTOC
  3. add the 'get clipboard contents' component
  4. add the 'run shell script' component
  5. paste in that code
  6. save

My task looks like this:


then, when you are in your markdown doc that looks like this

# great title [link]

content content content

## subtitle [deep link]

content content content

# second title no link
content content content

copy the whole doc, then run the MDTOC service, and the TOC will be pasted into your clipboard, this sample generated this

* [ great title ][link]
    * [ subtitle ][deep link]
* [ second title no lin]
* [ great title ][link]
    * [ subtitle ][deep link]
* [ second title no link]

Which, in Marked, looks quite nice