fix(notion-writer): paginate clear_page_content to handle >100 blocks
notion.blocks.children.list returns at most 100 blocks per call. Without pagination, --replace only cleared the first 100 blocks of a page, leaving the rest behind and producing surprising partial-replace behavior. Loop with next_cursor until has_more is false so the entire page is cleared. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -671,19 +671,31 @@ def append_to_page(notion: Client, page_id: str, blocks: List[Dict]) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def clear_page_content(notion: Client, page_id: str) -> bool:
|
def clear_page_content(notion: Client, page_id: str) -> bool:
|
||||||
"""Clear all content from a page."""
|
"""Clear all content from a page (paginates through all children)."""
|
||||||
try:
|
try:
|
||||||
formatted_id = format_id_with_dashes(page_id)
|
formatted_id = format_id_with_dashes(page_id)
|
||||||
|
|
||||||
# Get all child blocks
|
# Notion's blocks.children.list returns at most 100 blocks per call.
|
||||||
children = notion.blocks.children.list(block_id=formatted_id)
|
# Paginate via next_cursor so --replace clears the entire page, not
|
||||||
|
# just the first 100 blocks.
|
||||||
|
cursor = None
|
||||||
|
while True:
|
||||||
|
kwargs = {'block_id': formatted_id, 'page_size': 100}
|
||||||
|
if cursor:
|
||||||
|
kwargs['start_cursor'] = cursor
|
||||||
|
children = notion.blocks.children.list(**kwargs)
|
||||||
|
|
||||||
# Delete each block (skip already archived blocks)
|
|
||||||
for block in children.get('results', []):
|
for block in children.get('results', []):
|
||||||
if block.get('archived'):
|
if block.get('archived'):
|
||||||
continue
|
continue
|
||||||
notion.blocks.delete(block_id=block['id'])
|
notion.blocks.delete(block_id=block['id'])
|
||||||
|
|
||||||
|
if not children.get('has_more'):
|
||||||
|
break
|
||||||
|
cursor = children.get('next_cursor')
|
||||||
|
if not cursor:
|
||||||
|
break
|
||||||
|
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error clearing page: {e}")
|
print(f"Error clearing page: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user