Skip to content

Commit

Permalink
fix: fix numbered list format according to commonmark format (#58)
Browse files Browse the repository at this point in the history
* test: fix numbered list

* fix: numbered list requires local state

* fix: change numbered list format according to commonmark

from this change the output of numbered list will be:
```
1. list1
1. list2
```
even if the number list is sequenced.

---------

Co-authored-by: Geoff Cox <redgeoff@gmail.com>
  • Loading branch information
echo724 and redgeoff committed Dec 28, 2023
1 parent 13c7b79 commit 305ec68
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/semantic_release.yaml
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-python@v2
- name: Install poetry
run: pip install poetry
- uses: bjoluc/semantic-release-config-poetry@v1
- uses: bjoluc/semantic-release-config-poetry@v2
with:
pypi_token: ${{ secrets.PYPI_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
8 changes: 7 additions & 1 deletion CONTRIBUTION.md
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
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

0 comments on commit 305ec68

Please sign in to comment.