tips and tricks for movable type
comments: allow HTML and autolink URLs
September 28, 2002
In Blog Configuration | Preferences, you have two options: Allow HTML in comments and Auto-link URLs - but you can't have them both. The first option overrides the second.
In this support forum thread, SunMark wanted the best of both worlds. My first suggestion was to add the Comments Formatting Buttons - but on further reflection, I realized this only works if the commenter actually bothers to use the buttons - and do those buttons function anywhere but in IE? I'm not sure if they do or not.
I ran across another thread, where Brad Choate posted a way to use his Regex plugin to auto-link URLs in entries.
Ahhh! That will work for comments as well!
Put this code in your Comments Listing Template and your Individual Archive Template (if you use inline comments there) - I put mine right before the <MTComments> tag:
Then, write your comment body tag like this:
This allows you to check Allow HTML in comments, and still have URLs auto-linked if the commenter doesn't write the link with the a href tags.
(In case it isn't obvious, please note that you must leave Auto-link URLs UNchecked in order for this to work!)
NOTE: In testing, I did discover one flaw with this, which happens when the URL to be autolinked is on a line by itself (or, presumably, at the very end of a line of text), and you have Convert Breaks turned on. Because MT adds a <br> or <p> tag to the end of the text, the actual URL ends up looking like this:
which, of course, causes a 404 error when clicking on the link.
I've posted this problem in the thread where I found the original code, and will let you know if a solution is found!
Update 10.01.02: Brad gave me the change to the code, and it's been modified above. Works like a charm!
Update 12.19.02 Well, strangely, my autolink code stopped working. I went back to the original thread, where Brad posted a slightly different code change than the one he first gave me. That latest code didn't work for me at the time, but now suddenly, it does. Hmmmm. Modified above. Sigh.
Update 12.20.02 Okay, this one's on hold for now. I removed the autolink regex statement from my comment listings until my shameless begging for Brad's assistance bails me out. The issue is this: if someone actually codes a URL in the comments using the <a href> tag, the autolink regex gets applied to it and the URL ends up in shambles. So I guess I need an if/else regex, or another method of making this work. (Or maybe I just need to understand how to write those damned expressions myself, huh?)
Update 01.29.03 Yay! Thanks to Phillip for his solution in this thread! (Line breaks added for display purposes are indicated by »; these breaks should be removed when using the code!)
<MTRegexDefine name="autolink2">s!<(b[^>]*|i[^>]*|u[^>]*|br[^>]*|p[^>]*|pre[^>]*»
|tt[^>]*|strong[^>]*|em[^>]*|ol[^>]*|ul[^>]*|li[^>]*|blockquote[^>]*»
|div[^>]*|span[^>]*)>(http://[^\s<'"]+)!<$1><a href="$2">$2</a>!g</MTRegexDefine>
<MTCommentBody sanitize_html="a,a href,b,i,u,br,p,pre,tt,strong,em,ol,ul,li,blockquote,div class,span class" regex="autolink1 autolink2">
Update 04.04.05 Now that the Sanitize plugin has been integrated into Movable Type, there's another small change to how you apply this to the comment body:
Alternatively, you can eliminate the sanitize attribute entirely, and just define the allowed tags in either mt.cfg (GlobalSanitizeSpec) or in Weblog Configuration > Preferences : Sanitize Spec.
I also now use the Validable plugin, so:
TrackBack: 1
(URL: http://www.thegirliematters.com/sf/mt-track.cgi/94)
» Ordering
Excerpt: A very helpful tip here allowed me to reorder the entries in this diary. As requested, they're now sorted descending overall with entries ascending within a given day. Hope this...
Weblog: Phil's Diary
Tracked: 10.25.04 02:33 AM
Comments
I'm glad you're back!
(but I just forgot the number URL... :-) )
Lawrence
Wow, I missed that before...
Lawrence
We are testing the autolink feature here.
First a handcoded URL: The Girlie Matters
And one we want to autolink: http://www.thegirliematters.com
Another test of the new autolink
http://www.thegirliematters.com
I added one more definition to catch URLs that come after MT's auto-generated p tags. Go see the thread for an update.
I think it's all straight now!!
Sorry for making you a guinea pig girlie. The final, true and working code is fixed on the thread.
That was stressful!! =)
Yes it looks stressful but wen you get a chance be sure to change 1.29.02 to 03 to make it less stressful :-p
a common error...
I got confused for a sc didn't know if it was going up or down for the Update !
Test again:
Handcoded: The Girlie Matters
Autolinked: http://www.thegirliematters.com
I am using this code for autolink a given url
function xautolink($0,$1,$2){
s = $0.substring(0,$0.length);
if(s.indexOf("h") != 0)
s = s.replace(s,"http://"+s);
while (s.length>0 && s.charAt(s.length-1)=='.')
s=s.substring(0,s.length-1);
modS = s.replace(s,"<a href="+s+" target=_blank">"+s+"</a>");
return " "+modS;
}
function autolink(s){
var hlink = /(http:\/\/|https:\/\/|www.)([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g;
//var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g;
return (s.replace (hlink, xautolink));
}
function doautolink(){
var bodycontent = document.starttopic.msg.value;
bodycontent = autolink(bodycontent);
document.starttopic.HtmlMessage.value = bodycontent;
}
it works fine in PC but In mac it spits the code itself. Any one can help me to fix it?