Skip to content

Commit 3faf094

Browse files
committed
Add code blocks
1 parent ee1e30b commit 3faf094

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ protected virtual bool Transform(Block? block, StringBuilder sb, bool stopBefore
8484
case BlockTypes.ColumnList:
8585
TransformColumnList(block, sb);
8686
break;
87+
case BlockTypes.Code:
88+
TransformCode(block, sb);
89+
break;
8790

8891
case BlockTypes.Unsupported:
8992
break;
@@ -140,6 +143,23 @@ protected virtual void TransformCallout(Block block, StringBuilder sb)
140143
Append(callout.RichText, sb);
141144
sb.AppendLine("</div>");
142145
}
146+
147+
protected virtual void TransformCode(Block block, StringBuilder sb)
148+
{
149+
var code = block.Code!;
150+
151+
sb.Append("""<div class="notion-code-block" />""");
152+
153+
sb.Append("""<div class="notion-code-caption" />""");
154+
Append(code.Caption, sb);
155+
sb.Append("""</div>""");
156+
157+
sb.Append($"""<div class="notion-code language-{code.Language}" />""");
158+
Append(code.RichText, sb);
159+
sb.AppendLine("</div>");
160+
161+
sb.AppendLine("</div>");
162+
}
143163

144164
/// <summary>
145165
/// Manage a column list

NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static class BlockTypes
2727
public const string Callout = "callout";
2828
public const string ColumnList = "column_list";
2929
public const string Column = "column";
30+
public const string Code = "code";
3031

3132
public static readonly string[] SupportedBlocks =
3233
{
@@ -36,7 +37,7 @@ public static class BlockTypes
3637
//When the is_toggleable property is true
3738
Heading1, Heading2, Heading3,
3839
NumberedListItem, Paragraph, Image, Quote, File, //SyncedBlock, Table, Template,
39-
ToDo, Toggle
40+
ToDo, Toggle, Code
4041
};
4142

4243
public static readonly string[] BlocksWithChildren =
@@ -125,6 +126,7 @@ public Block()
125126
public BlockTextAndChildren? NumberedListItem { get; init; }
126127
public BlockTextAndChildrenAndCheck? ToDo { get; init; }
127128
public BlockTextAndChildren? Toggle { get; init; }
129+
public BlockCode? Code { get; init; }
128130

129131
public BlockChildPage? ChildPage { get; init; }
130132

@@ -146,6 +148,9 @@ public record BlockTextAndChildrenAndColor(List<Block> Children, List<RichText>
146148
{
147149
public NotionColor Color { get; init; }
148150
}
151+
public record BlockCode(string Language, List<RichText> Caption, List<RichText> RichText) : BlockText(RichText);
152+
153+
149154
public record BlockChildPage(string Title);
150155

151156
public record BlockCallout(List<RichText> RichText, NotionColor Color, NotionIcon Icon)

NotionSharp.ApiClient/NotionSharp.ApiClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<PropertyGroup>
2020
<!-- nuget configurable properties -->
21-
<Version>2.0.0</Version>
22-
<VersionSuffix>-pre9</VersionSuffix>
21+
<Version>2.0.1</Version>
22+
<VersionSuffix>-pre1</VersionSuffix>
2323
<DefineConstants>$(DefineConstants);</DefineConstants>
2424
</PropertyGroup>
2525

0 commit comments

Comments
 (0)