Skip to content

Commit

Permalink
[FEATURE] Improved page property table to markdown serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
egordm committed Oct 5, 2021
1 parent 1a204d3 commit c544931
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions notionsci/connections/notion/structures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion notionsci/connections/notion/structures/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions notionsci/connections/notion/structures/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -66,14 +66,17 @@ class RelationItem:
LastEditedByValue = Dict
UrlValue = str
RelationValue = List[RelationItem]
FilesValue = List[FileObject]


def object_to_text_value(raw_value: Any):
if isinstance(raw_value, list):
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=' '):
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c544931

Please sign in to comment.