From 88b3a0d66c089b1c4ed9cb2678bf4af3020b8c4e Mon Sep 17 00:00:00 2001 From: Florian Thonig Date: Mon, 7 Oct 2024 23:10:54 +0200 Subject: [PATCH] fix invalid structure blocks in my project blocks occur that dont have a value and this borks my project. so make this code more resilient. --- src/lib/utils.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 05de994..bdeb66e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -12,8 +12,20 @@ const groupBlockContent = (blockMap: BlockMap) => { let index = -1 Object.keys(blockMap).forEach((id) => { - blockMap[id].value.content?.forEach((blockId) => { - const blockType = blockMap[blockId]?.value?.type + const block = blockMap[id] + if (!block || !block.value || !block.value.content) { + // Skip this block if it's undefined or doesn't have the expected structure + return + } + + block.value.content.forEach((blockId) => { + const contentBlock = blockMap[blockId] + if (!contentBlock || !contentBlock.value) { + // Skip this content block if it's undefined or doesn't have the expected structure + return + } + + const blockType = contentBlock.value.type if (blockType && blockType !== lastType) { index++ @@ -32,10 +44,10 @@ const groupBlockContent = (blockMap: BlockMap) => { export const getListNumber = (blockId: string, blockMap: BlockMap) => { const groups = groupBlockContent(blockMap) - const group = groups.find((g) => g.includes(blockId)) + const group = groups.find((g) => g && g.includes(blockId)) if (!group) { - return + return undefined } return group.indexOf(blockId) + 1