I needed some relaxing regular expression fun tonight, so I’ve updated a couple of services in the Markdown Service Tools and made some small additions. The main new feature is for handling blockquotes, and there are two versions of the Service. I’m providing some brief explanations after the jump, but feel free to skip to the Markdown Service Tools page and just check the changelog.
The first of the blockquoting tools, “Blockquote Selection,” just adds a “>” and a space before every line in the selection which contains text. It preserves existing indentation and is best for quoting single paragraphs quickly or for large chunks with code blocks and other indented text you want to keep that way.
The second version, “Convert Indents to Quote Levels,” is for turning hierarchically-indented text (using tabs, not spaces) into nested blockquotes in Markdown format. In essence, it swaps tabs for
>’s. You can turn a conversation like this:
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
> Ut enim ad minima veniam, quis nostrum >> exercitationem ullam corporis >>> suscipit laboriosam, nisi ut aliquid ex ea >>>> commodi consequatur? >>> Quis autem vel eum iure reprehenderit qui in ea > voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Which previews/converts as something like:
The other big feature, and probably the one that took me the most time this evening, is a service for converting standalone URLs into hyperlinks. If a URL isn’t part of an HTML or Markdown (reference or inline) link, it gets turned into a Markdown-style self-link, which will preview/convert as a hyperlink with the URL itself as the text. This is done by surrounding the URL with angle brackets (
This service will ignore anything in an href attribute, anything preceded by “](“ (assumed to be an inline link), anything on a line that starts with a reference (
[title]: ...) and any links that are already “self-linked.” It looks for common punctuation marks at the end of a link and removes them from the conversion (comma, period, etc.). It also does its best to recognize when a paired character (parenthesis, brackets, quotes) exists at the beginning and end of the link so they can be separated from the url without brutally removing every occurence from the link (thus breaking any wikipedia link, et al)1.
The Service currently only looks for http, https and ftp links. If you have another use case I should consider, let me know.
Unwrap Paragraphs updated
The Unwrap Paragraphs service I added the other day has had a small dose of “smart” added to it. It won’t mess with your code blocks or other specially-formatted text now, and should do a much better job of concatenating words that were hyphenated when the text was hard-wrapped to begin with. Let me know how it goes, I’m definitely looking to improve this one. All of them, really, so don’t be shy with the feedback.
You can grab the whole pack of Services and read up on the rest of the included tools on the Markdown Service Tools project page. There’s a readme included in the download (and I just updated it and the documentation) with the full contents of that page, so feel free to download directly below and browse that on your own.
This took a lot of bending over backward because I need it to support Ruby 1.8 (default on your OS X machine), and Ruby 1.8 doesn’t support negative lookbehinds in regular expressions. I apologize for the inelegance of the code. When 1.9+ becomes standard on Macs, I might update it to be much prettier.↩