15 Nov 2017, 11:44

How to Tidy Markdown Tables

This post describes an OS X solution to a problem I was having with my Markdown editor. If you’re a Mac user, you might find it helpful.

So, one of the most important features of Markdown is that it’s readable whether viewed as plaintext or rendered. However, tables in Markdown are a little tricky. You can make a table using this Markdown:

| Heading 1 | Heading 2 |
| :-------- | --------: |
| Cell 1    | Cell 2    |

and it renders as:

Heading 1 Heading 2
Cell 1 Cell 2

So far so good. It takes a little effort to include those extra dashes and spaces in order for everything to line up properly, but not too much.

However, let’s say we decide Heading 2 should now be called The Second Heading, and the content of Cell 1 should become “To be or not to be”. Then, unless we adjust our spaces and dashes, our unrendered Markdown looks like this:

| Heading 1 | The Second Heading |
| :-------- | --------: |
| To be or not to be | Cell 2    |

Our Markdown now fails to deliver on one of its most important features: readability in plaintext!

The Markdown editor I use doesn’t provide a way to clean this up, and as I searched around I couldn’t find another explicitly advertising the feature I wanted: ability to select table Markdown text in the editor and apply a transform to tidy it up. I’m sure there’s an editor out there that has the feature, but I was having trouble finding it. Besides, I was otherwise happy with my existing editor and didn’t want to switch on account of a problem that, while annoying, is only occasionally annoying.

Just a little more searching, however, led me to Markdown Service Tools. They are a collection of OS X Services you can install and make available via the context menu in most any GUI app. Just select the text, Ctrl+click, and you’ll have a list of handy Markdown Services at the ready. Since they’re OS X Services, you can also associate them with keyboard shortcuts.

I’m really glad I found this. It means from now on when I need a Markdown table I can type as little as this:

| One Column | Another Column |
| :--- | ---: |
| A whole lot of content | Less content |

but I’m just a select and two clicks from this:

| One Column             | Another Column |  
| :--------------------- | -------------: |  
| A whole lot of content |   Less content |

I like it!