Skip to content

Commit

Permalink
Update meta tag helper to use PrimaryImage as default for OgImage
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Aug 18, 2020
1 parent 77810ca commit 0107140
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
15 changes: 14 additions & 1 deletion core/Piranha.AspNetCore/HtmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,25 @@ public static HtmlString MetaTags(this IApplicationService app, IMeta content, b
if (opengraph)
{
// Generate open graph tags
sb.AppendLine($"<meta name=\"og:type\" value=\"article\">");
if (content is PageBase page && page.IsStartPage)
{
sb.AppendLine($"<meta name=\"og:type\" value=\"website\">");
}
else
{
sb.AppendLine($"<meta name=\"og:type\" value=\"article\">");
}
sb.AppendLine($"<meta name=\"og:title\" value=\"{ OgTitle(content) }\">");
if (content.OgImage != null && content.OgImage.HasValue)
{
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteUrl(content.OgImage) }\">");
}
else if (content is RoutedContentBase contentBase && contentBase.PrimaryImage != null && contentBase.PrimaryImage.HasValue)
{
// If there's no OG image specified but we have a primary image,
// default to the primary image.
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteUrl(contentBase.PrimaryImage) }\">");
}
if (!string.IsNullOrWhiteSpace(OgDescription(content)))
{
sb.AppendLine($"<meta name=\"og:description\" value=\"{ OgDescription(content) }\">");
Expand Down
7 changes: 0 additions & 7 deletions core/Piranha/Models/GenericPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ namespace Piranha.Models
[Serializable]
public class GenericPage<T> : PageBase where T : GenericPage<T>
{
/// <summary>
/// Gets if this is the startpage of the site.
/// </summary>
public bool IsStartPage {
get { return !ParentId.HasValue && SortOrder == 0; }
}

/// <summary>
/// Creates a new page model using the given page type id.
/// </summary>
Expand Down
16 changes: 6 additions & 10 deletions core/Piranha/Models/PageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ public abstract class PageBase : RoutedContentBase, IBlockContent, IMeta, IComme
/// </summary>
public int SortOrder { get; set; }

/// <summary>
/// Gets/sets the optional primary image.
/// </summary>
public ImageField PrimaryImage { get; set; } = new ImageField();

/// <summary>
/// Gets/sets the optional excerpt.
/// </summary>
public string Excerpt { get; set; }

/// <summary>
/// Gets/sets the navigation title.
/// </summary>
Expand Down Expand Up @@ -100,5 +90,11 @@ public abstract class PageBase : RoutedContentBase, IBlockContent, IMeta, IComme
/// Checks if comments are open for this page.
/// </summary>
public bool IsCommentsOpen => EnableComments && Published.HasValue && (CloseCommentsAfterDays == 0 || Published.Value.AddDays(CloseCommentsAfterDays) > DateTime.Now);

/// <summary>
/// Gets if this is the startpage of the site.
/// </summary>
public bool IsStartPage => !ParentId.HasValue && SortOrder == 0;

}
}
10 changes: 0 additions & 10 deletions core/Piranha/Models/PostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ public abstract class PostBase : RoutedContentBase, IBlockContent, IMeta, IComme
[Required]
public Taxonomy Category { get; set; }

/// <summary>
/// Gets/sets the optional primary image.
/// </summary>
public ImageField PrimaryImage { get; set; } = new ImageField();

/// <summary>
/// Gets/sets the optional excerpt.
/// </summary>
public string Excerpt { get; set; }

/// <summary>
/// Gets/sets the optional redirect.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions core/Piranha/Models/RoutedContentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public abstract class RoutedContentBase : ContentBase
/// </summary>
public ImageField OgImage { get; set; } = new ImageField();

/// <summary>
/// Gets/sets the optional primary image.
/// </summary>
public ImageField PrimaryImage { get; set; } = new ImageField();

/// <summary>
/// Gets/sets the optional excerpt.
/// </summary>
public string Excerpt { get; set; }

/// <summary>
/// Gets/sets the optional route used by the middleware.
/// </summary>
Expand Down

0 comments on commit 0107140

Please sign in to comment.