notionhelper 0.4.0__py3-none-any.whl → 0.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
notionhelper/helper.py
CHANGED
|
@@ -174,8 +174,118 @@ class NotionHelper:
|
|
|
174
174
|
response = self._make_request("POST", url, payload)
|
|
175
175
|
return response.get("results", [])
|
|
176
176
|
|
|
177
|
-
def
|
|
178
|
-
"""
|
|
177
|
+
def _blocks_to_markdown(self, blocks: List[Dict[str, Any]]) -> str:
|
|
178
|
+
"""Converts Notion blocks to markdown format.
|
|
179
|
+
|
|
180
|
+
Parameters:
|
|
181
|
+
blocks (list): List of block objects from Notion API
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
str: Markdown formatted string
|
|
185
|
+
"""
|
|
186
|
+
markdown_lines = []
|
|
187
|
+
|
|
188
|
+
for block in blocks:
|
|
189
|
+
block_type = block.get("type", "")
|
|
190
|
+
block_data = block.get(block_type, {})
|
|
191
|
+
|
|
192
|
+
if block_type == "paragraph":
|
|
193
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
194
|
+
if text:
|
|
195
|
+
markdown_lines.append(text)
|
|
196
|
+
markdown_lines.append("")
|
|
197
|
+
|
|
198
|
+
elif block_type == "heading_1":
|
|
199
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
200
|
+
markdown_lines.append(f"# {text}")
|
|
201
|
+
markdown_lines.append("")
|
|
202
|
+
|
|
203
|
+
elif block_type == "heading_2":
|
|
204
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
205
|
+
markdown_lines.append(f"## {text}")
|
|
206
|
+
markdown_lines.append("")
|
|
207
|
+
|
|
208
|
+
elif block_type == "heading_3":
|
|
209
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
210
|
+
markdown_lines.append(f"### {text}")
|
|
211
|
+
markdown_lines.append("")
|
|
212
|
+
|
|
213
|
+
elif block_type == "bulleted_list_item":
|
|
214
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
215
|
+
markdown_lines.append(f"- {text}")
|
|
216
|
+
|
|
217
|
+
elif block_type == "numbered_list_item":
|
|
218
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
219
|
+
markdown_lines.append(f"1. {text}")
|
|
220
|
+
|
|
221
|
+
elif block_type == "code":
|
|
222
|
+
code_text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
223
|
+
language = block_data.get("language", "")
|
|
224
|
+
markdown_lines.append(f"```{language}")
|
|
225
|
+
markdown_lines.append(code_text)
|
|
226
|
+
markdown_lines.append("```")
|
|
227
|
+
markdown_lines.append("")
|
|
228
|
+
|
|
229
|
+
elif block_type == "image":
|
|
230
|
+
image_data = block_data.get("external", {}) or block_data.get("file", {})
|
|
231
|
+
image_url = image_data.get("url", "")
|
|
232
|
+
if image_url:
|
|
233
|
+
markdown_lines.append(f"")
|
|
234
|
+
markdown_lines.append("")
|
|
235
|
+
|
|
236
|
+
elif block_type == "divider":
|
|
237
|
+
markdown_lines.append("---")
|
|
238
|
+
markdown_lines.append("")
|
|
239
|
+
|
|
240
|
+
elif block_type == "quote":
|
|
241
|
+
text = self._extract_rich_text(block_data.get("rich_text", []))
|
|
242
|
+
markdown_lines.append(f"> {text}")
|
|
243
|
+
markdown_lines.append("")
|
|
244
|
+
|
|
245
|
+
return "\n".join(markdown_lines).strip()
|
|
246
|
+
|
|
247
|
+
def _extract_rich_text(self, rich_text_array: List[Dict[str, Any]]) -> str:
|
|
248
|
+
"""Extracts and formats rich text from Notion rich_text array.
|
|
249
|
+
|
|
250
|
+
Parameters:
|
|
251
|
+
rich_text_array (list): Array of rich text objects
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
str: Formatted text with markdown syntax
|
|
255
|
+
"""
|
|
256
|
+
result = []
|
|
257
|
+
|
|
258
|
+
for text_obj in rich_text_array:
|
|
259
|
+
content = text_obj.get("text", {}).get("content", "")
|
|
260
|
+
annotations = text_obj.get("annotations", {})
|
|
261
|
+
href = text_obj.get("href", None)
|
|
262
|
+
|
|
263
|
+
# Apply markdown formatting based on annotations
|
|
264
|
+
if annotations.get("bold"):
|
|
265
|
+
content = f"**{content}**"
|
|
266
|
+
if annotations.get("italic"):
|
|
267
|
+
content = f"*{content}*"
|
|
268
|
+
if annotations.get("strikethrough"):
|
|
269
|
+
content = f"~~{content}~~"
|
|
270
|
+
if annotations.get("code"):
|
|
271
|
+
content = f"`{content}`"
|
|
272
|
+
if href:
|
|
273
|
+
content = f"[{content}]({href})"
|
|
274
|
+
|
|
275
|
+
result.append(content)
|
|
276
|
+
|
|
277
|
+
return "".join(result)
|
|
278
|
+
|
|
279
|
+
def get_page(self, page_id: str, return_markdown: bool = False) -> Dict[str, Any]:
|
|
280
|
+
"""Retrieves the JSON of the page properties and an array of blocks on a Notion page given its page_id.
|
|
281
|
+
|
|
282
|
+
Parameters:
|
|
283
|
+
page_id (str): The ID of the Notion page
|
|
284
|
+
return_markdown (bool): If True, converts blocks to markdown. If False, returns raw JSON. Defaults to False.
|
|
285
|
+
|
|
286
|
+
Returns:
|
|
287
|
+
dict: Dictionary with 'properties' and 'content' (as JSON or markdown string)
|
|
288
|
+
"""
|
|
179
289
|
|
|
180
290
|
# Retrieve the page properties
|
|
181
291
|
page_url = f"https://api.notion.com/v1/pages/{page_id}"
|
|
@@ -187,10 +297,13 @@ class NotionHelper:
|
|
|
187
297
|
|
|
188
298
|
# Extract all properties as a JSON object
|
|
189
299
|
properties = page.get("properties", {})
|
|
190
|
-
|
|
300
|
+
content_blocks = [block for block in blocks["results"]]
|
|
191
301
|
|
|
192
|
-
#
|
|
193
|
-
|
|
302
|
+
# Convert to markdown if requested
|
|
303
|
+
if return_markdown:
|
|
304
|
+
content = self._blocks_to_markdown(content_blocks)
|
|
305
|
+
else:
|
|
306
|
+
content = content_blocks
|
|
194
307
|
|
|
195
308
|
# Return the properties JSON and blocks content
|
|
196
309
|
return {"properties": properties, "content": content}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: notionhelper
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.2
|
|
4
4
|
Summary: NotionHelper is a Python library that simplifies interactions with the Notion API, enabling easy management of databases, pages, and files within Notion workspaces.
|
|
5
5
|
Author-email: Jan du Plessis <drjanduplessis@icloud.com>
|
|
6
6
|
Requires-Python: >=3.10
|
|
@@ -168,6 +168,44 @@ helper.append_page_body(page_id, blocks)
|
|
|
168
168
|
print(f"Successfully appended content to page ID: {page_id}")
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
+
### Retrieve a Page and Convert to Markdown
|
|
172
|
+
|
|
173
|
+
NotionHelper can retrieve page content and optionally convert it to markdown format for easy use in documents, blogs, or other applications.
|
|
174
|
+
|
|
175
|
+
#### Get Page as JSON (Default)
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
page_id = "your_page_id"
|
|
179
|
+
result = helper.get_page(page_id)
|
|
180
|
+
properties = result["properties"] # Page properties
|
|
181
|
+
content = result["content"] # List of block objects (JSON)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Get Page as Markdown
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
page_id = "your_page_id"
|
|
188
|
+
result = helper.get_page(page_id, return_markdown=True)
|
|
189
|
+
properties = result["properties"] # Page properties
|
|
190
|
+
markdown_content = result["content"] # String in markdown format
|
|
191
|
+
print(markdown_content)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The markdown conversion supports:
|
|
195
|
+
- **Headings** (H1, H2, H3)
|
|
196
|
+
- **Text formatting** (bold, italic, strikethrough, code, links)
|
|
197
|
+
- **Lists** (bulleted and numbered)
|
|
198
|
+
- **Code blocks** with language syntax highlighting
|
|
199
|
+
- **Images**
|
|
200
|
+
- **Dividers** and block quotes
|
|
201
|
+
|
|
202
|
+
This is useful for:
|
|
203
|
+
- Exporting Notion pages to markdown files
|
|
204
|
+
- Integrating with static site generators
|
|
205
|
+
- Creating blog posts from Notion content
|
|
206
|
+
- Storing content in version control
|
|
207
|
+
- Converting documentation to other formats
|
|
208
|
+
|
|
171
209
|
### Get all pages from a Data Source as a Pandas DataFrame
|
|
172
210
|
|
|
173
211
|
```python
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
notionhelper/__init__.py,sha256=lidq1uQioToT1ePIu02XktGYTPMp_uY5Btw63GlNqOo,117
|
|
2
|
+
notionhelper/helper.py,sha256=s2d2chVsQTFeE9zyn6onfVms21jpVstKENQEu1jhKaY,33007
|
|
3
|
+
notionhelper/ml_logger.py,sha256=ex8Zy7reorLy5sOEfbDVZPwY2lsERJTTaJf3Ybj_vyg,8658
|
|
4
|
+
notionhelper-0.4.2.dist-info/METADATA,sha256=uGLMJ2UGAEFO0UfE-q8W5-BLiQS625z1HUsMpKaZoeM,21227
|
|
5
|
+
notionhelper-0.4.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
notionhelper-0.4.2.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
notionhelper/__init__.py,sha256=lidq1uQioToT1ePIu02XktGYTPMp_uY5Btw63GlNqOo,117
|
|
2
|
-
notionhelper/helper.py,sha256=6--mjtsgHDnP-rF1nEZ30CFEs15864un4xmQPa1_tuk,28650
|
|
3
|
-
notionhelper/ml_logger.py,sha256=ex8Zy7reorLy5sOEfbDVZPwY2lsERJTTaJf3Ybj_vyg,8658
|
|
4
|
-
notionhelper-0.4.0.dist-info/METADATA,sha256=YJ3su15m58HMp-gj2Tqf7Z3bTjCAG29cQfcycnt77dA,20058
|
|
5
|
-
notionhelper-0.4.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
-
notionhelper-0.4.0.dist-info/RECORD,,
|
|
File without changes
|