File tree Expand file tree Collapse file tree 3 files changed +63
-1
lines changed
DasBlog.Tests/UnitTests/UI
DasBlog.Web.Core/Extensions Expand file tree Collapse file tree 3 files changed +63
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -91,6 +91,27 @@ public static string FindFirstImage(this string blogcontent)
91
91
return firstimage . Trim ( ) ;
92
92
}
93
93
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
+
94
115
public static string FindFirstYouTubeVideo ( this string blogcontent )
95
116
{
96
117
var firstVideo = string . Empty ;
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public ProfilePost(IDasBlogSettings dasBlogSettings)
33
33
. ForMember ( dest => dest . IsPublic , opt => opt . MapFrom ( src => src . IsPublic ) )
34
34
. ForMember ( dest => dest . Syndicated , opt => opt . MapFrom ( src => src . Syndicated ) )
35
35
. 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 ( ) ) )
37
37
. ForMember ( dest => dest . VideoUrl , opt => opt . MapFrom ( src => src . Content . FindFirstYouTubeVideo ( ) ) )
38
38
. ForMember ( dest => dest . CreatedDateTime , opt => opt . MapFrom ( src => _dasBlogSettings . GetDisplayTime ( src . CreatedUtc ) ) )
39
39
. ForMember ( dest => dest . ModifiedDateTime , opt => opt . MapFrom ( src => _dasBlogSettings . GetDisplayTime ( src . ModifiedUtc ) ) ) ;
You can’t perform that action at this time.
0 commit comments