Skip to content

Commit

Permalink
Rewrote logic for meta data
Browse files Browse the repository at this point in the history
By utilizing our new Limbo.MetaData package, we can minimize the amount of code in this package.
  • Loading branch information
abjerner committed May 11, 2022
1 parent f1c3422 commit 55a882a
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 1,038 deletions.
3 changes: 2 additions & 1 deletion src/Limbo.Umbraco.Spa/Factories/ISpaMetaDataFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Limbo.Umbraco.Spa.Models;
using Limbo.Umbraco.Spa.Models.Meta;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Limbo.Umbraco.Spa.Factories {

Expand All @@ -14,7 +15,7 @@ public interface ISpaMetaDataFactory {
/// <param name="content">The content item.</param>
/// <param name="request">The current SPA request.</param>
/// <returns>An instance of <see cref="SpaMetaData"/>.</returns>
public SpaMetaData GetMetaData(SpaContentModel content, SpaRequest request);
SpaMetaData CreateMetaData(IPublishedContent content, SpaRequest request);

}

Expand Down
135 changes: 126 additions & 9 deletions src/Limbo.Umbraco.Spa/Factories/SpaMetaDataFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Limbo.Umbraco.Spa.Models;
using Limbo.MetaData.Models.OpenGraph;
using Limbo.MetaData.Models.Twitter;
using Limbo.Umbraco.Spa.Models;
using Limbo.Umbraco.Spa.Models.Meta;
using Skybrud.Essentials.Umbraco;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;

Expand All @@ -11,18 +12,134 @@ namespace Limbo.Umbraco.Spa.Factories {
/// </summary>
public class SpaMetaDataFactory : ISpaMetaDataFactory {

/// <inheritdoc/>
public SpaMetaData GetMetaData(SpaContentModel content, SpaRequest request) {
/// <summary>
/// Returns the meta data for the specified <paramref name="content"/> item.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="request">The current SPA request.</param>
/// <returns>An instance of <see cref="SpaMetaData"/>.</returns>
public virtual SpaMetaData CreateMetaData(IPublishedContent content, SpaRequest request) {

SpaMetaData meta = new SpaMetaData();
// Initialize a new meta data instance
var meta = new SpaMetaData(request.CultureInfo);

meta.HtmlAttributes.Language = content.GetCultureInfo().ToString();
// Update the various information with values from the virtual methods
meta.Title = GetTitle(meta, content, request);
meta.MetaTitle = GetMetaTitle(meta, content, request);
meta.MetaDescription = GetMetaDescription(meta, content, request);
meta.CanonicalUrl = GetCanonicalUrl(meta, content, request);
meta.Robots = GetRobots(meta, content, request);
meta.OpenGraph = GetOpenGraph(meta, content, request);
meta.TwitterCard = GetTwitterCard(meta, content, request);

meta.DangerouslyDisableSanitizers = new[] {"script"};
// Return the meta data
return meta;

meta.Canonical = content.Url(mode: UrlMode.Absolute);
}

return meta;
/// <summary>
/// Returns the canonical URL for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the canonical URL.</param>
/// <param name="request">The current request.</param>
/// <returns>The canonical URL for <paramref name="content"/>.</returns>
public virtual string GetCanonicalUrl(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {
return content.Url(request.CultureInfo.ToString(), UrlMode.Absolute);
}

/// <summary>
/// Returns the browser title (value for the <c>&lt;title&gt;</c> element) for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the browser title for.</param>
/// <param name="request">The current request.</param>
/// <returns>The browser title.</returns>
public virtual string GetTitle(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {
return $"{GetMetaTitle(metaData, content, request)} - {request.Site.Name}";
}

/// <summary>
/// Returns the page title for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the page title.</param>
/// <param name="request">The current request.</param>
/// <returns>The page title.</returns>
public virtual string GetMetaTitle(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {
return content.Name;
}

/// <summary>
/// Returns the meta description for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the page title.</param>
/// <param name="request">The current request.</param>
/// <returns>The meta description.</returns>
public virtual string GetMetaDescription(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {
return content.Value<string>("teaser");
}

/// <summary>
/// Returns the robots value for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the robots value.</param>
/// <param name="request">The current request.</param>
/// <returns>The robots value.</returns>
public virtual string GetRobots(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {

if (request.Arguments.HostName.Contains("liveserver.nu")) {
return "noindex, nofollow";
}

if (request.Arguments.HostName.Contains("testserver.nu")) {
return "noindex, nofollow";
}

if (request.Arguments.HostName.Contains("azurewebsites.net")) {
return "noindex, nofollow";
}

return content.Value<bool>("hideFromSearch") ? "noindex, follow" : "index, follow";

}

/// <summary>
/// Returns the Open Graph information for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the Open Graph information.</param>
/// <param name="request">The current request.</param>
/// <returns>An instance of <see cref="OpenGraphProperties"/> representing the Open Graph information.</returns>
public virtual OpenGraphProperties GetOpenGraph(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {

OpenGraphProperties og = new() {
Url = content.Url(mode: UrlMode.Absolute),
SiteName = request.Site.Name,
Title = content.Name,
Description = metaData.MetaDescription
};

return og;

}

/// <summary>
/// Returns the Twitter card for <paramref name="content"/>.
/// </summary>
/// <param name="metaData">The meta data instance.</param>
/// <param name="content">The page for which to get the Twitter card.</param>
/// <param name="request">The current request.</param>
/// <returns>An instance of <see cref="ITwitterCard"/> representing the Twitter card.</returns>
public virtual ITwitterCard GetTwitterCard(SpaMetaData metaData, IPublishedContent content, SpaRequest request) {

return new TwitterSummaryCard {
Site = request.Site.Name,
Title = metaData.Title,
Description = metaData.MetaDescription
};

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SpaMetaDataJsonConverter : JsonConverter {

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
if (!(value is SpaMetaData meta)) throw new ArgumentException("Must be an instance of SpaMetaData", nameof(value));
meta.WriteJson(writer);
meta.ToVueMetaJson().WriteTo(writer);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
Expand Down
1 change: 1 addition & 0 deletions src/Limbo.Umbraco.Spa/Limbo.Umbraco.Spa.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<!-- Include NuGet dependencies -->
<ItemGroup>
<PackageReference Include="Limbo.MetaData" Version="1.0.0-alpha001" />
<PackageReference Include="Skybrud.Essentials" Version="1.1.41" />
<PackageReference Include="Skybrud.Essentials.AspNetCore" Version="1.0.0-alpha003" />
<PackageReference Include="Skybrud.Essentials.Umbraco" Version="1.0.0-alpha002" />
Expand Down

This file was deleted.

This file was deleted.

50 changes: 0 additions & 50 deletions src/Limbo.Umbraco.Spa/Models/Meta/OpenGraph/SpaOpenGraphImage.cs

This file was deleted.

Loading

0 comments on commit 55a882a

Please sign in to comment.