diff --git a/notionsci/connections/notion/structures/blocks.py b/notionsci/connections/notion/structures/blocks.py index 352d71a..e513538 100644 --- a/notionsci/connections/notion/structures/blocks.py +++ b/notionsci/connections/notion/structures/blocks.py @@ -29,7 +29,10 @@ class BlockType(Enum): file = 'file' pdf = 'pdf' bookmark = 'bookmark' + equation = 'equation' + code = 'code' unsupported = 'unsupported' + child_database = 'child_database' BlockConvertor = ListConvertor(ForwardRefConvertor('Block')) @@ -229,6 +232,35 @@ def to_markdown(self, context: MarkdownContext) -> str: caption=self.to_markdown_caption(context) or ' ') +@dataclass_dict_convert(dict_letter_case=snakecase) +@dataclass +class EquationBlock(ToMarkdownMixin): + expression: str + + def to_markdown(self, context: MarkdownContext) -> str: + return MarkdownBuilder.equation(self.expression) + + +@dataclass_dict_convert(dict_letter_case=snakecase) +@dataclass +class CodeBlock(ToMarkdownMixin): + text: List[RichText] + language: str + + def to_markdown(self, context: MarkdownContext) -> str: + content = chain_to_markdown(self.text, context) + return MarkdownBuilder.code(content, self.language) + + +@dataclass_dict_convert(dict_letter_case=snakecase) +@dataclass +class ChildDatabaseBlock(ToMarkdownMixin): + title: str + + def to_markdown(self, context: MarkdownContext) -> str: + return f'Child Database {self.title} !!!!\n' + + FileBlock = FileObject @@ -260,6 +292,9 @@ class Block(ToMarkdownMixin): file: Optional[FileBlock] = None pdf: Optional[PdfBlock] = None bookmark: Optional[BookmarkBlock] = None + equation: Optional[EquationBlock] = None + code: Optional[CodeBlock] = None + child_database: Optional[ChildDatabaseBlock] = None unsupported: Optional[str] = None def to_markdown(self, context: MarkdownContext) -> str: diff --git a/notionsci/connections/notion/structures/common.py b/notionsci/connections/notion/structures/common.py index 5ffa610..dae4509 100644 --- a/notionsci/connections/notion/structures/common.py +++ b/notionsci/connections/notion/structures/common.py @@ -28,7 +28,7 @@ class UserObject: class RichTextType(Enum): text = 'text' mention = 'mention' - equation = 'equation' + equation = 'inline_equation' @dataclass_dict_convert(dict_letter_case=snakecase) @@ -122,7 +122,7 @@ def to_markdown(self, context: MarkdownContext) -> str: if self.type == RichTextType.text: result = self.plain_text elif self.type == RichTextType.equation: - result = MarkdownBuilder.equation(self.equation.expression) + result = MarkdownBuilder.inline_equation(self.equation.expression) elif self.type == RichTextType.mention: result = self.plain_text diff --git a/notionsci/utils/markdown.py b/notionsci/utils/markdown.py index 802a759..5d8b5a5 100644 --- a/notionsci/utils/markdown.py +++ b/notionsci/utils/markdown.py @@ -82,7 +82,7 @@ def url(url, alt=None): return f'[{alt or url}]({url})' @staticmethod - def equation(equation: str): + def inline_equation(equation: str): return f'${equation}$' @staticmethod @@ -157,6 +157,14 @@ def table(data: pd.DataFrame, alignments: Dict[str, str] = None): data.style.set_properties(subset=[col], **{'text-align': align}) return data.to_markdown() + @staticmethod + def code(content, language): + return f'```{language}\n{content}\n```' + + @staticmethod + def equation(content): + return f'$$\n{content}\n$$' + def chain_to_markdown(items: List[ToMarkdownMixin], context: MarkdownContext, sep='', prefix=''): result = [] @@ -165,4 +173,4 @@ def chain_to_markdown(items: List[ToMarkdownMixin], context: MarkdownContext, se context.counter = 1 result.append(item.to_markdown(context)) - return sep.join(map(lambda x: f'{prefix}{x}', filter(lambda x: x is not None, result))) if items else None + return sep.join(map(lambda x: f'{prefix}{x}', filter(lambda x: x is not None, result))) if items else ''