Skip to content

Commit

Permalink
Add missing renderer (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roblinde authored Aug 12, 2024
1 parent 681f214 commit c2ab680
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
<PackageId>contentful.aspnetcore</PackageId>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>8.0.0</VersionPrefix>
<VersionPrefix>8.0.1</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>8.0.0</Version>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1</Version>
<AssemblyVersion>8.0.1.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>8.0.0.0</FileVersion>
<FileVersion>8.0.1.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
Expand All @@ -25,7 +25,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="8.0.0" />
<PackageReference Include="contentful.csharp" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
Expand Down
1 change: 1 addition & 0 deletions Contentful.Core/Configuration/ContentJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
case "hyperlink":
return jObject.ToObject<Hyperlink>(serializer);
case "asset-hyperlink":
return jObject.ToObject<AssetHyperlink>(serializer);
case "embedded-asset-inline":
case "embedded-asset-block":
return jObject.ToObject<AssetStructure>(serializer);
Expand Down
2 changes: 1 addition & 1 deletion Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>contentful.csharp</PackageId>
<AssemblyTitle>contentful.net</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>8.0.0</VersionPrefix>
<VersionPrefix>8.0.1</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand Down
64 changes: 64 additions & 0 deletions Contentful.Core/Models/Authoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public HtmlRenderer(HtmlRendererOptions options)
new ListItemContentRenderer(_contentRendererCollection, options.ListItemOptions),
new QuoteContentRenderer(_contentRendererCollection),
new AssetRenderer(_contentRendererCollection),
new AssetHyperlinkRenderer(_contentRendererCollection),
new NullContentRenderer()
});
}
Expand Down Expand Up @@ -667,6 +668,69 @@ public async Task<string> RenderAsync(IContent content)
}
}

/// <summary>
/// A renderer for an asset hyperlink.
/// </summary>
public class AssetHyperlinkRenderer : IContentRenderer
{
private readonly ContentRendererCollection _rendererCollection;

/// <summary>
/// Initializes a new AssetHyperlinkRenderer.
/// </summary>
/// <param name="rendererCollection">The collection of renderer to use for sub-content.</param>
public AssetHyperlinkRenderer(ContentRendererCollection rendererCollection)
{
_rendererCollection = rendererCollection;
}

/// <summary>
/// The order of this renderer in the collection.
/// </summary>
public int Order { get; set; } = 100;

/// <summary>
/// Whether or not this renderer supports the provided content.
/// </summary>
/// <param name="content">The content to evaluate.</param>
/// <returns>Returns true if the content is an assethyperlink, otherwise false.</returns>
public bool SupportsContent(IContent content)
{
return content is AssetHyperlink;
}

/// <summary>
/// Renders the content asynchronously.
/// </summary>
/// <param name="content">The content to render.</param>
/// <returns>The a tag.</returns>
public async Task<string> RenderAsync(IContent content)
{
var assetStructure = content as AssetHyperlink;
var asset = assetStructure.Data.Target;
var sb = new StringBuilder();

var url = asset.File?.Url;
sb.Append(string.IsNullOrEmpty(url) ? "<a>" : $"<a href=\"{asset.File.Url}\">");

if (assetStructure.Content != null && assetStructure.Content.Any())
{
foreach (var subContent in assetStructure.Content)
{
var renderer = _rendererCollection.GetRendererForContent(subContent);
sb.Append(await renderer.RenderAsync(subContent));
}
}
else
{
sb.Append(asset.Title);
}
sb.Append("</a>");

return sb.ToString();
}
}

/// <summary>
/// A renderer for a hyperlink.
/// </summary>
Expand Down

0 comments on commit c2ab680

Please sign in to comment.