Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix numbered list format according to commonmark format #58

Merged
merged 3 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ Pull requests are welcome.
2. make changes and push to your repo
3. send pull request from your **develop** branch to this develop branch

**This is only way to give pull request to this repo. Thank you**
**This is only way to give pull request to this repo. Thank you**

Please make sure that you do the following before submitting your code:
1. Run the tests: `poetry run python -m unittest discover tests`
2. Format the code `poetry run black .`
3. Use isort the code `poetry run isort .`
4. Lint the code `poetry run flake8 .`
53 changes: 24 additions & 29 deletions notion2md/convertor/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import hashlib
import os
import urllib.request as request
import uuid

from urllib.parse import urlparse

from cleo.io.io import IO
Expand All @@ -13,7 +11,6 @@
from notion2md.console.formatter import status
from notion2md.console.formatter import success
from notion2md.notion_api import NotionClient

from .richtext import richtext_convertor


Expand All @@ -22,8 +19,6 @@ def __init__(self, config: Config, client: NotionClient, io: IO = None):
self._config = config
self._client = client
self._io = io
self._continued_numbered_list = False
self._numbered_list_number = 1

def convert(self, blocks: dict) -> str:
outcome_blocks: str = ""
Expand All @@ -32,33 +27,24 @@ def convert(self, blocks: dict) -> str:
outcome_blocks = "".join([result for result in results])
return outcome_blocks

def convert_block(self, block: dict, depth=0) -> str:
def convert_block(
self,
block: dict,
depth=0,
):
outcome_block: str = ""
block_type = block["type"]
# Handle the case where the block is a list item
if block_type == "numbered_list_item":
if self._continued_numbered_list:
self._numbered_list_number += 1
else:
self._continued_numbered_list = True
self._numbered_list_number = 1
else:
self._continued_numbered_list = False
# Special Case: Block is blank
if (
block_type == "paragraph"
and not block["has_children"]
and not block[block_type]["rich_text"]
):
if check_block_is_blank(block, block_type):
return blank() + "\n\n"
# Normal Case
try:
if block_type in BLOCK_TYPES:
outcome_block = (
BLOCK_TYPES[block_type](
self.collect_info(block[block_type])
)
+ "\n\n"
BLOCK_TYPES[block_type](
self.collect_info(block[block_type])
)
+ "\n\n"
)
else:
outcome_block = f"[//]: # ({block_type} is not supported)\n\n"
Expand All @@ -78,9 +64,11 @@ def convert_block(self, block: dict, depth=0) -> str:
depth += 1
child_blocks = self._client.get_children(block["id"])
for block in child_blocks:
outcome_block += "\t" * depth + self.convert_block(
block, depth
converted_block = self.convert_block(
block,
depth,
)
outcome_block += "\t" * depth + converted_block
except Exception as e:
if self._io:
self._io.write_line(
Expand All @@ -102,7 +90,7 @@ def create_table(self, cell_blocks: dict):
if index == 0:
table = " | " + " | ".join(value) + " | " + "\n"
table += (
" | " + " | ".join(["----"] * len(value)) + " | " + "\n"
" | " + " | ".join(["----"] * len(value)) + " | " + "\n"
)
continue
table += " | " + " | ".join(value) + " | " + "\n"
Expand Down Expand Up @@ -139,7 +127,6 @@ def collect_info(self, payload: dict) -> dict:
# table cells
if "cells" in payload:
info["cells"] = payload["cells"]
info["number"] = self._numbered_list_number
return info

def download_file(self, url: str) -> str:
Expand Down Expand Up @@ -182,6 +169,14 @@ def to_string(self, blocks: dict) -> str:
return self.convert(blocks)


def check_block_is_blank(block, block_type):
return (
block_type == "paragraph"
and not block["has_children"]
and not block[block_type]["rich_text"]
)


# Converting Methods
def paragraph(info: dict) -> str:
return info["text"]
Expand Down Expand Up @@ -217,7 +212,7 @@ def numbered_list_item(info: dict) -> str:
"""
input: item:dict = {"number":int, "text":str}
"""
return f"{info['number']}. {info['text']}"
return f"1. {info['text']}"


def to_do(info: dict) -> str:
Expand Down
Loading
Loading