CustomTag: ShortenLogUrls helps save your blog layout

We noticed recently that if someone posted a comment on our blog with a really long URL it wouldn’t wrap and as a result it would throw off the whole template. To address this we wrote a custom tag that uses regular expressions to search for long URLs in the comment area and cut off the text portion.

To get an idea of the problem, take a look at this page, the original page before we implemented URL shortening.

The custom tag, cf_ShortenLongURLs, searches for simple URL links in the contained content like those auto-produced by MovableType in entry comments. It then replaces the displahyed portion with a chopped off version and three dots. For good measure, I also included the ability to add an attribute to the links which I use to force target=”_blank”.

\1",
textToFix,
startPos,
true)>

" & shortUrl & "">

It’s a pretty straightforward tag. It has two optional attributes for the cutoff length and new link attributes. Implemented inside MovableType, it looks like this:

<MTComments>
   <div class="comments-body">
      <cf_ShortenLongUrls>
         <$MTCommentBody$>
      </cf_ShortenLongUrls>
      <span class="comments-post">
      Posted by:
      <$MTCommentAuthorLink spam_protect="1"$>
      at <$MTCommentDate$></span>
   </div>
</MTComments>

After applying, the pages with long links are displayed nicely, as you can see here.

4 thoughts on “CustomTag: ShortenLogUrls helps save your blog layout

  1. Sure, you can use it when displaying data retrieved through RSS. That’s a good idea since you won’t know if the content has URL’s that will throw your design out of wack. Just make sure you use it when displaying the RSS content to an end user and not when generating RSS for automatic consumption–the raw RSS source should be original URL without the shortened version.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>