Skip to content

Instantly share code, notes, and snippets.

@andershaig
Created September 20, 2011 21:47
Show Gist options
  • Save andershaig/1230507 to your computer and use it in GitHub Desktop.
Save andershaig/1230507 to your computer and use it in GitHub Desktop.
Custom: Entry Titles Into Links
<!--
/ Before
/-----------------------------
/ Note: This only worked because the media_item.title
/ field already had URLs entered (http://) and that's it.
////////////////////////////// -->
<div class="media_title">
{{ media_item.title }}
</div>
<!--
/ After
/-----------------------------
/ #magic_button just got added to the custom design CSS.
////////////////////////////// -->
<div class="media_title">
<script type="text/javascript">
var to_parse = '{{ media_item.title | strip_newlines }}';
// Regex to pick a URL out of the complete text. Works even if the text is just the URL but wouldn't
// work if there were multiple URLs (it would need to be modified).
var URL_RE = /(?:(?=[\s`!()\[\]{};:'".,<>?«»“”‘’])|\b)((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/|[a-z0-9.\-]+[.](?:com|org|net))(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))*(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]|\b))/gi;
var parsed_url = to_parse.match(URL_RE);
document.write('<a id="magic_button" href="' + parsed_url + '" target="_blank">View Blog Post &raquo;</a>');
</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment