From 165a18d713fe11793d9903aa34aa9d9ef09d0826 Mon Sep 17 00:00:00 2001 From: RayJason Date: Wed, 11 Jun 2025 17:17:45 +0800 Subject: [PATCH 1/3] feat: add support for table block rendering in Notion HTML conversion --- plugins/notion/src/blocksToHTML.ts | 22 ++++++++++++++++++++++ plugins/notion/src/notion.ts | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/plugins/notion/src/blocksToHTML.ts b/plugins/notion/src/blocksToHTML.ts index 50d02010e..00282c035 100644 --- a/plugins/notion/src/blocksToHTML.ts +++ b/plugins/notion/src/blocksToHTML.ts @@ -98,6 +98,28 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) { case "code": htmlContent += `
${richTextToHTML(block.code.rich_text)}
` break + case "table": + htmlContent += `` + break + case "table_row": + if (blocks[i - 1]?.type !== "table") { + htmlContent += `` + block.table_row.cells.forEach((cell) => { + htmlContent += `` + }) + htmlContent += `` + } else { + htmlContent += `` + block.table_row.cells.forEach((cell) => { + htmlContent += `` + }) + htmlContent += `` + } + + if (blocks[i + 1]?.type !== "table_row") { + htmlContent += `
${richTextToHTML(cell)}
${richTextToHTML(cell)}
` + } + break case "video": { if (block.video.type !== "external") { break diff --git a/plugins/notion/src/notion.ts b/plugins/notion/src/notion.ts index d9cbf8da3..8dc747796 100644 --- a/plugins/notion/src/notion.ts +++ b/plugins/notion/src/notion.ts @@ -533,6 +533,19 @@ export interface SynchronizeResult extends SyncStatus { status: "success" | "completed_with_errors" } +async function getBlockChildrenIterator(blockId: string) { + assert(notion, "Notion client is not initialized") + const blocksIterator = iteratePaginatedAPI(notion.blocks.children.list, { + block_id: blockId, + }) + const blocks: BlockObjectResponse[] = [] + for await (const block of blocksIterator) { + if (!isFullBlock(block)) continue + blocks.push(block) + } + return blocks +} + async function getPageBlocksAsRichText(pageId: string) { assert(notion, "Notion client is not initialized") @@ -544,6 +557,11 @@ async function getPageBlocksAsRichText(pageId: string) { for await (const block of blocksIterator) { if (!isFullBlock(block)) continue blocks.push(block) + + if (block.type === "table") { + const tableRows = await getBlockChildrenIterator(block.id) + blocks.push(...tableRows) + } } assert(blocks.every(isFullBlock), "Response is not a full block") From 2726073c33d9cdaa1145c4eaca766d7e4eaa2086 Mon Sep 17 00:00:00 2001 From: RayJason Date: Tue, 24 Jun 2025 09:08:30 +0800 Subject: [PATCH 2/3] fix: table layout --- plugins/notion/src/blocksToHTML.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/notion/src/blocksToHTML.ts b/plugins/notion/src/blocksToHTML.ts index 00282c035..c9dc252c4 100644 --- a/plugins/notion/src/blocksToHTML.ts +++ b/plugins/notion/src/blocksToHTML.ts @@ -102,7 +102,7 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) { htmlContent += `` break case "table_row": - if (blocks[i - 1]?.type !== "table") { + if (blocks[i - 1]?.type === "table") { htmlContent += `` block.table_row.cells.forEach((cell) => { htmlContent += `` @@ -138,6 +138,6 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) { break } } - + console.log('blocksToHtml', htmlContent) return htmlContent } From 84c78180b53712e767c58fc88f815467c6eb51be Mon Sep 17 00:00:00 2001 From: Cedric Boirard Date: Wed, 25 Jun 2025 13:05:14 +0200 Subject: [PATCH 3/3] Remove console log --- plugins/notion/src/blocksToHTML.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/notion/src/blocksToHTML.ts b/plugins/notion/src/blocksToHTML.ts index c9dc252c4..f89c63edd 100644 --- a/plugins/notion/src/blocksToHTML.ts +++ b/plugins/notion/src/blocksToHTML.ts @@ -138,6 +138,5 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) { break } } - console.log('blocksToHtml', htmlContent) return htmlContent }
${richTextToHTML(cell)}