Skip to content

Commit b34e902

Browse files
committed
Don't delete / re-upload blocks unless we have to - reduce page disruption
1 parent 0040466 commit b34e902

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/_notion_scripts/upload.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def main(
154154
(page,) = pages_matching_title
155155
else:
156156
page = session.create_page(parent=parent, title=title)
157-
sys.stdout.write(f"Created new page: {title} ({page.url})\n")
157+
sys.stdout.write(f"Created new page: '{title}' ({page.url})\n")
158158

159159
if icon:
160160
page.icon = Emoji(emoji=icon)
@@ -164,8 +164,18 @@ def main(
164164
for details in blocks
165165
]
166166

167-
for child in page.children:
168-
child.delete()
169-
170-
page.append(blocks=block_objs)
171-
sys.stdout.write(f"Updated existing page: {title} ({page.url})\n")
167+
match_until_index = 0
168+
for index, existing_page_block in enumerate(iterable=page.children):
169+
if index < len(blocks) and existing_page_block == block_objs[index]:
170+
match_until_index = index
171+
else:
172+
break
173+
174+
sys.stdout.write(
175+
f"Matching blocks until index {match_until_index} for page '{title}'\n"
176+
)
177+
for existing_page_block in page.children[match_until_index + 1 :]:
178+
existing_page_block.delete()
179+
180+
page.append(blocks=block_objs[match_until_index + 1 :])
181+
sys.stdout.write(f"Updated existing page: '{title}' ({page.url})\n")

0 commit comments

Comments
 (0)