From e1ad71140218a964528b53769652ad26fda20f5b Mon Sep 17 00:00:00 2001 From: Waleed Gadelkareem Date: Tue, 4 Oct 2022 03:26:35 +0200 Subject: [PATCH 1/5] Merge blocks with same type together and handle limits on children and text length --- gkeep2notion.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gkeep2notion.py b/gkeep2notion.py index e3d23a8..5b4ba3a 100755 --- a/gkeep2notion.py +++ b/gkeep2notion.py @@ -264,10 +264,20 @@ def parseBlock(p: str) -> dict: def parseTextToPage(text: str, page: Page): lines = text.splitlines() - print(f"Parsing {len(lines)} blocks") - for p in lines: + lines.insert(len(lines), '') + last_block = None + + for x in range(0, len(lines)): + p = lines[x] block = parseBlock(p) - page.add_text(block['text'], block['type']) + if last_block: + if 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]: From 948f31619109596f65ff7842587c387a3777d4ad Mon Sep 17 00:00:00 2001 From: Waleed Gadelkareem Date: Tue, 4 Oct 2022 03:28:54 +0200 Subject: [PATCH 2/5] Update gkeep2notion.py --- gkeep2notion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gkeep2notion.py b/gkeep2notion.py index 5b4ba3a..9f90abf 100755 --- a/gkeep2notion.py +++ b/gkeep2notion.py @@ -266,7 +266,7 @@ def parseTextToPage(text: str, page: Page): lines = text.splitlines() lines.insert(len(lines), '') last_block = None - + print(f"Parsing {len(lines)} blocks") for x in range(0, len(lines)): p = lines[x] block = parseBlock(p) From 0b513f022c2a451ed365c8870a7da39b5eb80aa7 Mon Sep 17 00:00:00 2001 From: Waleed Gadelkareem Date: Mon, 10 Oct 2022 16:57:42 +0200 Subject: [PATCH 3/5] limit merging blocks to text type --- gkeep2notion.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gkeep2notion.py b/gkeep2notion.py index 9f90abf..68e8658 100755 --- a/gkeep2notion.py +++ b/gkeep2notion.py @@ -86,7 +86,7 @@ def add_chunk(self, text: str, url: str = ''): self._chunks.append({ "type": "text", "text": { - "content": text + "content": text } }) @@ -271,7 +271,8 @@ def parseTextToPage(text: str, page: Page): p = lines[x] block = parseBlock(p) if last_block: - if last_block['type'] == block['type'] and len(last_block['text']) + len(block['text']) < 2000: + if last_block['type'] == 'text' 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 From 8079f9c84b866933bab257df059b4b4776f89165 Mon Sep 17 00:00:00 2001 From: Waleed Gadelkareem Date: Thu, 20 Oct 2022 17:53:15 +0200 Subject: [PATCH 4/5] make merge text optional --- config.example.ini | 1 + gkeep2notion.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/config.example.ini b/config.example.ini index e616169..5536fba 100644 --- a/config.example.ini +++ b/config.example.ini @@ -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 \ No newline at end of file diff --git a/gkeep2notion.py b/gkeep2notion.py index 68e8658..354adb8 100755 --- a/gkeep2notion.py +++ b/gkeep2notion.py @@ -262,7 +262,7 @@ 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 @@ -270,9 +270,12 @@ def parseTextToPage(text: str, page: Page): for x in range(0, len(lines)): p = lines[x] block = parseBlock(p) + if not config.merge_paragraphs: + page.add_text(block['text'], block['type']) + continue if last_block: - if last_block['type'] == 'text' and last_block['type'] == block['type'] and len(last_block['text']) + len( - block['text']) < 2000: + 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 @@ -342,7 +345,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): From 116f0a9a236f833b7a2032d2673919b6ffce0c79 Mon Sep 17 00:00:00 2001 From: Waleed Gadelkareem Date: Thu, 20 Oct 2022 21:37:42 +0200 Subject: [PATCH 5/5] add config for merge_paragraphs --- gkeep2notion.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gkeep2notion.py b/gkeep2notion.py index 354adb8..e14d9cd 100755 --- a/gkeep2notion.py +++ b/gkeep2notion.py @@ -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: