Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write ExpandingLinkToPageBlock._get_child_blocks #180

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ Here are some features we're planning to add in the future:

## Changelog

### v0.10.3
- Add `_get_child_blocks` to the `ExpandingLinkToPageBlock` in the `expandlinktopages.py`
plugin and implement it to patch the `Client.get_child_blocks` inside of the `to_pandoc` method.
This sets the `page` argument to the `self.page` of the `ExpandingLinkToPageBlock` instance,
making sure that the parent page of the blocks on the linked page is set to the parent page of
the `ExpandingLinkToPageBlock` and not the linked page itself.

### v0.10.2
- Have the `ConnectionThrottled` exception inherit the `HTTPResponseError` exception and update tests

Expand Down
33 changes: 27 additions & 6 deletions n2y/plugins/expandlinktopages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

from n2y.blocks import LinkToPageBlock


Expand All @@ -11,12 +13,16 @@ def to_pandoc(self):
assert self.linked_node_id is not None

if self.link_type == "page_id":
page = self.client.get_page(self.linked_node_id)
# The `page.block` refers to the ChildPageBlock in the page; we don't
# want to call `to_pandoc` on it directly, since we don't want a
# full pandoc document, but just the content that would have been in
# that document.
return page.block.children_to_pandoc()
# This `with` statement ensures that the parent page of the blocks on the linked page
# is set to the parent page of the `ExpandingLinkToPageBlock` and not the linked page
# itself.
with patch.object(self.client, "get_child_blocks", self._get_child_blocks()):
page = self.client.get_page(self.linked_node_id)
# The `page.block` refers to the ChildPageBlock in the page; we don't
# want to call `to_pandoc` on it directly, since we don't want a
# full pandoc document, but just the content that would have been in
# that document.
return page.block.children_to_pandoc()
else:
# TODO: Might be expanded to handle links to databases as well.
self.client.logger.warning(
Expand All @@ -26,6 +32,21 @@ def to_pandoc(self):
)
return None

def _get_child_blocks(self):
"""
returns a function that operates like `n2y.notion.Client.get_child_blocks
but sets the `page` argument as the `self.page` of this block.
"""

def func(block_id, _, get_children):
child_notion_blocks = self.client.get_child_notion_blocks(block_id)
return [
self.client.wrap_notion_block(b, self.page, get_children)
for b in child_notion_blocks
]

return func


notion_classes = {
"blocks": {
Expand Down
Loading