From the beginning on this blog, I’m using the long-form of URLs for Amazon referrals. Here’s one for the GoPro Hero3 in HTML:

My brand new <a href="">GoPro Hero3</a><img src="" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> is awesome.

Which renders to:

My brand new GoPro Hero3 is awesome.

Now with a Markdown syntax, the code becomes:

My brand new [GoPro Hero3]( ![]( is awesome.

These links are ugly and requires a second URL for the 1-pixel image tracker.

I just discovered that you can use a shorter version of the link. Which make the markup cleaner:

My brand new [GoPro Hero3]( is awesome.

And here’s how I’ve transformed, in all my Markdown articles, the long Amazon links to their short form.

First, I simply removed all 1-pixel trackers:

$ find ./* -iname "*.md" -exec perl -0777 -i -pe "s/\!\[\]\(http:\/\/www\.assoc-amazon\.com.*?\)//gs" "{}" \;

Then I converted all links in one batch with this magic one-liner:

$ find ./* -iname "*.md" -exec perl -0777 -i -pe "s/\(http:\/\/www\.amazon\.com\/gp\/product\/(.*?)\/.*?\)/\(http:\/\/www\.amazon\.com\/dp\/\1\/\?tag=kevideld-20\)/gs" "{}" \;

You can even go further (thanks Elias for the tip) and use Amazon’s URL shortener to get the following short links:

My brand new [GoPro Hero3]( is awesome.

This is the sed command I used to convert all links from the previous form to this new scheme:

$ find ./* -iname "*.md" -exec perl -0777 -i -pe "s/\(http:\/\/www\.amazon\.com\/dp\/(.*?)\)/\(http:\/\/amzn\.com\/\1\)/gs" "{}" \;

Related content