commit
e73d76bdd8
4 changed files with 63 additions and 9 deletions
|
@ -4,6 +4,6 @@ namespace BirdsiteLive.Common.Regexes
|
||||||
{
|
{
|
||||||
public class UrlRegexes
|
public class UrlRegexes
|
||||||
{
|
{
|
||||||
public static readonly Regex Url = new Regex(@"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
|
public static readonly Regex Url = new Regex(@"(.?)(((http|ftp|https):\/\/)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -44,11 +44,8 @@ namespace BirdsiteLive.Domain.Tools
|
||||||
var urlMatch = UrlRegexes.Url.Matches(messageContent);
|
var urlMatch = UrlRegexes.Url.Matches(messageContent);
|
||||||
foreach (Match m in urlMatch)
|
foreach (Match m in urlMatch)
|
||||||
{
|
{
|
||||||
var url = m.ToString().Replace("\n", string.Empty).Trim();
|
var url = m.Groups[2].ToString();
|
||||||
|
var protocol = m.Groups[3].ToString();
|
||||||
var protocol = "https://";
|
|
||||||
if (url.StartsWith("http://")) protocol = "http://";
|
|
||||||
else if (url.StartsWith("ftp://")) protocol = "ftp://";
|
|
||||||
|
|
||||||
var truncatedUrl = url.Replace(protocol, string.Empty);
|
var truncatedUrl = url.Replace(protocol, string.Empty);
|
||||||
|
|
||||||
|
@ -68,7 +65,7 @@ namespace BirdsiteLive.Domain.Tools
|
||||||
}
|
}
|
||||||
|
|
||||||
messageContent = Regex.Replace(messageContent, m.ToString(),
|
messageContent = Regex.Replace(messageContent, m.ToString(),
|
||||||
$@" <a href=""{url}"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">{protocol}</span><span class=""ellipsis"">{firstPart}</span><span class=""invisible"">{secondPart}</span></a>");
|
$@"{m.Groups[1]}<a href=""{url}"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">{protocol}</span><span class=""ellipsis"">{firstPart}</span><span class=""invisible"">{secondPart}</span></a>");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract Hashtags
|
// Extract Hashtags
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
|
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
<Version>0.14.0</Version>
|
<Version>0.14.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -136,7 +136,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Extract_MultiUrls__Test()
|
public void Extract_MultiUrls_Test()
|
||||||
{
|
{
|
||||||
#region Stubs
|
#region Stubs
|
||||||
var message = $"https://t.co/L8BpyHgg25 Bla!{Environment.NewLine}https://www.eff.org/deeplinks/2020/07/pact-act-not-solution-problem-harmful-online-content";
|
var message = $"https://t.co/L8BpyHgg25 Bla!{Environment.NewLine}https://www.eff.org/deeplinks/2020/07/pact-act-not-solution-problem-harmful-online-content";
|
||||||
|
@ -160,6 +160,63 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Extract_SmallUrl_Test()
|
||||||
|
{
|
||||||
|
#region Stubs
|
||||||
|
var message = @"🚀 test http://GOV.UK date 🎉 data http://GOV.UK woopsi.";
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Mocks
|
||||||
|
var logger = new Mock<ILogger<StatusExtractor>>();
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
var service = new StatusExtractor(_settings, logger.Object);
|
||||||
|
var result = service.Extract(message);
|
||||||
|
|
||||||
|
#region Validations
|
||||||
|
Assert.AreEqual(@"🚀 test <a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a> date 🎉 data <a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a> woopsi.", result.content);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Extract_SmallUrl_2_Test()
|
||||||
|
{
|
||||||
|
#region Stubs
|
||||||
|
var message = @"🚀http://GOV.UK";
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Mocks
|
||||||
|
var logger = new Mock<ILogger<StatusExtractor>>();
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
var service = new StatusExtractor(_settings, logger.Object);
|
||||||
|
var result = service.Extract(message);
|
||||||
|
|
||||||
|
#region Validations
|
||||||
|
Assert.AreEqual(@"🚀<a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a>", result.content);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Extract_SmallUrl_3_Test()
|
||||||
|
{
|
||||||
|
#region Stubs
|
||||||
|
var message = @"🚀http://GOV.UK.";
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Mocks
|
||||||
|
var logger = new Mock<ILogger<StatusExtractor>>();
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
var service = new StatusExtractor(_settings, logger.Object);
|
||||||
|
var result = service.Extract(message);
|
||||||
|
|
||||||
|
#region Validations
|
||||||
|
Assert.AreEqual(@"🚀<a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a>.", result.content);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Extract_SingleHashTag_Test()
|
public void Extract_SingleHashTag_Test()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue