Skip to content

Commit

Permalink
fix build - buildTableOfContents infinite loop memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
jackieluc committed Jan 4, 2023
1 parent ceefedc commit 1e138b5
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/utils/notion/buildTableOfContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,22 @@ export default function buildTableOfContents(content: BlockObjectResponse[]) {
let index = 0;
while (index < headingBlocks.length && headingBlocks[index]?.type) {
const headingBlock = headingBlocks[index];
const nextHeadingBlock = headingBlocks[index + 1];

if (BlockType.Heading2 in headingBlock) {
if (headingBlock?.type === BlockType.Heading2) {
const title = headingBlock[headingBlock.type].rich_text[0].plain_text;

let tocItem: TableOfContent = {
title,
url: `#${getSlug(title)}`,
};

// if next block is an h2, skip
if (nextHeadingBlock && BlockType.Heading2 in nextHeadingBlock) {
if (headingBlocks[index + 1]?.type === BlockType.Heading2) {
index++;
} else {
// for each h3, add as children to h2
const childrenHeadings = [];
while (nextHeadingBlock && BlockType.Heading3 in nextHeadingBlock) {
const headingBlock = nextHeadingBlock;

while (headingBlocks[index + 1]?.type === BlockType.Heading3) {
const headingBlock = headingBlocks[index + 1] as Heading3BlockObjectResponse;
const title = headingBlock[headingBlock.type].rich_text[0].plain_text;
let tocItem: TableOfContent = {
title,
Expand Down

1 comment on commit 1e138b5

@vercel
Copy link

@vercel vercel bot commented on 1e138b5 Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.