Skip to content

Merge blocks with same type together and handle Notion limits on children and text length #7

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions config.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ import_media=true
[notion]
token=Copy it from Notion cookies: token_v2
root_url=https://www.notion.so/PAGE-ID Create a root url in your Notion
merge_paragraphs=False
25 changes: 20 additions & 5 deletions gkeep2notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def add_chunk(self, text: str, url: str = ''):
self._chunks.append({
"type": "text",
"text": {
"content": text
"content": text
}
})

Expand Down Expand Up @@ -180,6 +180,7 @@ def __init__(self, ini: ConfigParser):
self.import_media = ini['gkeep']['import_media'].lower() == 'true'
self.token = ini['notion']['token']
self.root_url = ini['notion']['root_url']
self.merge_paragraphs = ini['notion']['merge_paragraphs']


def get_config(path='config.ini') -> Config:
Expand Down Expand Up @@ -262,12 +263,26 @@ def parseBlock(p: str) -> dict:
}


def parseTextToPage(text: str, page: Page):
def parseTextToPage(text: str, page: Page, config: Config):
lines = text.splitlines()
lines.insert(len(lines), '')
last_block = None
print(f"Parsing {len(lines)} blocks")
for p in lines:
for x in range(0, len(lines)):
p = lines[x]
block = parseBlock(p)
page.add_text(block['text'], block['type'])
if not config.merge_paragraphs:
page.add_text(block['text'], block['type'])
continue
if last_block:
if last_block['type'] == BlockType.Paragraph and last_block['type'] == block['type'] and len(
last_block['text']) + len(block['text']) < 2000:
last_block['text'] += "\n" + block['text']
if x < len(lines) - 1:
continue
page.add_text(last_block['text'], last_block['type'])
last_block = block



def getNoteCategories(note: node.TopLevelNode) -> list[str]:
Expand Down Expand Up @@ -331,7 +346,7 @@ def parseNote(note: node.TopLevelNode, page: Page, keep: Keep, config: Config):
# Text
text = note.text
# Render page blocks
parseTextToPage(text, page)
parseTextToPage(text, page, config)


def parseList(list: node.List, page: Page):
Expand Down