Links resolving in a blog comment

   —   
This article gives you instructions on how to automatically resolve URL addresses into clickable links inserted by users in a comment of a blog post. There are two methods to achieve that -> pre-processing and post-processing.

Storing comments with resolved links

1. Edit the BlogCommentEdit.ascx.cs file, which is located in the ~\CMSModules\Blogs\Controls\ directory, and look for the following code around line 467:

bci.CommentText = HTMLHelper.HTMLEncode(txtComments.Text.Trim());

2. Replace it with the code below:

string msgText = txtComments.Text.Trim();
bci.CommentText = Regex.Replace(msgText, "(http://[^ ]+)", "<a href='$1'>$1</a>"); //this is a very simple regular expression, which works, but it doesn't cover all possible url variations

3. Apply the same approach for the BlogCommentDetail.ascx.cs file located in the ~\CMSModules\Blogs\Controls\ directory, but now find the following code around line 100:

string comment = HTMLHelper.HTMLEncodeLineBreaks(bci.CommentText);

4. Replace it with the line below:

string comment = bci.CommentText;

Resolving links after retrieving comments from database

1. Edit the BlogCommentDetail.ascx.cs file located in the ~\CMSModules\Blogs\Controls\ directory and comment find the following code around line 100:

string comment = HTMLHelper.HTMLEncodeLineBreaks(bci.CommentText);

2. Replace it with the line below:

string comment = Regex.Replace(bci.CommentText, "(http://[^ ]+)", "<a href='$1'>$1</a>");

 
-jh-


Applies to: Kentico CMS 6.0
Share this article on   LinkedIn