Skip to content

Fix callouts with FileIcons #504

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,57 @@ def get_html(self) -> Optional[HtmlTag]:
return None


@dataclass
class FileIconContent(FromJSONMixin):
url: str
expiry_time: Optional[str] = None # Add expiry_time if needed

@classmethod
def from_dict(cls, data: dict):
# Only include expiry_time if it exists in the dictionary
# Notion API might not always include it
init_data = {"url": data.get("url")}
if "expiry_time" in data:
init_data["expiry_time"] = data.get("expiry_time")
return cls(**init_data)


@dataclass
class FileIcon(FromJSONMixin, GetHTMLMixin):
file: FileIconContent
type: str = "file"

@classmethod
def from_dict(cls, data: dict):
return cls(file=FileIconContent.from_dict(data=data.pop("file")), **data)

def get_html(self) -> Optional[HtmlTag]:
# Render the file URL, similar to how ExternalIcon is handled
if self.file:
# Could potentially render an <img> tag, but sticking to URL for consistency
return A([Href(self.file.url)], [f"[File Icon: {self.file.url}]"])
else:
return None


class Icon(FromJSONMixin):
@classmethod
def from_dict(cls, data: dict) -> Union[EmojiIcon, ExternalIcon]:
def from_dict(cls, data: dict) -> Union[EmojiIcon, ExternalIcon, FileIcon]:
t = data.get("type")
if t == "emoji":
return EmojiIcon.from_dict(data)
elif t == "external":
return ExternalIcon.from_dict(data)
elif t == "file":
return FileIcon.from_dict(data)
else:
raise ValueError(f"Unexpected icon type: {t} ({data})")


@dataclass
class Callout(BlockBase):
color: str
icon: Optional[Union[EmojiIcon, ExternalIcon]] = None
icon: Optional[Union[EmojiIcon, ExternalIcon, FileIcon]] = None
rich_text: List[RichText] = field(default_factory=list)

@staticmethod
Expand All @@ -76,9 +111,11 @@ def can_have_children() -> bool:
@classmethod
def from_dict(cls, data: dict):
rich_text = data.pop("rich_text", [])
icon_data = data.pop("icon", None)
icon = Icon.from_dict(icon_data) if icon_data else None
return cls(
color=data["color"],
icon=Icon.from_dict(data.pop("icon")),
icon=icon,
rich_text=[RichText.from_dict(rt) for rt in rich_text],
)

Expand Down
Loading