Skip to content

Commit

Permalink
Change default value of EscapeNonAsciiCharacters to false + Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mjebrahimi committed Aug 18, 2024
1 parent 31613e4 commit 6e4335a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/SeoTags/JsonLd/InfoTypes/BreadcrumbInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/SeoTags/JsonLd/InfoTypes/JsonLdExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static AggregateRating ToAggregateRating(this IEnumerable<ReviewInfo> 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.");
Expand Down
17 changes: 7 additions & 10 deletions src/SeoTags/JsonLd/JsonLd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public JsonLd()
};
jsonSerializerSettings = new()
{
Converters = new List<JsonConverter>
{
new StringEnumConverter()
},
Converters = [new StringEnumConverter()],
ContractResolver = dateTimeOffsetToIso8601ContractResolver,
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Expand All @@ -50,12 +47,12 @@ public bool RenderDateAsUTC
}

/// <summary>
/// Gets or sets a value indicating escape non ASCII characters.
/// Gets or sets a value indicating escape non ASCII characters. (Default is <see langword="false"/>)
/// </summary>
/// <value>
/// <c>true</c> if escape non ASCII characters; otherwise, <c>false</c>.
/// </value>
public bool EscapeNonAsciiCharacters { get; set; } = true;
public bool EscapeNonAsciiCharacters { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating graph mode enabled.
Expand All @@ -68,7 +65,7 @@ public bool RenderDateAsUTC
/// <summary>
/// Gets or sets the things.
/// </summary>
public List<Thing> Things { get; set; } = new();
public List<Thing> Things { get; set; } = [];

/// <summary>
/// Renders the specified builder.
Expand Down Expand Up @@ -205,13 +202,13 @@ public JsonLd AddReview(ReviewInfo reviewInfo)
/// <param name="thingInfo">The thing information.</param>
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
Expand All @@ -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; }

Expand Down
4 changes: 2 additions & 2 deletions src/SeoTags/MetaLink/Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Feed
/// <param name="title">The title.</param>
/// <param name="url">The URL.</param>
/// <param name="feedType">Type of the feed.</param>
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));
Expand All @@ -35,6 +35,6 @@ public Feed(string title, string url, FeedType feedType)
/// <summary>
/// Gets or sets the type of the feed. (default: RSS)
/// </summary>
public FeedType FeedType { get; set; } = FeedType.Rss;
public FeedType FeedType { get; set; }
}
}
42 changes: 18 additions & 24 deletions src/SeoTags/MetaLink/MetaLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public class MetaLink
/// <summary>
/// Gets or sets the DNS prefetch urls. (create both rel='preconnect' and rel='dns-prefetch' link tags)
/// </summary>
public List<string> DnsPrefetchUrls { get; set; } = new();
public List<string> DnsPrefetchUrls { get; set; } = [];

/// <summary>
/// Gets or sets the preloads. (create rel='preload' link tag).
/// </summary>
public List<Preload> Preloads { get; set; } = new();
public List<Preload> Preloads { get; set; } = [];

/// <summary>
/// Gets or sets the site title. (final title generate from SiteTitle and PageTitle by TitleFormat)
Expand All @@ -64,7 +64,7 @@ public class MetaLink
/// <summary>
/// Gets or sets the keywords. (create name='keywords' meta tag)
/// </summary>
public List<string> Keywords { get; set; } = new();
public List<string> Keywords { get; set; } = [];

/// <summary>
/// Gets or sets the canonical URL. (create rel='canonical' link tag)
Expand Down Expand Up @@ -104,7 +104,7 @@ public class MetaLink
/// <summary>
/// Gets or sets the feeds. (create application/rss+xml and application/atom+xml link tags)
/// </summary>
public List<Feed> Feeds { get; set; } = new();
public List<Feed> Feeds { get; set; } = [];

/// <summary>
/// Gets or sets the robots meta tag.
Expand Down Expand Up @@ -298,7 +298,7 @@ public MetaLink SetPagingInfo(string prevUrl, string nextUrl)
/// <param name="feedType">Type of the feed.</param>
public MetaLink AddFeed(string title, string url, FeedType feedType)
{
Feeds ??= new();
Feeds ??= [];
Feeds.Add(new Feed(title, url, feedType));
return this;
}
Expand All @@ -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;
}

Expand All @@ -325,9 +324,8 @@ public MetaLink AddFeed(IEnumerable<Feed> feeds)
{
feeds.EnsureNotNullAndNotNullItem(nameof(feeds));

Feeds ??= new();
foreach (var feed in feeds)
Feeds.Add(feed);
Feeds ??= [];
Feeds.AddRange(feeds);
return this;
}

Expand All @@ -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;
}

Expand All @@ -353,9 +350,8 @@ public MetaLink AddDnsPrefetch(IEnumerable<string> dnsPrefetchUrls)
{
dnsPrefetchUrls.EnsureNotNullAndNotNullItem(nameof(dnsPrefetchUrls));

DnsPrefetchUrls ??= new();
foreach (var item in dnsPrefetchUrls)
DnsPrefetchUrls.Add(item);
DnsPrefetchUrls ??= [];
DnsPrefetchUrls.AddRange(dnsPrefetchUrls);
return this;
}

Expand All @@ -367,7 +363,7 @@ public MetaLink AddDnsPrefetch(IEnumerable<string> dnsPrefetchUrls)
/// <param name="as">The as attrubite.</param>
public MetaLink AddPreload(string url, string mimeType = null, PreloadType? @as = null)
{
Preloads ??= new();
Preloads ??= [];
Preloads.Add(new Preload(url, mimeType, @as));
return this;
}
Expand All @@ -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;
}

Expand All @@ -394,9 +389,8 @@ public MetaLink AddPreload(IEnumerable<Preload> preloads)
{
preloads.EnsureNotNullAndNotNullItem(nameof(preloads));

Preloads ??= new();
foreach (var item in preloads)
Preloads.Add(item);
Preloads ??= [];
Preloads.AddRange(preloads);
return this;
}

Expand Down
22 changes: 10 additions & 12 deletions src/SeoTags/OpenGraph/OpenGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SeoTags
/// </summary>
public class OpenGraph
{
#region Properties
#region Properties
/// <summary>
/// Gets or sets the type of card. (og:type)
/// </summary>
Expand Down Expand Up @@ -44,7 +44,7 @@ public class OpenGraph
/// <summary>
/// Gets or sets the locale alternatives. (og:locale:alternate)
/// </summary>
public List<string> LocaleAlternatives { get; set; } = new();
public List<string> LocaleAlternatives { get; set; } = [];

/// <summary>
/// Gets or sets the image URL. (og:image and og:image:secure_url if url starts with https:)
Expand Down Expand Up @@ -104,7 +104,7 @@ public class OpenGraph
/// <summary>
/// Gets or sets the article tags. (article:tag)
/// </summary>
public List<string> ArticleTags { get; set; } = new();
public List<string> ArticleTags { get; set; } = [];

/// <summary>
/// Gets or sets the URL of iframe player of video. (og:video and og:video:secure_url if url starts with https:)
Expand Down Expand Up @@ -139,7 +139,7 @@ public class OpenGraph
/// <summary>
/// Gets or sets the video tags. (og:video:tag)
/// </summary>
public List<string> VideoTags { get; set; } = new();
public List<string> VideoTags { get; set; } = [];

/// <summary>
/// Gets or sets the audio URL. (og:audio and og:audio:secure_url if url starts with https:)
Expand Down Expand Up @@ -174,7 +174,7 @@ public class OpenGraph
/// <summary>
/// Gets or sets the book tags. (book:tag)
/// </summary>
public List<string> BookTags { get; set; } = new();
public List<string> BookTags { get; set; } = [];

/// <summary>
/// Gets or sets the product price amount. (product:price:amount and og:price:amount)
Expand All @@ -189,7 +189,7 @@ public class OpenGraph
/// <summary>
/// Gets or sets the see also urls. (og:see_also)
/// </summary>
public List<string> SeeAlsoUrls { get; set; } = new();
public List<string> SeeAlsoUrls { get; set; } = [];

/// <summary>
/// Gets or sets a value indicating render date times as UTC. (default: <see langword="true"/>)
Expand Down Expand Up @@ -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;
}

Expand All @@ -573,9 +572,8 @@ public OpenGraph AddSeeAlsoUrls(IEnumerable<string> seeAlsoUrls)
{
seeAlsoUrls.EnsureNotNullAndNotNullItem(nameof(seeAlsoUrls));

SeeAlsoUrls ??= new();
foreach (var item in seeAlsoUrls)
SeeAlsoUrls.Add(item);
SeeAlsoUrls ??= [];
SeeAlsoUrls.AddRange(seeAlsoUrls);
return this;
}
#endregion
Expand Down
6 changes: 3 additions & 3 deletions src/SeoTags/TwitterCard/TwitterCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class TwitterCard
/// &lt;meta name="twitter:label2" content="Color" /&gt;
/// &lt;meta name="twitter:data2" content="Black" /&gt;
/// </summary>
public Dictionary<string, string> AdditionalInfo { get; set; } = new();
public Dictionary<string, string> AdditionalInfo { get; set; } = [];

///// <summary>
///// Url of your page. &lt;meta name="twitter:url" content="[for example: https://site.com/page-url]" /gt;
Expand Down Expand Up @@ -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;
}
Expand All @@ -292,7 +292,7 @@ public TwitterCard AddAdditionalInfo(Dictionary<string, string> additionalInfo)
{
additionalInfo.EnsureNotNull(nameof(additionalInfo));

AdditionalInfo ??= new();
AdditionalInfo ??= [];
foreach (var item in additionalInfo)
{
item.Value.EnsureNotNullOrWhiteSpace(nameof(item.Value));
Expand Down
2 changes: 1 addition & 1 deletion src/SeoTags/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
/// Given a file path, determine the MIME type.
Expand Down

0 comments on commit 6e4335a

Please sign in to comment.