Skip to content

Commit

Permalink
fix: Fixed echo724#40 due to the update of Notion API (echo724#42)
Browse files Browse the repository at this point in the history
* fix: Fixed MarkdownExporter makes markdown file even if unzipped flag is on

* refactor: Moved CLIExporter to exporter directory

* chore: Update dependency requirement of the version of Notion-client 1.0.1

* fix: Fixed parsing 'text' error due to update of Notion API
  • Loading branch information
echo724 committed Oct 14, 2022
1 parent 30e8f0b commit 1a63351
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
14 changes: 1 addition & 13 deletions notion2md/console/commands/export_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@
from notion2md.console.formatter import status
from notion2md.console.formatter import success
from notion2md.console.ui.indicator import progress
from notion2md.exporter.block import Exporter


class CLIExporter(Exporter):
def export(self, blocks):
with open(
os.path.join(
self._config.tmp_path, self._config.file_name + ".md"
),
"w",
encoding="utf-8",
) as output:
output.write(self.block_convertor.convert(blocks))
from notion2md.exporter.block import CLIExporter


ARGS_NEW_KEY_MAP = {
Expand Down
64 changes: 36 additions & 28 deletions notion2md/convertor/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,44 @@ def convert_block(self, block: dict, depth=0) -> str:
if (
block_type == "paragraph"
and not block["has_children"]
and not block[block_type]["text"]
and not block[block_type]["rich_text"]
):
return blank() + "\n\n"
# Normal Case
if block_type in BLOCK_TYPES:
outcome_block = (
BLOCK_TYPES[block_type](self.collect_info(block[block_type]))
+ "\n\n"
)
else:
outcome_block = f"[//]: # ({block_type} is not supported)\n\n"
# Convert child block
if block["has_children"]:
# create child page
if block_type == "child_page":
# call make_child_function
pass
# create table block
elif block_type == "table":
depth += 1
child_blocks = self._client.get_children(block["id"])
outcome_block = self.create_table(cell_blocks=child_blocks)
# create indent block
else:
depth += 1
child_blocks = self._client.get_children(block["id"])
for block in child_blocks:
outcome_block += "\t" * depth + self.convert_block(
block, depth
try:
if block_type in BLOCK_TYPES:
outcome_block = (
BLOCK_TYPES[block_type](
self.collect_info(block[block_type])
)
+ "\n\n"
)
else:
outcome_block = f"[//]: # ({block_type} is not supported)\n\n"
# Convert child block
if block["has_children"]:
# create child page
if block_type == "child_page":
# call make_child_function
pass
# create table block
elif block_type == "table":
depth += 1
child_blocks = self._client.get_children(block["id"])
outcome_block = self.create_table(cell_blocks=child_blocks)
# create indent block
else:
depth += 1
child_blocks = self._client.get_children(block["id"])
for block in child_blocks:
outcome_block += "\t" * depth + self.convert_block(
block, depth
)
except Exception as e:
if self._io:
self._io.write_line(
error(f"{e}: Error occured block_type:{block_type}")
)
return outcome_block

def create_table(self, cell_blocks: dict):
Expand All @@ -103,8 +111,8 @@ def create_table(self, cell_blocks: dict):

def collect_info(self, payload: dict) -> dict:
info = dict()
if "text" in payload:
info["text"] = richtext_convertor(payload["text"])
if "rich_text" in payload:
info["text"] = richtext_convertor(payload["rich_text"])
if "icon" in payload:
info["icon"] = payload["icon"]["emoji"]
if "checked" in payload:
Expand Down
14 changes: 13 additions & 1 deletion notion2md/exporter/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def export(self):
self.create_directories()
with open(
os.path.join(
self._config.output_path, self._config.file_name + ".md"
self._config.tmp_path, self._config.file_name + ".md"
),
"w",
encoding="utf-8",
Expand All @@ -88,3 +88,15 @@ def export(self):
class StringExporter(Exporter):
def export(self):
return self.block_convertor.to_string(self.get_blocks())


class CLIExporter(Exporter):
def export(self, blocks):
with open(
os.path.join(
self._config.tmp_path, self._config.file_name + ".md"
),
"w",
encoding="utf-8",
) as output:
output.write(self.block_convertor.convert(blocks))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exclude = ["Test/test_*"]

[tool.poetry.dependencies]
python = ">=3.7, <4"
notion-client = ">=0.7.1"
notion-client = ">=1.0.1"
cleo = "1.0.0a4"

[tool.poetry.scripts]
Expand Down

0 comments on commit 1a63351

Please sign in to comment.