Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Embedding URLs in Blog Comments View modes: 
User avatar
Member
Member
stevenmc - 1/26/2012 8:58:16 AM
   
Embedding URLs in Blog Comments
Hi there,

Is there a way to convert URLs that people input in a blog comment into a clickable link?

Thanks

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/27/2012 7:06:49 AM
   
RE:Embedding URLs in Blog Comments
Hello,

Sure, but you will need to implement a whole logic for this. Let me give you an example:

In the file ~\CMSModules\MessageBoards\Controls\Messages\MessageEdit.ascx.cs is the btnOk_Click() method, in which you can find a // Setup message info part of a code, so here you can replace all url addresses with the links on them. A simple working sample of code could look like this:

// Setup message info
messageInfo.MessageEmail = txtEmail.Text.Trim();
string msgText = txtMessage.Text.Trim();
messageInfo.MessageText = Regex.Replace(msgText, "(http://[^ ]+)", "<a href='$1'>$1</a>");


Best regards,
Jan Hermann

User avatar
Member
Member
stevenmc - 1/27/2012 8:10:34 AM
   
RE:Embedding URLs in Blog Comments
Hello,

Thanks for your reply.
I've pasted in the example code you supplied and it doesn't appear to work. Any thoughts as to why this might be happening?

Thanks.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/27/2012 12:20:21 PM
   
RE:Embedding URLs in Blog Comments
Jan's regex probably isn't working in your case because you may need to add RegexOptions.IgnoreCase parameter to his Regex.Replace(msgText,@"his regex",RegexOptions.IgnoreCase);.

Even though that might make Jan's code work in your case, it is not very good to use because it doesn't take into account any urls that may already exist in the content or querystring parameters or things like that. I have found a bit of code and modified it so that it would both be more accurate and preserve any existing anchor or image tags in the content.

//use it like this
// Setup message info
messageInfo.MessageEmail = txtEmail.Text.Trim();
string msgText = txtMessage.Text.Trim();
messageInfo.MessageText = MakeUrlsActive(msgText);

//you can make this a string extension method by changing the parameter to (this string s) if you'd like.
public static string MakeUrlsActive(string s)
{
//find anchor and image tags already in content and preserve them
Dictionary<string, Match> anchorsAndImages = new Dictionary<string, Match>();
var anchorRegex = new Regex(@"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>|<img.*?src=[""'](?<imgurl>.*?)[^>]?>");
foreach (Match m in anchorRegex.Matches(s))
{
string g = Guid.NewGuid().ToString();
anchorsAndImages.Add(g,m);
s = s.Replace(m.Value, g);
}
//Finds URLs with no protocol
var urlregex = new Regex(@"\b\({0,1}(?<url>(?<!http://)(www|ftp)\.[^ ,""\s<)]*)\b",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
//Finds URLs with a protocol
var httpurlregex = new Regex(@"\b\({0,1}(?<url>[^>](http://www\.|http://|https://|ftp://)[^,""\s<)]*)\b",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
//Finds email addresses
var emailregex = new Regex(@"\b(?<mail>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)\b",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
s = urlregex.Replace(s, " <a href=\"http://${url}\" target=\"_blank\">${url}</a>");
s = httpurlregex.Replace(s, " <a href=\"${url}\" target=\"_blank\">${url}</a>");
s = emailregex.Replace(s, "<a href=\"mailto:${mail}\">${mail}</a>");

//put the anchors back in place
foreach (string key in anchorsAndImages.Keys)
{
s = s.Replace(key, anchorsAndImages[key].Value);
}

return s;
}

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/30/2012 2:05:24 AM
   
RE:Embedding URLs in Blog Comments
Hello,

Yes, the code I have posted is not a complex working solution. It is only a sample of code to point stevenmc to a right direction :)

Best regards,
Jan Hermann

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/30/2012 2:09:08 AM
   
RE:Embedding URLs in Blog Comments
Hello,

Could you please be more specific? What do you see in the message board, when you post a text with an url address? Could you please show me a screenshot of your message board?

Best regards,
Jan Hermann

User avatar
Member
Member
stevenmc - 1/30/2012 3:09:37 AM
   
RE:Embedding URLs in Blog Comments
Hi there,

It's not the message board I'm looking to do this on - it's the blog comments area.

I pasted your code into /CMSModules/MessageBoards/Controls/Messages/MessageEdit.ascx.cs, Jiveabillion. This doesn't seem to work either though. Is this because I'm pasting it into the message board area instead of the Blog area?

FYI: I have pretty much no knowledge of C#, etc.

User avatar
Member
Member
stevenmc - 1/30/2012 3:17:17 AM
   
RE:Embedding URLs in Blog Comments
Apologies, I meant to attach a screenshot of a standard blog comment:

http://i.imgur.com/WfrOF.jpg

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/30/2012 3:59:07 AM
   
RE:Embedding URLs in Blog Comments
Hello,

There are two options, how to add and display comments on your blog, so you probably use the second one -> Comment view web part (you can find it under the Design tab on your document in CMS Desk). So in this case, you will need to edit following two files:

~\CMSModules\Blogs\Controls\BlogCommentEdit.ascx.cs (in the PerformAction method around the line 467):
//bci.CommentText = HTMLHelper.HTMLEncode(txtComments.Text.Trim());
string msgText = txtComments.Text.Trim();
bci.CommentText = Regex.Replace(msgText, "(http://[^ ]+)", "<a href='$1'>$1</a>");


~\CMSModules\Blogs\Controls\BlogCommentDetail.ascx.cs (in the GetHTMLEncodedCommentText method around the line 100):
//string comment = HTMLHelper.HTMLEncodeLineBreaks(bci.CommentText);
string comment = bci.CommentText;


Best regards,
Jan Hermann