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,18 +671,30 @@ def append_to_page(notion: Client, page_id: str, blocks: List[Dict]) -> 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:
|
||||
formatted_id = format_id_with_dashes(page_id)
|
||||
|
||||
# Get all child blocks
|
||||
children = notion.blocks.children.list(block_id=formatted_id)
|
||||
# Notion's blocks.children.list returns at most 100 blocks per call.
|
||||
# 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', []):
|
||||
if block.get('archived'):
|
||||
continue
|
||||
notion.blocks.delete(block_id=block['id'])
|
||||
for block in children.get('results', []):
|
||||
if block.get('archived'):
|
||||
continue
|
||||
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
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user