From c544931dadb1c5fd711be68670862dea92590f81 Mon Sep 17 00:00:00 2001 From: Egor Dmitriev Date: Tue, 5 Oct 2021 22:40:50 +0200 Subject: [PATCH] [FEATURE] Improved page property table to markdown serialization --- notionsci/connections/notion/structures/common.py | 5 +++-- notionsci/connections/notion/structures/content.py | 2 +- .../connections/notion/structures/properties.py | 13 +++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/notionsci/connections/notion/structures/common.py b/notionsci/connections/notion/structures/common.py index dae4509..d4832f9 100644 --- a/notionsci/connections/notion/structures/common.py +++ b/notionsci/connections/notion/structures/common.py @@ -163,15 +163,16 @@ class FileObject(ToMarkdownMixin): caption: Optional[List[RichText]] = None file: Optional[FileTypeObject] = None external: Optional[FileTypeObject] = None + name: Optional[str] = None # Only filled for when used as property value def get_url(self) -> str: return self.file.url if self.type == FileType.file else self.external.url def to_markdown_caption(self, context: MarkdownContext) -> str: - return chain_to_markdown(self.caption, context) + return chain_to_markdown(self.caption, context) if self.caption else None def to_markdown(self, context: MarkdownContext) -> str: - return MarkdownBuilder.url(self.get_url(), self.to_markdown_caption(context)) + return MarkdownBuilder.url(self.get_url(), self.name or self.to_markdown_caption(context)) @dataclass_dict_convert(dict_letter_case=snakecase) diff --git a/notionsci/connections/notion/structures/content.py b/notionsci/connections/notion/structures/content.py index 394915e..1b2d164 100644 --- a/notionsci/connections/notion/structures/content.py +++ b/notionsci/connections/notion/structures/content.py @@ -94,7 +94,7 @@ class Page(ContentObject, ToMarkdownMixin, ChildrenMixin, HasPropertiesMixin[Pro def to_markdown(self, context: MarkdownContext) -> str: title = MarkdownBuilder.heading(self.get_title(), 'h1') prop_data = [ - {'Name': name, 'Value': prop.value()} for name, prop in self.properties.items() + {'Name': name, 'Value': prop.to_markdown(context)} for name, prop in self.properties.items() if prop.type != PropertyType.title ] props = MarkdownBuilder.table(pd.DataFrame(prop_data)) + '\n' if prop_data else None diff --git a/notionsci/connections/notion/structures/properties.py b/notionsci/connections/notion/structures/properties.py index c37c102..f1cf8b3 100644 --- a/notionsci/connections/notion/structures/properties.py +++ b/notionsci/connections/notion/structures/properties.py @@ -6,7 +6,7 @@ from dataclass_dict_convert import dataclass_dict_convert from stringcase import snakecase -from notionsci.connections.notion.structures.common import RichText, Color, ID +from notionsci.connections.notion.structures.common import RichText, Color, ID, FileObject from notionsci.utils import ExplicitNone, ToMarkdownMixin, MarkdownContext @@ -66,6 +66,7 @@ class RelationItem: LastEditedByValue = Dict UrlValue = str RelationValue = List[RelationItem] +FilesValue = List[FileObject] def object_to_text_value(raw_value: Any): @@ -73,7 +74,9 @@ def object_to_text_value(raw_value: Any): return ' '.join([object_to_text_value(v) for v in raw_value]) elif isinstance(raw_value, RichText): return raw_value.text_value() - return raw_value + elif isinstance(raw_value, Dict): + return str(raw_value) + return str(raw_value) def object_to_markdown(raw_value: Any, context: MarkdownContext, sep=' '): @@ -83,7 +86,9 @@ def object_to_markdown(raw_value: Any, context: MarkdownContext, sep=' '): return raw_value.to_markdown(context) elif isinstance(raw_value, SelectValue): return raw_value.name - return raw_value + elif isinstance(raw_value, ToMarkdownMixin): + return raw_value.to_markdown(context) + return str(raw_value) ## Property Definition Types @@ -102,7 +107,7 @@ class Property(ToMarkdownMixin): multi_select: Optional[MultiSelectValue] = None date: Optional[DateValue] = None people: Optional[PeopleValue] = None - files: Optional[Dict] = None + files: Optional[FilesValue] = None checkbox: Optional[CheckboxValue] = None url: Optional[UrlValue] = None email: Optional[EmailValue] = None