Created
September 20, 2011 21:47
-
-
Save andershaig/1230507 to your computer and use it in GitHub Desktop.
Custom: Entry Titles Into Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
/ 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 »</a>'); | |
</script> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment