Skip to content
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
8 changes: 7 additions & 1 deletion n2y/plugins/internallinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ class NotionInternalLink(TextRichText):

def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
if block is None or not is_internal_link(self.href, self.block.page.notion_id):
# Rich text can appear in contexts without a page (e.g. property
# values); only same-page links inside a page's blocks are resolvable.
if (
block is None
or block.page is None
or not is_internal_link(self.href, self.block.page.notion_id)
):
raise UseNextClass

def to_pandoc(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_plugin_internallinks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest.mock import Mock, patch

import pytest

from n2y.errors import UseNextClass

from n2y.blocks import ChildPageBlock, DividerBlock, HeadingOneBlock, ParagraphBlock
from n2y.notion import Client
from n2y.notion_mocks import (
Expand All @@ -12,6 +16,7 @@
)
from n2y.page import Page
from n2y.plugins.internallinks import (
NotionInternalLink,
find_target_block,
get_notion_id_from_href,
is_internal_link,
Expand Down Expand Up @@ -151,6 +156,17 @@ def mock_page_with_link_to_divider(wrap_notion_user, link_text: str = "see below
return page, href


@patch("n2y.notion.Client.wrap_notion_user")
def test_internal_link_in_block_without_page_uses_next_class(wrap_notion_user):
# Rich text can appear in contexts whose block has no page (e.g. property
# values); the plugin must defer instead of crashing on block.page.
client = Client("", plugins=["n2y.plugins.internallinks"])
wrap_notion_user.return_value = User(client, mock_user())
rich_text = mock_rich_text(text="x", href="/1234#5678")
with pytest.raises(UseNextClass):
NotionInternalLink(client, rich_text, block=Mock(page=None))


def test_internal_link_to_textless_block_left_unresolved():
# Links can target blocks with no rich_text (images, dividers); the plugin
# must not crash and should leave the link unchanged.
Expand Down
Loading