Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion plugins/notion/src/blocksToHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {
case "code":
htmlContent += `<pre><code class="language-${block.code.language.replace(" ", "-")}">${richTextToHTML(block.code.rich_text)}</code></pre>`
break
case "table":
htmlContent += `<table>`
break
case "table_row":
if (blocks[i - 1]?.type === "table") {
htmlContent += `<thead><tr>`
block.table_row.cells.forEach((cell) => {
htmlContent += `<th>${richTextToHTML(cell)}</th>`
})
htmlContent += `</tr></thead><tbody>`
} else {
htmlContent += `<tr>`
block.table_row.cells.forEach((cell) => {
htmlContent += `<td>${richTextToHTML(cell)}</td>`
})
htmlContent += `</tr>`
}

if (blocks[i + 1]?.type !== "table_row") {
htmlContent += `</tbody></table>`
}
break
case "video": {
if (block.video.type !== "external") {
break
Expand All @@ -116,6 +138,5 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {
break
}
}

return htmlContent
}
18 changes: 18 additions & 0 deletions plugins/notion/src/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand Down
Loading