Skip to content

Commit f814093

Browse files
committed
Progress towards getting all children
1 parent c37b7d3 commit f814093

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/_notion_scripts/upload.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ultimate_notion.blocks import Image as UnoImage
1818
from ultimate_notion.blocks import Video as UnoVideo
1919
from ultimate_notion.obj_api.blocks import Block as UnoObjAPIBlock
20+
from ultimate_notion.obj_api.core import Unset, UnsetType
2021

2122

2223
class _SerializedBlockTreeNode(TypedDict):
@@ -120,6 +121,75 @@ def _upload_blocks_recursively(
120121
)
121122

122123

124+
@beartype
125+
def _reconstruct_nested_structure(
126+
*,
127+
items: list[dict[str, Any]],
128+
) -> list[dict[str, Any]]:
129+
"""
130+
Reconstruct nested structure from flattened block items.
131+
"""
132+
result: list[dict[str, Any]] = []
133+
for item in items:
134+
block = item["block"]
135+
block_type = block["type"]
136+
137+
# Handle blocks that can have children
138+
if block_type in {
139+
"paragraph",
140+
"callout",
141+
"bulleted_list_item",
142+
"numbered_list_item",
143+
"toggle",
144+
}:
145+
pass
146+
# children = item.get("children", [])
147+
# nested_blocks = _reconstruct_nested_structure(
148+
# items=children,
149+
# )
150+
# block[block_type]["children"] = nested_blocks
151+
152+
result.append(block)
153+
return result
154+
155+
156+
@beartype
157+
def _sanitize_serialized_block(
158+
*,
159+
serialized_block: dict[str, Any],
160+
) -> dict[str, Any]:
161+
"""
162+
Sanitize a serialized block.
163+
"""
164+
return serialized_block
165+
if isinstance(serialized_block, dict):
166+
return {
167+
key: _sanitize_serialized_block(serialized_block=value)
168+
for key, value in serialized_block.items()
169+
}
170+
return serialized_block
171+
172+
173+
@beartype
174+
def _sanitize_block(
175+
*,
176+
block: Block,
177+
session: Session,
178+
) -> Any:
179+
"""
180+
Sanitize a block.
181+
"""
182+
if block.obj_ref.id and not isinstance(block.obj_ref.id, UnsetType):
183+
block = session.get_block(block_ref=block.obj_ref.id)
184+
block.obj_ref.created_by = Unset
185+
block.obj_ref.last_edited_by = Unset
186+
block.obj_ref.created_time = Unset
187+
block.obj_ref.last_edited_time = Unset
188+
block.obj_ref.id = Unset
189+
block.obj_ref.parent = Unset
190+
return block
191+
192+
123193
@click.command()
124194
@click.option(
125195
"--file",
@@ -185,6 +255,53 @@ def main(
185255

186256
for child in page.children:
187257
child.delete()
258+
block_list = _reconstruct_nested_structure(items=blocks)
259+
block_obj_list = [
260+
Block.wrap_obj_ref(UnoObjAPIBlock.model_validate(obj=block))
261+
for block in block_list
262+
]
263+
sanitized_block_list = [
264+
_sanitize_block(block=block, session=session)
265+
for block in block_obj_list
266+
]
267+
children_list = list(page.children)
268+
sanitized_children_list = [
269+
_sanitize_block(block=child, session=session)
270+
for child in children_list
271+
]
272+
273+
import difflib
274+
import pprint
275+
276+
for index, page_child in enumerate(sanitized_children_list):
277+
block_child = sanitized_block_list[index]
278+
if page_child != block_child:
279+
s1 = pprint.pformat(
280+
page_child.__dict__, sort_dicts=True
281+
).splitlines()
282+
s2 = pprint.pformat(
283+
block_child.__dict__, sort_dicts=True
284+
).splitlines()
285+
286+
diff = difflib.unified_diff(s1, s2)
287+
288+
diff_list = list(diff)
289+
nice_diff = "\n".join(diff)
290+
breakpoint()
291+
# children_obj_list = [
292+
# Block.wrap_obj_ref(UnoObjAPIBlock.model_validate(obj=child))
293+
# for child in children_list
294+
# ]
295+
# serialized_children_list = [
296+
# child.obj_ref.serialize_for_api() for child in children_list
297+
# ]
298+
# sanitized_serialized_children_list = [
299+
# _sanitize_serialized_block(serialized_block=serialized_block)
300+
# for serialized_block in serialized_children_list
301+
# ]
302+
breakpoint()
303+
# for child in page.children:
304+
# child.delete()
188305

189306
# See https://developers.notion.com/reference/request-limits#limits-for-property-values
190307
# which shows that the max number of blocks per request is 100.

src/sphinx_notion/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ def _create_rich_text_from_children(*, node: nodes.Element) -> Text:
9292
for child in node.children:
9393
if isinstance(child, nodes.reference):
9494
link_url = child.attributes["refuri"]
95+
assert isinstance(link_url, str)
96+
link_url_with_slash = link_url.rstrip("/") + "/"
9597
link_text = child.attributes.get("name", link_url)
9698

9799
new_text = text(
98100
text=link_text,
99-
href=link_url,
101+
href=link_url_with_slash,
100102
bold=False,
101103
italic=False,
102104
code=False,
105+
color=default_color,
103106
)
104107
elif isinstance(child, nodes.target):
105108
continue

0 commit comments

Comments
 (0)