Skip to content

Commit

Permalink
Add divider support and some test case
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Apr 27, 2023
1 parent acd6059 commit a04412c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions html2notion/translate/html2json_yinxiang.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def convert_paragraph(self, soup):
rich_text.extend(text_obj)
return json_obj

def convert_divider(self, soup):
return {
"object": "block",
"type": "divider",
"divider": {}
}

def convert_heading(self, soup):
heading_map = {"h1": "heading_1", "h2": "heading_2", "h3": "heading_3",
"h4": "heading_3", "h5": "heading_3", "h6": "heading_3"}
Expand Down Expand Up @@ -300,6 +307,8 @@ def get_block_type(self, single_tag):
# at the same time, and the first one takes precedence.
if self._check_is_todo(single_tag):
return Block.TO_DO.value
elif tag_name == 'hr':
return Block.DIVIDER.value
elif tag_name == 'ol':
return Block.NUMBERED_LIST.value
elif tag_name == 'ul':
Expand Down
66 changes: 65 additions & 1 deletion tests/test_yinxiang.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,68 @@
}
]

divider_content = '<hr/>'
divider_block = [
{
"object": "block",
"type": "divider",
"divider": {}
}
]

quote_content = """<div style="--en-blockquote:true;box-sizing: border-box; padding-left: 19px; padding-top: 6px; padding-bottom: 6px; border-left: 3px solid #b4c0cc; background-position: initial initial; background-repeat: initial initial; margin-top: 6px"><div>We recommend completing our quickstart tutorial to get acquainted with key concepts through a <span style="color: #9B00FF;">hands-on, interactive example</span>.</div><div><br/></div><div>First, you’ll need a prompt that makes it clear what you want. Let’s start with an instruction. <b>Submit this prompt</b> to generate your first completion.</div></div>"""
quote_block = [
{
"object": "block",
"type": "quote",
"quote": {
"rich_text": [
{
"plain_text": "We recommend completing our quickstart tutorial to get acquainted with key concepts through a ",
"text": {
"content": "We recommend completing our quickstart tutorial to get acquainted with key concepts through a "
},
"type": "text"
},
{
"plain_text": "hands-on, interactive example",
"text": {
"content": "hands-on, interactive example"
},
"type": "text",
"annotations": {
"color": "purple"
}
},
{
"plain_text": ".\n\nFirst, you\u2019ll need a prompt that makes it clear what you want. Let\u2019s start with an instruction.\u00a0",
"text": {
"content": ".\n\nFirst, you\u2019ll need a prompt that makes it clear what you want. Let\u2019s start with an instruction.\u00a0"
},
"type": "text"
},
{
"plain_text": "Submit this prompt",
"text": {
"content": "Submit this prompt"
},
"type": "text",
"annotations": {
"bold": True
}
},
{
"plain_text": "\u00a0to generate your first completion.",
"text": {
"content": "\u00a0to generate your first completion."
},
"type": "text"
}
]
}
}
]

def test_convert():
if 'GITHUB_ACTIONS' not in os.environ:
from html2notion.utils import test_prepare_conf, logger
Expand All @@ -768,14 +830,16 @@ def test_convert():
super_note_table_content: table_block,
to_do_content: to_do_block,
to_do_normal_content: to_do_block,
divider_content: divider_block,
quote_content: quote_block,
}

for html_content in html_jsons:
body_content = '<body>' + html_content + '</body>'
yinxiang = Html2JsonYinXiang(body_content)
yinxiang.process()
json_obj = yinxiang.children
print(json.dumps(json_obj, indent=4))
# print(json.dumps(json_obj, indent=4))
assert json_obj == html_jsons[html_content]


Expand Down

0 comments on commit a04412c

Please sign in to comment.