From 6e4335a33e277d9617f01df8c31369d73720ecfd Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ebrahimi Date: Sun, 18 Aug 2024 10:31:07 +0330 Subject: [PATCH] Change default value of EscapeNonAsciiCharacters to false + Code cleanup --- .../JsonLd/InfoTypes/BreadcrumbInfo.cs | 6 +-- .../JsonLd/InfoTypes/JsonLdExtensions.cs | 4 +- src/SeoTags/JsonLd/JsonLd.cs | 17 ++++---- src/SeoTags/MetaLink/Feed.cs | 4 +- src/SeoTags/MetaLink/MetaLink.cs | 42 ++++++++----------- src/SeoTags/OpenGraph/OpenGraph.cs | 22 +++++----- src/SeoTags/TwitterCard/TwitterCard.cs | 6 +-- src/SeoTags/Utilities.cs | 2 +- 8 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/SeoTags/JsonLd/InfoTypes/BreadcrumbInfo.cs b/src/SeoTags/JsonLd/InfoTypes/BreadcrumbInfo.cs index a4f92db..02e9737 100644 --- a/src/SeoTags/JsonLd/InfoTypes/BreadcrumbInfo.cs +++ b/src/SeoTags/JsonLd/InfoTypes/BreadcrumbInfo.cs @@ -53,7 +53,7 @@ public override BreadcrumbList ConvertTo() item.Name.EnsureNotNullOrWhiteSpace(nameof(item.Name)); item.Url.EnsureNotNullOrWhiteSpace(nameof(item.Url)); - var url = item.Url.ToUri(); + var uri = item.Url.ToUri(); return new ListItem { Position = @index + 1, @@ -62,8 +62,8 @@ public override BreadcrumbList ConvertTo() //Name = item.Name, Item = new WebPage { - Id = url.Relative("#webpage"), - Url = url, + Id = uri.Relative("#webpage"), + Url = uri, Name = item.Name, } }; diff --git a/src/SeoTags/JsonLd/InfoTypes/JsonLdExtensions.cs b/src/SeoTags/JsonLd/InfoTypes/JsonLdExtensions.cs index 995de33..69c0b44 100644 --- a/src/SeoTags/JsonLd/InfoTypes/JsonLdExtensions.cs +++ b/src/SeoTags/JsonLd/InfoTypes/JsonLdExtensions.cs @@ -63,8 +63,8 @@ public static AggregateRating ToAggregateRating(this IEnumerable rev var bestRating = bestRatingList[0]; var worstRating = worstRatingList[0]; - var ratingValue = reviews.Average(p => p.RatingValue); - var ratingCount = reviews?.Count(); + var ratingValue = reviews.DefaultIfEmpty().Average(p => p.RatingValue); + var ratingCount = reviews.Count(); if (ratingValue > bestRating) throw new ArgumentException("Rating value can not be grater than best rating value."); diff --git a/src/SeoTags/JsonLd/JsonLd.cs b/src/SeoTags/JsonLd/JsonLd.cs index b2bfed4..b33e6dc 100644 --- a/src/SeoTags/JsonLd/JsonLd.cs +++ b/src/SeoTags/JsonLd/JsonLd.cs @@ -29,10 +29,7 @@ public JsonLd() }; jsonSerializerSettings = new() { - Converters = new List - { - new StringEnumConverter() - }, + Converters = [new StringEnumConverter()], ContractResolver = dateTimeOffsetToIso8601ContractResolver, DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore, @@ -50,12 +47,12 @@ public bool RenderDateAsUTC } /// - /// Gets or sets a value indicating escape non ASCII characters. + /// Gets or sets a value indicating escape non ASCII characters. (Default is ) /// /// /// true if escape non ASCII characters; otherwise, false. /// - public bool EscapeNonAsciiCharacters { get; set; } = true; + public bool EscapeNonAsciiCharacters { get; set; } = false; /// /// Gets or sets a value indicating graph mode enabled. @@ -68,7 +65,7 @@ public bool RenderDateAsUTC /// /// Gets or sets the things. /// - public List Things { get; set; } = new(); + public List Things { get; set; } = []; /// /// Renders the specified builder. @@ -205,13 +202,13 @@ public JsonLd AddReview(ReviewInfo reviewInfo) /// The thing information. public JsonLd Add(IThingInfo thingInfo) { - Things ??= new(); + Things ??= []; Things.Add(thingInfo.ToThing()); return this; } #region ContractResolver and JsonConverter - private class DateTimeOffsetToIso8601ContractResolver : DefaultContractResolver + private sealed class DateTimeOffsetToIso8601ContractResolver : DefaultContractResolver { private readonly DateTimeOffsetToIso8601JsonConverter dateTimeOffsetToIso8601JsonConverter = new(); public bool RenderDateAsUTC @@ -228,7 +225,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ return jsonProperty; } - private class DateTimeOffsetToIso8601JsonConverter : DateTimeToIso8601DateValuesJsonConverter + private sealed class DateTimeOffsetToIso8601JsonConverter : DateTimeToIso8601DateValuesJsonConverter { public bool RenderDateAsUTC { get; set; } diff --git a/src/SeoTags/MetaLink/Feed.cs b/src/SeoTags/MetaLink/Feed.cs index 8d0a34c..c1bcc0e 100644 --- a/src/SeoTags/MetaLink/Feed.cs +++ b/src/SeoTags/MetaLink/Feed.cs @@ -11,7 +11,7 @@ public class Feed /// The title. /// The URL. /// Type of the feed. - public Feed(string title, string url, FeedType feedType) + public Feed(string title, string url, FeedType feedType = FeedType.Rss) { title.EnsureNotNullOrWhiteSpace(nameof(title)); url.EnsureNotNullOrWhiteSpace(nameof(url)); @@ -35,6 +35,6 @@ public Feed(string title, string url, FeedType feedType) /// /// Gets or sets the type of the feed. (default: RSS) /// - public FeedType FeedType { get; set; } = FeedType.Rss; + public FeedType FeedType { get; set; } } } diff --git a/src/SeoTags/MetaLink/MetaLink.cs b/src/SeoTags/MetaLink/MetaLink.cs index 639aed3..613f8c2 100644 --- a/src/SeoTags/MetaLink/MetaLink.cs +++ b/src/SeoTags/MetaLink/MetaLink.cs @@ -34,12 +34,12 @@ public class MetaLink /// /// Gets or sets the DNS prefetch urls. (create both rel='preconnect' and rel='dns-prefetch' link tags) /// - public List DnsPrefetchUrls { get; set; } = new(); + public List DnsPrefetchUrls { get; set; } = []; /// /// Gets or sets the preloads. (create rel='preload' link tag). /// - public List Preloads { get; set; } = new(); + public List Preloads { get; set; } = []; /// /// Gets or sets the site title. (final title generate from SiteTitle and PageTitle by TitleFormat) @@ -64,7 +64,7 @@ public class MetaLink /// /// Gets or sets the keywords. (create name='keywords' meta tag) /// - public List Keywords { get; set; } = new(); + public List Keywords { get; set; } = []; /// /// Gets or sets the canonical URL. (create rel='canonical' link tag) @@ -104,7 +104,7 @@ public class MetaLink /// /// Gets or sets the feeds. (create application/rss+xml and application/atom+xml link tags) /// - public List Feeds { get; set; } = new(); + public List Feeds { get; set; } = []; /// /// Gets or sets the robots meta tag. @@ -298,7 +298,7 @@ public MetaLink SetPagingInfo(string prevUrl, string nextUrl) /// Type of the feed. public MetaLink AddFeed(string title, string url, FeedType feedType) { - Feeds ??= new(); + Feeds ??= []; Feeds.Add(new Feed(title, url, feedType)); return this; } @@ -311,9 +311,8 @@ public MetaLink AddFeed(params Feed[] feeds) { feeds.EnsureNotNullAndNotNullItem(nameof(feeds)); - Feeds ??= new(); - foreach (var feed in feeds) - Feeds.Add(feed); + Feeds ??= []; + Feeds.AddRange(feeds); return this; } @@ -325,9 +324,8 @@ public MetaLink AddFeed(IEnumerable feeds) { feeds.EnsureNotNullAndNotNullItem(nameof(feeds)); - Feeds ??= new(); - foreach (var feed in feeds) - Feeds.Add(feed); + Feeds ??= []; + Feeds.AddRange(feeds); return this; } @@ -339,9 +337,8 @@ public MetaLink AddDnsPrefetch(params string[] dnsPrefetchUrls) { dnsPrefetchUrls.EnsureNotNullAndNotNullItem(nameof(dnsPrefetchUrls)); - DnsPrefetchUrls ??= new(); - foreach (var item in dnsPrefetchUrls) - DnsPrefetchUrls.Add(item); + DnsPrefetchUrls ??= []; + DnsPrefetchUrls.AddRange(dnsPrefetchUrls); return this; } @@ -353,9 +350,8 @@ public MetaLink AddDnsPrefetch(IEnumerable dnsPrefetchUrls) { dnsPrefetchUrls.EnsureNotNullAndNotNullItem(nameof(dnsPrefetchUrls)); - DnsPrefetchUrls ??= new(); - foreach (var item in dnsPrefetchUrls) - DnsPrefetchUrls.Add(item); + DnsPrefetchUrls ??= []; + DnsPrefetchUrls.AddRange(dnsPrefetchUrls); return this; } @@ -367,7 +363,7 @@ public MetaLink AddDnsPrefetch(IEnumerable dnsPrefetchUrls) /// The as attrubite. public MetaLink AddPreload(string url, string mimeType = null, PreloadType? @as = null) { - Preloads ??= new(); + Preloads ??= []; Preloads.Add(new Preload(url, mimeType, @as)); return this; } @@ -380,9 +376,8 @@ public MetaLink AddPreload(params Preload[] preloads) { preloads.EnsureNotNullAndNotNullItem(nameof(preloads)); - Preloads ??= new(); - foreach (var item in preloads) - Preloads.Add(item); + Preloads ??= []; + Preloads.AddRange(preloads); return this; } @@ -394,9 +389,8 @@ public MetaLink AddPreload(IEnumerable preloads) { preloads.EnsureNotNullAndNotNullItem(nameof(preloads)); - Preloads ??= new(); - foreach (var item in preloads) - Preloads.Add(item); + Preloads ??= []; + Preloads.AddRange(preloads); return this; } diff --git a/src/SeoTags/OpenGraph/OpenGraph.cs b/src/SeoTags/OpenGraph/OpenGraph.cs index f69a656..5cf8ad4 100644 --- a/src/SeoTags/OpenGraph/OpenGraph.cs +++ b/src/SeoTags/OpenGraph/OpenGraph.cs @@ -10,7 +10,7 @@ namespace SeoTags /// public class OpenGraph { - #region Properties + #region Properties /// /// Gets or sets the type of card. (og:type) /// @@ -44,7 +44,7 @@ public class OpenGraph /// /// Gets or sets the locale alternatives. (og:locale:alternate) /// - public List LocaleAlternatives { get; set; } = new(); + public List LocaleAlternatives { get; set; } = []; /// /// Gets or sets the image URL. (og:image and og:image:secure_url if url starts with https:) @@ -104,7 +104,7 @@ public class OpenGraph /// /// Gets or sets the article tags. (article:tag) /// - public List ArticleTags { get; set; } = new(); + public List ArticleTags { get; set; } = []; /// /// Gets or sets the URL of iframe player of video. (og:video and og:video:secure_url if url starts with https:) @@ -139,7 +139,7 @@ public class OpenGraph /// /// Gets or sets the video tags. (og:video:tag) /// - public List VideoTags { get; set; } = new(); + public List VideoTags { get; set; } = []; /// /// Gets or sets the audio URL. (og:audio and og:audio:secure_url if url starts with https:) @@ -174,7 +174,7 @@ public class OpenGraph /// /// Gets or sets the book tags. (book:tag) /// - public List BookTags { get; set; } = new(); + public List BookTags { get; set; } = []; /// /// Gets or sets the product price amount. (product:price:amount and og:price:amount) @@ -189,7 +189,7 @@ public class OpenGraph /// /// Gets or sets the see also urls. (og:see_also) /// - public List SeeAlsoUrls { get; set; } = new(); + public List SeeAlsoUrls { get; set; } = []; /// /// Gets or sets a value indicating render date times as UTC. (default: ) @@ -559,9 +559,8 @@ public OpenGraph AddSeeAlsoUrls(params string[] seeAlsoUrls) { seeAlsoUrls.EnsureNotNullAndNotNullItem(nameof(seeAlsoUrls)); - SeeAlsoUrls ??= new(); - foreach (var item in seeAlsoUrls) - SeeAlsoUrls.Add(item); + SeeAlsoUrls ??= []; + SeeAlsoUrls.AddRange(seeAlsoUrls); return this; } @@ -573,9 +572,8 @@ public OpenGraph AddSeeAlsoUrls(IEnumerable seeAlsoUrls) { seeAlsoUrls.EnsureNotNullAndNotNullItem(nameof(seeAlsoUrls)); - SeeAlsoUrls ??= new(); - foreach (var item in seeAlsoUrls) - SeeAlsoUrls.Add(item); + SeeAlsoUrls ??= []; + SeeAlsoUrls.AddRange(seeAlsoUrls); return this; } #endregion diff --git a/src/SeoTags/TwitterCard/TwitterCard.cs b/src/SeoTags/TwitterCard/TwitterCard.cs index b179ec3..186601f 100644 --- a/src/SeoTags/TwitterCard/TwitterCard.cs +++ b/src/SeoTags/TwitterCard/TwitterCard.cs @@ -77,7 +77,7 @@ public class TwitterCard /// <meta name="twitter:label2" content="Color" /> /// <meta name="twitter:data2" content="Black" /> /// - public Dictionary AdditionalInfo { get; set; } = new(); + public Dictionary AdditionalInfo { get; set; } = []; ///// ///// Url of your page. <meta name="twitter:url" content="[for example: https://site.com/page-url]" /gt; @@ -279,7 +279,7 @@ public TwitterCard AddAdditionalInfo(string label, string data) label.EnsureNotNullOrWhiteSpace(nameof(label)); data.EnsureNotNullOrWhiteSpace(nameof(data)); - AdditionalInfo ??= new(); + AdditionalInfo ??= []; AdditionalInfo[label] = data; return this; } @@ -292,7 +292,7 @@ public TwitterCard AddAdditionalInfo(Dictionary additionalInfo) { additionalInfo.EnsureNotNull(nameof(additionalInfo)); - AdditionalInfo ??= new(); + AdditionalInfo ??= []; foreach (var item in additionalInfo) { item.Value.EnsureNotNullOrWhiteSpace(nameof(item.Value)); diff --git a/src/SeoTags/Utilities.cs b/src/SeoTags/Utilities.cs index be697e2..373a0f8 100644 --- a/src/SeoTags/Utilities.cs +++ b/src/SeoTags/Utilities.cs @@ -157,7 +157,7 @@ public static Uri Relative(this Uri uri, string relative) //https://github.com/samuelneff/MimeTypeMap #endregion - private static readonly IContentTypeProvider contentTypeProvider = new FileExtensionContentTypeProvider(); + private static readonly FileExtensionContentTypeProvider contentTypeProvider = new(); /// /// Given a file path, determine the MIME type.