Skip to content

Commit

Permalink
Merge pull request #172 from jmalarcon/master
Browse files Browse the repository at this point in the history
Enhanced Post.FirstImgSrc method to really find the first image
  • Loading branch information
farzindev authored Apr 2, 2021
2 parents 7f695b4 + 739d681 commit 11b9f17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions BlogEngine/BlogEngine.Core/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

using BlogEngine.Core.Data.Models;
Expand Down Expand Up @@ -694,28 +695,27 @@ public bool IsVisibleToPublic
}

/// <summary>
/// URL of the first image in the post, if any
/// URL of the first image in the post, if any.
/// If there's no first image, returns the URL to "images/defaultImg.jpg" in the current theme used in the blog
/// </summary>
public string FirstImgSrc
{
get
{
int idx = Content.IndexOf("<img src=");
if (idx > 0)
string srcValue = null;
if (!string.IsNullOrEmpty(content))
{
try
Match match = Regex.Match(content, @"<img\s+?.*?src=('|"")(.*?)\1.*?>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
if (match.Success)
{
idx = idx + 10;
var idxEnd = Content.IndexOf("\"", idx);
if (idxEnd > idx)
{
var len = idxEnd - idx;
return Content.Substring(idx, len);
}
srcValue = match.Groups[2].Value;
}
catch (Exception) { }
}
return "";
if (string.IsNullOrEmpty(srcValue))
{
srcValue = Utils.RelativeWebRoot + "Custom/Themes/" + BlogSettings.Instance.Theme + "/images/defaultImg.jpg";
}
return srcValue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion BlogEngine/BlogEngine.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
[assembly: ComVisible(false)]
[assembly: AllowPartiallyTrustedCallers]
[assembly: AssemblyVersion("3.3.8.0")]
[assembly: SecurityRules(SecurityRuleSet.Level1)]
[assembly: SecurityRules(SecurityRuleSet.Level1)]

0 comments on commit 11b9f17

Please sign in to comment.