Skip to content

Commit 2d51f1e

Browse files
feat: support synced blocks
1 parent 088a1a6 commit 2d51f1e

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The following [Notion API block object types](https://developers.notion.com/refe
4646
| Divider | ✅ Yes | |
4747
| Table Of Contents | ❌ not planned | static site generators have their own ToC implementations |
4848
| Breadcrumb | ❌ not planned | static site generators have their own nav implementations |
49+
| Synced Block | ✅ Yes | renders all children blocks |
4950

5051
Support for other block types can be considered once they are available on the official Notion API.
5152

src/BlockRenderer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class BlockRenderer {
2424
block: Block,
2525
assets: AssetWriter,
2626
context: RenderingLoggingContext
27-
): Promise<BlockRenderResult> {
27+
): Promise<BlockRenderResult | null> {
2828
const renderMarkdown = async (text: RichText[]) => {
2929
return await this.richText.renderMarkdown(text, context);
3030
};
@@ -98,6 +98,10 @@ export class BlockRenderer {
9898
const msg = `<!-- included database ${block.id} -->\n`;
9999
const db = await this.deferredRenderer.renderChildDatabase(block.id);
100100
return { lines: msg + db.markdown };
101+
case "synced_block":
102+
// nothing to render, only the contents of the synced block are relevant
103+
// however, these are children nöpcl, and thus retrieved by recursion in RecusivveBodyRenderer
104+
return null;
101105
case "toggle":
102106
case "child_page":
103107
case "embed":

src/Blocks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export interface ChildDatabaseBlock extends BlockBase {
3535
type: "child_database";
3636
}
3737

38+
export interface SyncedBlock extends BlockBase {
39+
type: "synced_block";
40+
}
41+
3842
// these are blocks that the notion API client code does not have proper typings for
3943
// for unknown reasons they removed types alltogether in v0.4 of the client
4044
// https://github.com/makenotion/notion-sdk-js/pulls?q=is%3Apr+is%3Aclosed#issuecomment-927781781
@@ -44,7 +48,8 @@ export type Block =
4448
| QuoteBlock
4549
| CalloutBlock
4650
| DividerBlock
47-
| ChildDatabaseBlock;
51+
| ChildDatabaseBlock
52+
| SyncedBlock;
4853

4954
export {
5055
Emoji,

src/RecursiveBodyRenderer.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export class RecursiveBodyRenderer {
1111
constructor(
1212
readonly publicApi: Client,
1313
readonly blockRenderer: BlockRenderer
14-
) { }
14+
) {}
1515

1616
async renderBody(
1717
page: Page,
1818
assets: AssetWriter,
19-
context: RenderingLoggingContext,
19+
context: RenderingLoggingContext
2020
): Promise<string> {
2121
debug("begin rendering body of page " + page.id, page.properties);
2222

@@ -42,24 +42,28 @@ export class RecursiveBodyRenderer {
4242
assets: AssetWriter,
4343
context: RenderingLoggingContext
4444
): Promise<string> {
45-
const parentBlock = await this.blockRenderer.renderBlock(block, assets, context);
46-
const parentLines = this.indent(parentBlock.lines, indent);
45+
const parentBlock = await this.blockRenderer.renderBlock(
46+
block,
47+
assets,
48+
context
49+
);
50+
const parentLines = parentBlock && this.indent(parentBlock.lines, indent);
4751

4852
// due to the way the Notion API is built, we need to recurisvely retrieve child
4953
// blocks, see https://developers.notion.com/reference/retrieve-a-block
5054
// "If a block contains the key has_children: true, use the Retrieve block children endpoint to get the list of children"
5155
const children = block.has_children
5256
? (await this.publicApi.blocks.children.list({ block_id: block.id }))
53-
.results
57+
.results
5458
: [];
5559

56-
const childIndent = indent + " ".repeat(parentBlock.childIndent || 0);
60+
const childIndent = indent + " ".repeat(parentBlock?.childIndent || 0);
5761
const renderChilds = children.map(
5862
async (x) => await this.renderBlock(x, childIndent, assets, context)
5963
);
6064
const childLines = await Promise.all(renderChilds);
6165

62-
return [parentLines, ...childLines].join("\n\n");
66+
return [parentLines, ...childLines].filter((x) => !!x).join("\n\n");
6367
}
6468

6569
private indent(content: string, indent: string) {

0 commit comments

Comments
 (0)