Skip to content

Commit bdbe49e

Browse files
committed
Enhanced Post.FirstImgSrc method to really find the first image in the content using regular expressions.
The previous code didn't find images with styls or other attributes and that was a problem. Bumped minor version to reflect change.
1 parent 5a099c3 commit bdbe49e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

BlogEngine/BlogEngine.Core/Post.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Net.Mail;
1010
using System.Text;
11+
using System.Text.RegularExpressions;
1112
using System.Web;
1213

1314
using BlogEngine.Core.Data.Models;
@@ -694,28 +695,27 @@ public bool IsVisibleToPublic
694695
}
695696

696697
/// <summary>
697-
/// URL of the first image in the post, if any
698+
/// URL of the first image in the post, if any.
699+
/// If there's no first image, returns the URL to "images/defaultImg.jpg" in the current theme used in the blog
698700
/// </summary>
699701
public string FirstImgSrc
700702
{
701703
get
702704
{
703-
int idx = Content.IndexOf("<img src=");
704-
if (idx > 0)
705+
string srcValue = null;
706+
if (!string.IsNullOrEmpty(content))
705707
{
706-
try
708+
Match match = Regex.Match(content, @"<img\s+?.*?src=('|"")(.*?)\1.*?>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
709+
if (match.Success)
707710
{
708-
idx = idx + 10;
709-
var idxEnd = Content.IndexOf("\"", idx);
710-
if (idxEnd > idx)
711-
{
712-
var len = idxEnd - idx;
713-
return Content.Substring(idx, len);
714-
}
711+
srcValue = match.Groups[2].Value;
715712
}
716-
catch (Exception) { }
717713
}
718-
return "";
714+
if (string.IsNullOrEmpty(srcValue))
715+
{
716+
srcValue = Utils.RelativeWebRoot + "Custom/Themes/" + BlogSettings.Instance.Theme + "/images/defaultImg.jpg";
717+
}
718+
return srcValue;
719719
}
720720
}
721721

BlogEngine/BlogEngine.Core/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
[assembly: AssemblyConfiguration("")]
1414
[assembly: AssemblyCompany("")]
1515
[assembly: AssemblyProduct("BlogEngine.NET")]
16-
[assembly: AssemblyCopyright("Copyright @ 2007-2016")]
16+
[assembly: AssemblyCopyright("Copyright @ 2007-2017")]
1717
[assembly: AssemblyTrademark("")]
1818
[assembly: AssemblyCulture("")]
1919
[assembly: CLSCompliant(false)]
2020
[assembly: ComVisible(false)]
2121
[assembly: AllowPartiallyTrustedCallers]
22-
[assembly: AssemblyVersion("3.3.5.0")]
22+
[assembly: AssemblyVersion("3.3.6.0")]
2323
[assembly: SecurityRules(SecurityRuleSet.Level1)]

0 commit comments

Comments
 (0)