Skip to content

Commit

Permalink
[FEATURE] Added support for equation and code blocks to markdown conv…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
egordm committed Oct 5, 2021
1 parent 4226d26 commit 1bc6497
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
35 changes: 35 additions & 0 deletions notionsci/connections/notion/structures/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions notionsci/connections/notion/structures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions notionsci/utils/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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 ''

0 comments on commit 1bc6497

Please sign in to comment.