Skip to content

Commit 13447a8

Browse files
committed
Try support supper note: fix code tag
1 parent 6fcf053 commit 13447a8

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

html2notion/translate/html2json_yinxiang.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def convert_code(self, soup):
9999
},
100100
}
101101
rich_text = json_obj["code"]["rich_text"]
102-
children_list = list(soup.children) if soup.has_attr('children') else [soup]
102+
children_list = list(soup.children) if isinstance(soup, Tag) else [soup]
103103
for index, child in enumerate(children_list):
104104
is_last_child = index == len(children_list) - 1
105105
text_obj = self.parse_inline_block(child)
@@ -220,15 +220,17 @@ def _recursive_parse_style(self, tag_soup, tag_text, text_params):
220220

221221
for child in tag_soup.children:
222222
logger.debug(f'Recursive, child: {child}, {child.name}')
223-
if child.name:
223+
if isinstance(child, Tag):
224224
self._recursive_parse_style(child, child.text, text_params)
225225
return
226226

227227
# <b><u>unlineline and bold</u></b>
228228
# <div><font color="#ff2600">Red color4</font></div>
229+
# <div> Code in super note</div>
229230
def parse_inline_block(self, tag_soup):
230231
block_objs = []
231-
for child in tag_soup.children:
232+
all_tags = tag_soup.children if isinstance(tag_soup, Tag) else [tag_soup]
233+
for child in all_tags:
232234
text_params = {}
233235
tag_name = child.name.lower() if child.name else ""
234236
child_text = child.text if child.text else ""

tests/test_yinxiang.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,96 @@
408408
}
409409
]
410410

411+
code_paragraph_content = '<div style="-en-codeblock:true;"><div>Quote 1</div><div>2</div><div>3</div><div><span style="color: rgb(255, 38, 0);">Read 4</span></div><div><span style="color: rgb(255, 38, 0);">5</span></div><div>6. <a href="https://openai.com/">OpenAI</a>’s mission is to create artificial intelligence systems that benefit everyone. To that end, we invest heavily in research and engineering to ensure our AI systems are safe and secure. However, as with any <font color="#942192"><b>complex technology</b></font>, we understand that vulnerabilities and flaws can emerge.</div></div>'
412+
code_paragraph_block = [
413+
{
414+
"object": "block",
415+
"type": "code",
416+
"code": {
417+
"rich_text": [
418+
{
419+
"plain_text": "Quote 1\n2\n3\n",
420+
"text": {
421+
"content": "Quote 1\n2\n3\n"
422+
},
423+
"type": "text"
424+
},
425+
{
426+
"plain_text": "Read 4",
427+
"text": {
428+
"content": "Read 4"
429+
},
430+
"type": "text",
431+
"annotations": {
432+
"color": "red"
433+
}
434+
},
435+
{
436+
"plain_text": "\n",
437+
"text": {
438+
"content": "\n"
439+
},
440+
"type": "text"
441+
},
442+
{
443+
"plain_text": "5",
444+
"text": {
445+
"content": "5"
446+
},
447+
"type": "text",
448+
"annotations": {
449+
"color": "red"
450+
}
451+
},
452+
{
453+
"plain_text": "\n6. ",
454+
"text": {
455+
"content": "\n6. "
456+
},
457+
"type": "text"
458+
},
459+
{
460+
"href": "https://openai.com/",
461+
"plain_text": "OpenAI",
462+
"text": {
463+
"link": {
464+
"url": "https://openai.com/"
465+
},
466+
"content": "OpenAI"
467+
},
468+
"type": "text"
469+
},
470+
{
471+
"plain_text": "\u2019s mission is to create artificial intelligence systems that benefit everyone. To that end, we invest heavily in research and engineering to ensure our AI systems are safe and secure. However, as with any ",
472+
"text": {
473+
"content": "\u2019s mission is to create artificial intelligence systems that benefit everyone. To that end, we invest heavily in research and engineering to ensure our AI systems are safe and secure. However, as with any "
474+
},
475+
"type": "text"
476+
},
477+
{
478+
"plain_text": "complex technology",
479+
"text": {
480+
"content": "complex technology"
481+
},
482+
"type": "text",
483+
"annotations": {
484+
"color": "purple",
485+
"bold": True
486+
}
487+
},
488+
{
489+
"plain_text": ", we understand that vulnerabilities and flaws can emerge.",
490+
"text": {
491+
"content": ", we understand that vulnerabilities and flaws can emerge."
492+
},
493+
"type": "text"
494+
}
495+
],
496+
"language": "plain text"
497+
}
498+
}
499+
]
500+
411501
language_code_content = '<div style="--en-codeblock:true;--en-codeblockLanguage:python;">import os\nprint("hello")</div>'
412502
language_code_block = [
413503
{
@@ -441,6 +531,7 @@ def test_convert():
441531
paragram_rich_content: paragram_rich_block,
442532
heading_content: heading_block,
443533
code_content: code_block,
534+
code_paragraph_content: code_paragraph_block,
444535
language_code_content: language_code_block
445536
}
446537

@@ -449,8 +540,8 @@ def test_convert():
449540
yinxiang = Html2JsonYinXiang(body_content)
450541
yinxiang.process()
451542
json_obj = yinxiang.children
452-
print(json.dumps(json_obj, indent=4))
453-
# assert json_obj == html_jsons[html_content]
543+
# print(json.dumps(json_obj, indent=4))
544+
assert json_obj == html_jsons[html_content]
454545

455546

456547
if __name__ == '__main__':

0 commit comments

Comments
 (0)