notion-graph-view
notion-graph-view copied to clipboard
add_node inside parser need to be enhanced
Faced index out of bound issue before, need to handle the empty/null title list case. Below is the updated working version
def _add_node(self, block: any, **kwargs):
"""
:param block: any type of block, page, database
:kwargs url: page or database url
"""
url = block.get('url', '')
title = block.get('title', None)
if not title or not isinstance(title, str):
if block.get('object') == 'database':
title_list = block.get('title', [])
if title_list and isinstance(title_list, list) and 'plain_text' in title_list[0]:
title = title_list[0]['plain_text']
else:
title = 'Untitled'
elif block.get('object') == 'page':
properties = block.get('properties', {})
if block.get('parent', {}).get('type') != "database_id":
title_list = properties.get('title', {}).get('title', [])
if title_list and isinstance(title_list, list) and 'plain_text' in title_list[0]:
title = title_list[0]['plain_text']
else:
title = 'Untitled'
else:
title_list = properties.get('Name', {}).get('title', [])
if title_list and isinstance(title_list, list) and 'plain_text' in title_list[0]:
title = title_list[0]['plain_text']
else:
title = 'Untitled'
else:
title = block.get(block.get('type'), {}).get('title', 'Untitled')
print("+node:", title)
self._graph.add_node(
block.get('id', 'unknown_id'),
label=title,
title=f'<a href="{url}">open page</a>',
color=COLOR_NODE,
size=10,
borderWidth=0
)