From 3faf0949697cecd9c3ccefc603e2cf4d5e1d4649 Mon Sep 17 00:00:00 2001 From: Benjamin Mayrargue Date: Thu, 18 Jan 2024 10:27:54 +0100 Subject: [PATCH] Add code blocks --- .../Lib/HtmlRendering/HtmlRenderer.cs | 20 +++++++++++++++++++ .../Lib/PublicApi/Model/Block.cs | 7 ++++++- .../NotionSharp.ApiClient.csproj | 4 ++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs b/NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs index 629a1dd..25b29bc 100644 --- a/NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs +++ b/NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs @@ -84,6 +84,9 @@ protected virtual bool Transform(Block? block, StringBuilder sb, bool stopBefore case BlockTypes.ColumnList: TransformColumnList(block, sb); break; + case BlockTypes.Code: + TransformCode(block, sb); + break; case BlockTypes.Unsupported: break; @@ -140,6 +143,23 @@ protected virtual void TransformCallout(Block block, StringBuilder sb) Append(callout.RichText, sb); sb.AppendLine(""); } + + protected virtual void TransformCode(Block block, StringBuilder sb) + { + var code = block.Code!; + + sb.Append("""
"""); + + sb.Append("""
"""); + Append(code.Caption, sb); + sb.Append("""
"""); + + sb.Append($"""
"""); + Append(code.RichText, sb); + sb.AppendLine("
"); + + sb.AppendLine("
"); + } /// /// Manage a column list diff --git a/NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs b/NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs index 0487698..8b4278c 100644 --- a/NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs +++ b/NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs @@ -27,6 +27,7 @@ public static class BlockTypes public const string Callout = "callout"; public const string ColumnList = "column_list"; public const string Column = "column"; + public const string Code = "code"; public static readonly string[] SupportedBlocks = { @@ -36,7 +37,7 @@ public static class BlockTypes //When the is_toggleable property is true Heading1, Heading2, Heading3, NumberedListItem, Paragraph, Image, Quote, File, //SyncedBlock, Table, Template, - ToDo, Toggle + ToDo, Toggle, Code }; public static readonly string[] BlocksWithChildren = @@ -125,6 +126,7 @@ public Block() public BlockTextAndChildren? NumberedListItem { get; init; } public BlockTextAndChildrenAndCheck? ToDo { get; init; } public BlockTextAndChildren? Toggle { get; init; } + public BlockCode? Code { get; init; } public BlockChildPage? ChildPage { get; init; } @@ -146,6 +148,9 @@ public record BlockTextAndChildrenAndColor(List Children, List { public NotionColor Color { get; init; } } +public record BlockCode(string Language, List Caption, List RichText) : BlockText(RichText); + + public record BlockChildPage(string Title); public record BlockCallout(List RichText, NotionColor Color, NotionIcon Icon) diff --git a/NotionSharp.ApiClient/NotionSharp.ApiClient.csproj b/NotionSharp.ApiClient/NotionSharp.ApiClient.csproj index 229c91b..b3c9a19 100644 --- a/NotionSharp.ApiClient/NotionSharp.ApiClient.csproj +++ b/NotionSharp.ApiClient/NotionSharp.ApiClient.csproj @@ -18,8 +18,8 @@ - 2.0.0 - -pre9 + 2.0.1 + -pre1 $(DefineConstants);