Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored event description to display clickable emails, urls, phone numbers #294

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 20 additions & 31 deletions PC2/Views/Calendar/_IndexPartial.cshtml
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
@{
@using System.Text.RegularExpressions

@{
// recieve the event description
string eventDescription = ViewData["eventDescription"].ToString();

@*loop through EventDescription string to search for links*@
@for (int i = 0; i < eventDescription.Length; i++)
{
// look for the first instance of "http"
if (eventDescription.IndexOf("http") == i)
{
// find the next space after the link if it exists
int linkEnd = eventDescription.IndexOf(" ", i);
if (linkEnd == -1)
{
// if not the link is the last word in the string
linkEnd = eventDescription.Length;
}
// create a substring of the link
string link = eventDescription.Substring(i, linkEnd - i);
//remove commas or periods from the end of the link
if (link.EndsWith(",") || link.EndsWith("."))
{
link = link.Substring(0, link.Length - 1);
linkEnd -= 2;
}
<a href="@link" target="_blank">@link</a>
// skip to the end of the link
i += linkEnd - i;
}
else
{
@eventDescription[i]
}
}
// Use a regular expression to find any email addresses in the event description and replace them with a mailto link
string emailPattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b";
string emailReplacement = "<a href=\"mailto:$0\">$0</a>";
string eventDescriptionResult = Regex.Replace(eventDescription, emailPattern, emailReplacement);

// Use a regular expression to find any web addresses that being with https or http in the event description and replace them with a hyperlink
string webPattern = @"\bhttps?://\S+";
string webReplacement = "<a href=\"$0\">$0</a>";
eventDescriptionResult = Regex.Replace(eventDescriptionResult, webPattern, webReplacement);

// Use a regular expression to find any phone numbers and replace them with a tel link
string phonePattern = @"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b";
string phoneReplacement = "<a href=\"tel:$0\">$0</a>";
eventDescriptionResult = Regex.Replace(eventDescriptionResult, phonePattern, phoneReplacement);

// Display the event description with mailto links
@Html.Raw(eventDescriptionResult)
}
Loading