Writing WordPress Posts in Markdown

Pen and Pants is hosted by WordPress, but I write my blog posts in my favorite text editor using Markdown. That way I have all the conveniences those afford and I can archive the posts in plain text on GitHub.

The tricky part is going from the .md files to some text I can paste into the input box in WordPress. I learned today that you can write posts in Markdown, but that still doesn’t work perfectly for me because WordPress treats new lines within blocks as hard breaks. (When writing posts I break all lines before 80 characters for more convenient editing and diffing. Keeping all those breaks literal doesn’t translate well to web pages.)

Today, thanks to Ethan White, I figured out that Pandoc can help. By converting my Markdown to Markdown with the --no-wrap flag Pandoc will output paragraphs on a single line but otherwise give me regular Markdown. The command I use looks like this:

pandoc -f markdown -t markdown --no-wrap blog-post.md

I can take the output of that and past it into WordPress’ text input box (after ticking the box to allow Markdown when writing posts).

Note that if you use fenced codeblocks (as on GitHub) WordPress will convert that into its special source code widget. If instead you want something presented using only <pre><code> tags then use indentation to indicate it is pre-formatted text.

Tips for Mac Users

If you use Homebrew you can install Pandoc via the cask add on:

brew cask install pandoc

To copy the output of pandoc straight to the clipboard you can use the pbcopy command:

pandoc -f markdown -t markdown --no-wrap blog-post.md | pbcopy

3 thoughts on “Writing WordPress Posts in Markdown

    • I’ve thought about it, and if I was to start blogging today I probably would go with a static site generator like Jekyll (there are a lot of options). But WordPress takes care of my domain registration and I haven’t figured out how to take ownership of that so I could move the blog. WordPress also has pretty good themes and I can get comments without ads (as opposed to Disqus).

  1. Thanks, this works beautifully!

    Notice for those who want to keep the code widgets (ugly as they are): recent versions of pandoc will by default turn the language flags into a syntax not currently supported by WP. E.g. using ‘ to represent a backtick, it transforms ”’ python into ”’ {.python}.

    To preserve the language tags in bareword form, one can disable the “fenced_code_attributes” extension in pandoc (notice also `–no-wrap` is deprecated in favor of `–wrap=none`):

    `pandoc -f markdown -t markdown-fenced_code_attributes –wrap=none post.md`

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s