From d57778fc4c27774fa11260ea3aa105db1b1f1436 Mon Sep 17 00:00:00 2001 From: Philippe Rolet Date: Wed, 10 Jan 2024 15:42:44 +0100 Subject: [PATCH] Fix: notion document block ordering (#3132) Related [incident](https://dust4ai.slack.com/archives/C05B529FHV1/p1704893056589759) --- connectors/src/connectors/notion/temporal/activities.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/connectors/src/connectors/notion/temporal/activities.ts b/connectors/src/connectors/notion/temporal/activities.ts index 64930710c6a7..4d82e7709894 100644 --- a/connectors/src/connectors/notion/temporal/activities.ts +++ b/connectors/src/connectors/notion/temporal/activities.ts @@ -1502,6 +1502,8 @@ export async function renderAndUpsertPageFromCache({ const topLevelBlocks = blockCacheEntries.filter( (b) => b.parentBlockId === null ); + topLevelBlocks.sort((a, b) => a.indexInParent - b.indexInParent); + const blocksByParentId: Record = {}; for (const blockCacheEntry of blockCacheEntries) { if (!blockCacheEntry.parentBlockId) continue; @@ -1572,6 +1574,7 @@ export async function renderAndUpsertPageFromCache({ } let renderedBlock = b.blockText ? `${indent}${b.blockText}\n` : ""; const children = blocksByParentId[b.notionBlockId] ?? []; + children.sort((a, b) => a.indexInParent - b.indexInParent); for (const child of children) { renderedBlock += renderBlock(child, `- ${indent}`); }