Skip to content

Commit 3e826a8

Browse files
committed
Changes to look for a hero image first.
Added unit tests.
1 parent 1c688fc commit 3e826a8

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using DasBlog.Core.Extensions;
2+
using Xunit;
3+
4+
namespace DasBlog.Tests.UnitTests.Misc
5+
{
6+
public class StringExtensionTest
7+
{
8+
public StringExtensionTest() { }
9+
10+
[Fact]
11+
[Trait("StringExtension", "UnitTest")]
12+
public void FindHeroImage_ReturnsHeroImage_WhenHeroImageExists()
13+
{
14+
string blogContent = "<img class=\"hero-image\" src=\"image.jpg\">";
15+
string result = blogContent.FindHeroImage();
16+
17+
Assert.Equal("image.jpg", result);
18+
}
19+
20+
[Fact]
21+
[Trait("StringExtension", "UnitTest")]
22+
public void FindHeroImage_ReturnsFirstImage_WhenHeroImageDoesNotExist()
23+
{
24+
string blogContent = "<img src=\"image.jpg\">";
25+
string result = blogContent.FindHeroImage();
26+
27+
Assert.Equal("image.jpg", result);
28+
}
29+
30+
[Fact]
31+
[Trait("StringExtension", "UnitTest")]
32+
public void FindHeroImage_ReturnsNoImage_WhenNoImageExist()
33+
{
34+
string blogContent = "<p>This is some test...</p";
35+
string result = blogContent.FindHeroImage();
36+
37+
Assert.Empty(result);
38+
}
39+
40+
}
41+
}

source/DasBlog.Web.Core/Extensions/StringExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ public static string FindFirstImage(this string blogcontent)
9191
return firstimage.Trim();
9292
}
9393

94+
public static string FindHeroImage(this string blogcontent)
95+
{
96+
var heroImage = string.Empty;
97+
98+
var regex = new Regex("<img[^>]*class=\"hero-image\"[^>]*src=\"([^\"]+)\"[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
99+
100+
var match = regex.Match(blogcontent);
101+
102+
if (match.Success)
103+
{
104+
heroImage = match.Groups[1].Value.Trim();
105+
}
106+
107+
if (string.IsNullOrEmpty(heroImage))
108+
{
109+
heroImage = FindFirstImage(blogcontent);
110+
}
111+
112+
return heroImage;
113+
}
114+
94115
public static string FindFirstYouTubeVideo(this string blogcontent)
95116
{
96117
var firstVideo = string.Empty;

source/DasBlog.Web.UI/Mappers/ProfilePost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ProfilePost(IDasBlogSettings dasBlogSettings)
3333
.ForMember(dest => dest.IsPublic, opt => opt.MapFrom(src => src.IsPublic))
3434
.ForMember(dest => dest.Syndicated, opt => opt.MapFrom(src => src.Syndicated))
3535
.ForMember(dest => dest.PermaLink, opt => opt.MapFrom(src => _dasBlogSettings.GeneratePostUrl(src)))
36-
.ForMember(dest => dest.ImageUrl, opt => opt.MapFrom(src => src.Content.FindFirstImage()))
36+
.ForMember(dest => dest.ImageUrl, opt => opt.MapFrom(src => src.Content.FindHeroImage()))
3737
.ForMember(dest => dest.VideoUrl, opt => opt.MapFrom(src => src.Content.FindFirstYouTubeVideo()))
3838
.ForMember(dest => dest.CreatedDateTime, opt => opt.MapFrom(src => _dasBlogSettings.GetDisplayTime(src.CreatedUtc)))
3939
.ForMember(dest => dest.ModifiedDateTime, opt => opt.MapFrom(src => _dasBlogSettings.GetDisplayTime(src.ModifiedUtc)));

0 commit comments

Comments
 (0)