fix start tag detection

This commit is contained in:
Nicolas Constant 2020-08-01 00:00:27 -04:00
parent d9a1dc84be
commit ce996c20b2
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
2 changed files with 48 additions and 1 deletions

View file

@ -13,7 +13,11 @@ namespace BirdsiteLive.Domain.Tools
public class StatusExtractor : IStatusExtractor
{
private readonly Regex _hastagRegex = new Regex(@"\W(\#[a-zA-Z0-9_ー]+\b)(?!;)");
//private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)");
//private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+)\b(?!;)");
private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_ー]+\b)(?!;)");
//private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)");
//private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+)\b(?!;)");
private readonly InstanceSettings _instanceSettings;
#region Ctor
@ -26,6 +30,7 @@ namespace BirdsiteLive.Domain.Tools
public (string content, Tag[] tags) ExtractTags(string messageContent)
{
var tags = new List<Tag>();
messageContent = $" {messageContent} ";
var hashtagMatch = _hastagRegex.Matches(messageContent);
foreach (var m in hashtagMatch)
@ -62,7 +67,7 @@ namespace BirdsiteLive.Domain.Tools
$@" <span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
}
return (messageContent, tags.ToArray());
return (messageContent.Trim(), tags.ToArray());
}
}
}

View file

@ -43,6 +43,27 @@ namespace BirdsiteLive.Domain.Tests.Tools
#endregion
}
[TestMethod]
public void Extract_SingleHashTag_AtStart_Test()
{
#region Stubs
var message = $"#mytag Bla!";
#endregion
var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
#region Validations
Assert.AreEqual(1, result.tags.Length);
Assert.AreEqual("#mytag", result.tags.First().name);
Assert.AreEqual("Hashtag", result.tags.First().type);
Assert.AreEqual("https://domain.name/tags/mytag", result.tags.First().href);
Assert.IsTrue(result.content.Contains("Bla!"));
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/mytag"" class=""mention hashtag"" rel=""tag"">#<span>mytag</span></a>"));
#endregion
}
[TestMethod]
public void Extract_SingleHashTag_SpecialChar_Test()
{
@ -126,6 +147,27 @@ namespace BirdsiteLive.Domain.Tests.Tools
#endregion
}
[TestMethod]
public void Extract_SingleMentionTag_AtStart_Test()
{
#region Stubs
var message = $"@mynickname Bla!";
#endregion
var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
#region Validations
Assert.AreEqual(1, result.tags.Length);
Assert.AreEqual("@mynickname@domain.name", result.tags.First().name);
Assert.AreEqual("Mention", result.tags.First().type);
Assert.AreEqual("https://domain.name/users/mynickname", result.tags.First().href);
Assert.IsTrue(result.content.Contains("Bla!"));
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
#endregion
}
[TestMethod]
public void Extract_MultiMentionTag_Test()
{