Updated MetaTagHelper.cs to add new metadata properties including ArticlePublishedTime, MetaDescription, and others. Refactored JavaScript in Index1.cshtml for improved readability and added functionality for video playback. Updated metadata attributes in Index.cshtml and Index1.cshtml to support new properties, ensuring better SEO and social media sharing capabilities.
147 lines
6.0 KiB
C#
147 lines
6.0 KiB
C#
using Microsoft.AspNetCore.Razor.TagHelpers;
|
|
|
|
namespace CatherineLynwood.TagHelpers
|
|
{
|
|
[HtmlTargetElement("MetaTag")]
|
|
public class MetaTagHelper : TagHelper
|
|
{
|
|
#region Public Properties
|
|
|
|
public DateTime? ArticleModifiedTime { get; set; }
|
|
|
|
public DateTime? ArticlePublishedTime { get; set; }
|
|
|
|
public string MetaAuthor { get; set; }
|
|
|
|
public string MetaDescription { get; set; }
|
|
|
|
public string MetaImage { get; set; }
|
|
|
|
public string MetaImageAlt { get; set; }
|
|
|
|
public string MetaImagePNG { get; set; }
|
|
|
|
public string MetaKeywords { get; set; }
|
|
|
|
public string MetaTitle { get; set; }
|
|
|
|
public string MetaUrl { get; set; }
|
|
|
|
public string OgSiteName { get; set; }
|
|
|
|
public string OgType { get; set; } = "article";
|
|
|
|
public string TwitterCardType { get; set; } = "summary_large_image";
|
|
|
|
public string TwitterCreatorHandle { get; set; }
|
|
|
|
public int? TwitterPlayerHeight { get; set; }
|
|
|
|
public int? TwitterPlayerWidth { get; set; }
|
|
|
|
public string TwitterSiteHandle { get; set; }
|
|
|
|
public string TwitterVideoUrl { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
|
{
|
|
output.TagName = null; // Remove wrapper tag
|
|
var metaTags = new System.Text.StringBuilder();
|
|
|
|
// General meta tags
|
|
if (!string.IsNullOrWhiteSpace(MetaDescription))
|
|
metaTags.AppendLine($"<meta name=\"description\" content=\"{MetaDescription}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaKeywords))
|
|
metaTags.AppendLine($"<meta name=\"keywords\" content=\"{MetaKeywords}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaAuthor))
|
|
metaTags.AppendLine($"<meta name=\"author\" content=\"{MetaAuthor}\">");
|
|
|
|
// Open Graph meta tags
|
|
if (!string.IsNullOrWhiteSpace(MetaTitle))
|
|
metaTags.AppendLine($"<meta property=\"og:title\" content=\"{MetaTitle}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaDescription))
|
|
metaTags.AppendLine($"<meta property=\"og:description\" content=\"{MetaDescription}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(OgType))
|
|
metaTags.AppendLine($"<meta property=\"og:type\" content=\"{OgType}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaUrl))
|
|
metaTags.AppendLine($"<meta property=\"og:url\" content=\"{MetaUrl}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImage))
|
|
metaTags.AppendLine($"<meta property=\"og:image\" content=\"{MetaImage}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImagePNG))
|
|
metaTags.AppendLine($"<meta property=\"og:image\" content=\"{MetaImagePNG}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImageAlt))
|
|
metaTags.AppendLine($"<meta property=\"og:image:alt\" content=\"{MetaImageAlt}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(OgSiteName))
|
|
metaTags.AppendLine($"<meta property=\"og:site_name\" content=\"{OgSiteName}\">");
|
|
|
|
if (ArticlePublishedTime.HasValue)
|
|
metaTags.AppendLine($"<meta property=\"article:published_time\" content=\"{ArticlePublishedTime.Value:yyyy-MM-ddTHH:mm:ssZ}\">");
|
|
|
|
if (ArticleModifiedTime.HasValue)
|
|
metaTags.AppendLine($"<meta property=\"article:modified_time\" content=\"{ArticleModifiedTime.Value:yyyy-MM-ddTHH:mm:ssZ}\">");
|
|
|
|
// Twitter meta tags
|
|
if (!string.IsNullOrWhiteSpace(TwitterVideoUrl))
|
|
{
|
|
// Ensure absolute URL for player
|
|
var playerUrl = TwitterVideoUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)
|
|
? TwitterVideoUrl
|
|
: $"https://www.catherinelynwood.com/{TwitterVideoUrl.TrimStart('/')}";
|
|
|
|
metaTags.AppendLine("<meta name=\"twitter:card\" content=\"player\">");
|
|
metaTags.AppendLine($"<meta name=\"twitter:player\" content=\"{playerUrl}\">");
|
|
|
|
if (TwitterPlayerWidth.HasValue)
|
|
metaTags.AppendLine($"<meta name=\"twitter:player:width\" content=\"{TwitterPlayerWidth}\">");
|
|
|
|
if (TwitterPlayerHeight.HasValue)
|
|
metaTags.AppendLine($"<meta name=\"twitter:player:height\" content=\"{TwitterPlayerHeight}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImage))
|
|
metaTags.AppendLine($"<meta name=\"twitter:image\" content=\"{MetaImage}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImageAlt))
|
|
metaTags.AppendLine($"<meta name=\"twitter:image:alt\" content=\"{MetaImageAlt}\">");
|
|
}
|
|
else
|
|
{
|
|
metaTags.AppendLine($"<meta name=\"twitter:card\" content=\"{TwitterCardType}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaTitle))
|
|
metaTags.AppendLine($"<meta name=\"twitter:title\" content=\"{MetaTitle}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaDescription))
|
|
metaTags.AppendLine($"<meta name=\"twitter:description\" content=\"{MetaDescription}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImage))
|
|
metaTags.AppendLine($"<meta name=\"twitter:image\" content=\"{MetaImage}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(MetaImageAlt))
|
|
metaTags.AppendLine($"<meta name=\"twitter:image:alt\" content=\"{MetaImageAlt}\">");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(TwitterSiteHandle))
|
|
metaTags.AppendLine($"<meta name=\"twitter:site\" content=\"{TwitterSiteHandle}\">");
|
|
|
|
if (!string.IsNullOrWhiteSpace(TwitterCreatorHandle))
|
|
metaTags.AppendLine($"<meta name=\"twitter:creator\" content=\"{TwitterCreatorHandle}\">");
|
|
|
|
output.Content.SetHtmlContent(metaTags.ToString());
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |