Skip to content

Enhanced Post.FirstImgSrc method to really find the first image #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]