Skip to content

Commit

Permalink
Add support for table headers and fix entry tags serialization at cre…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Roblinde committed Oct 6, 2021
1 parent e7feb27 commit fb10c16
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>6.0.14</Version>
<AssemblyVersion>6.0.14.0</AssemblyVersion>
<Version>6.0.15</Version>
<AssemblyVersion>6.0.15.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>6.0.14.0</FileVersion>
<FileVersion>6.0.15.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="6.0.14" />
<PackageReference Include="contentful.csharp" Version="6.0.15" />
<PackageReference Include="gitlink" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions Contentful.Core.Tests/ContentfulClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,8 @@ public async Task TurningRichTextIntoHtmlShouldYieldCorrectResultForTables()
var res = await _client.GetEntries<TableModel>();
var html = await htmlrenderer.ToHtml(res.First().Blab);
//Assert
Assert.Contains("<table><tr><td><p>Cell1</p></td><td><p>Cell2</p></td><td><p>Cell3</p></td></tr><tr><td><p>Cell4</p></td><td><p>Cell5</p></td><td><p>Cell6</p></td></tr></table>", html);
}
Assert.Contains("<table><tr><th><p>Cell1</p></th><th><p>Cell2</p></th><th><p>Cell3</p></th></tr><tr><td><p>Cell4</p></td><td><p>Cell5</p></td><td><p>Cell6</p></td></tr></table>", html);
}

[Fact]
public async Task TurningRichTextContentIntoHtmlShouldYieldCorrectResultWithSelectiveResolving()
Expand Down
30 changes: 27 additions & 3 deletions Contentful.Core.Tests/JsonFiles/EntriesCollectionWithIncludes.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
"locale": "en-US"
},
"metadata": {
"tags": [ "bob" ]
"tags": [
{
"sys": {
"type": "Link",
"linkType": "Tag",
"id": "nyCampaign"
}
}
]
},
"fields": {
"title": "Seven Tips From Ernest Hemingway on How to Write Fiction",
Expand Down Expand Up @@ -87,7 +95,15 @@
"locale": "en-US"
},
"metadata": {
"tags": [ "bob" ]
"tags": [
{
"sys": {
"type": "Link",
"linkType": "Tag",
"id": "nyCampaign"
}
}
]
},
"fields": {
"title": "Down the Rabbit Hole",
Expand Down Expand Up @@ -167,7 +183,15 @@
"locale": "en-US"
},
"metadata": {
"tags": [ "nobby" ]
"tags": [
{
"sys": {
"type": "Link",
"linkType": "Tag",
"id": "nyCampaign"
}
}
]
},
"fields": {
"name": "Mike Springer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"data": {},
"content": [
{
"nodeType": "table-cell",
"nodeType": "table-header-cell",
"data": {},
"content": [
{
Expand All @@ -83,7 +83,7 @@
]
},
{
"nodeType": "table-cell",
"nodeType": "table-header-cell",
"data": {},
"content": [
{
Expand All @@ -101,7 +101,7 @@
]
},
{
"nodeType": "table-cell",
"nodeType": "table-header-cell",
"data": {},
"content": [
{
Expand Down
2 changes: 2 additions & 0 deletions Contentful.Core/Configuration/ContentJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return jObject.ToObject<TableRow>(serializer);
case "table-cell":
return jObject.ToObject<TableCell>(serializer);
case "table-header-cell":
return jObject.ToObject<TableHeader>(serializer);
case "hr":
return jObject.ToObject<HorizontalRuler>();
case "entry-hyperlink":
Expand Down
6 changes: 3 additions & 3 deletions Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>6.0.14.0</AssemblyVersion>
<FileVersion>6.0.14.0</FileVersion>
<Version>6.0.14</Version>
<AssemblyVersion>6.0.15.0</AssemblyVersion>
<FileVersion>6.0.15.0</FileVersion>
<Version>6.0.15</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="gitlink" Version="3.1.0">
Expand Down
4 changes: 2 additions & 2 deletions Contentful.Core/ContentfulManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public async Task<Entry<dynamic>> CreateEntry(Entry<dynamic> entry, string conte
}

var res = await PostAsync($"{_baseUrl}{spaceId ?? _options.SpaceId}/{EnvironmentsBase}entries",
ConvertObjectToJsonStringContent(new { fields = entry.Fields }), cancellationToken, null, contentTypeId).ConfigureAwait(false);
ConvertObjectToJsonStringContent(new { fields = entry.Fields, metadata = entry.Metadata }), cancellationToken, null, contentTypeId).ConfigureAwait(false);

await EnsureSuccessfulResult(res).ConfigureAwait(false);

Expand Down Expand Up @@ -565,7 +565,7 @@ public async Task<Entry<dynamic>> CreateOrUpdateEntry(Entry<dynamic> entry, stri
}

var res = await PutAsync($"{_baseUrl}{spaceId ?? _options.SpaceId}/{EnvironmentsBase}entries/{entry.SystemProperties.Id}",
ConvertObjectToJsonStringContent(new { fields = entry.Fields }), cancellationToken, version, contentTypeId).ConfigureAwait(false);
ConvertObjectToJsonStringContent(new { fields = entry.Fields, metadata = entry.Metadata }), cancellationToken, version, contentTypeId).ConfigureAwait(false);

await EnsureSuccessfulResult(res).ConfigureAwait(false);

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 @@ -59,6 +59,7 @@ public HtmlRenderer(HtmlRendererOptions options)
new TableRenderer(_contentRendererCollection),
new TableRowRenderer(_contentRendererCollection),
new TableCellRenderer(_contentRendererCollection),
new TableHeaderRenderer(_contentRendererCollection),
new ListContentRenderer(_contentRendererCollection),
new ListItemContentRenderer(_contentRendererCollection, options.ListItemOptions),
new QuoteContentRenderer(_contentRendererCollection),
Expand Down Expand Up @@ -354,6 +355,69 @@ public Task<string> RenderAsync(IContent content)
}
}

/// <summary>
/// A renderer for a table header.
/// </summary>
public class TableHeaderRenderer : IContentRenderer
{
private readonly ContentRendererCollection _rendererCollection;

/// <summary>
/// Initializes a new TableHeaderRenderer
/// </summary>
/// <param name="rendererCollection">The collection of renderer to use for sub-content.</param>
public TableHeaderRenderer(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 a table header, otherwise false.</returns>
public bool SupportsContent(IContent content)
{
return content is TableHeader;
}

/// <summary>
/// Renders the content to an html th-tag.
/// </summary>
/// <param name="content">The content to render.</param>
/// <returns>The table header-tag as a string.</returns>
public string Render(IContent content)
{
var tableHeader = content as TableHeader;
var sb = new StringBuilder();
sb.Append("<th>");

foreach (var subContent in tableHeader.Content)
{
var renderer = _rendererCollection.GetRendererForContent(subContent);
sb.Append(renderer.Render(subContent));
}

sb.Append("</th>");
return sb.ToString();
}

/// <summary>
/// Renders the content asynchronously.
/// </summary>
/// <param name="content">The content to render.</param>
/// <returns>The rendered string.</returns>
public Task<string> RenderAsync(IContent content)
{
return Task.FromResult(Render(content));
}
}

/// <summary>
/// A renderer for a table cell.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Contentful.Core/Models/AuthoringModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,27 @@ public class TableRow : IContent
public GenericStructureData Data { get; set; }
}

/// <summary>
/// Represents a header of a table content node.
/// </summary>
public class TableHeader : IContent
{
/// <summary>
/// The type of node.
/// </summary>
public string NodeType { get; set; }

/// <summary>
/// The content of the table row
/// </summary>
public List<IContent> Content { get; set; }

/// <summary>
/// The additional data of the node.
/// </summary>
public GenericStructureData Data { get; set; }
}

/// <summary>
/// Represents a cell of a table row content node.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Contentful.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class Entry<T> : IEntry<T>
[JsonProperty("sys")]
public SystemProperties SystemProperties { get; set; }

/// <summary>
/// The system managed metadata of an entry.
/// </summary>
[JsonProperty("$metadata")]
public ContentfulMetadata Metadata { get; set; }

/// <summary>
/// The fields of the entry deserialized to the type T.
/// </summary>
Expand Down

0 comments on commit fb10c16

Please sign in to comment.