Skip to content

Commit

Permalink
Add code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
softlion committed Jan 18, 2024
1 parent ee1e30b commit 3faf094
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions NotionSharp.ApiClient/Lib/HtmlRendering/HtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,6 +143,23 @@ protected virtual void TransformCallout(Block block, StringBuilder sb)
Append(callout.RichText, sb);
sb.AppendLine("</div>");
}

protected virtual void TransformCode(Block block, StringBuilder sb)
{
var code = block.Code!;

sb.Append("""<div class="notion-code-block" />""");

sb.Append("""<div class="notion-code-caption" />""");
Append(code.Caption, sb);
sb.Append("""</div>""");

sb.Append($"""<div class="notion-code language-{code.Language}" />""");
Append(code.RichText, sb);
sb.AppendLine("</div>");

sb.AppendLine("</div>");
}

/// <summary>
/// Manage a column list
Expand Down
7 changes: 6 additions & 1 deletion NotionSharp.ApiClient/Lib/PublicApi/Model/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
{
Expand All @@ -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 =
Expand Down Expand Up @@ -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; }

Expand All @@ -146,6 +148,9 @@ public record BlockTextAndChildrenAndColor(List<Block> Children, List<RichText>
{
public NotionColor Color { get; init; }
}
public record BlockCode(string Language, List<RichText> Caption, List<RichText> RichText) : BlockText(RichText);


public record BlockChildPage(string Title);

public record BlockCallout(List<RichText> RichText, NotionColor Color, NotionIcon Icon)
Expand Down
4 changes: 2 additions & 2 deletions NotionSharp.ApiClient/NotionSharp.ApiClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<PropertyGroup>
<!-- nuget configurable properties -->
<Version>2.0.0</Version>
<VersionSuffix>-pre9</VersionSuffix>
<Version>2.0.1</Version>
<VersionSuffix>-pre1</VersionSuffix>
<DefineConstants>$(DefineConstants);</DefineConstants>
</PropertyGroup>

Expand Down

0 comments on commit 3faf094

Please sign in to comment.