|
1 | 1 | from md2notionpage.core import parse_md |
2 | 2 | import os |
3 | 3 | from notion_client import Client |
| 4 | +from anthropic import Anthropic |
4 | 5 |
|
5 | 6 | # load_dotenv() |
6 | | -os.environ["NOTION_SECRET"] = "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" |
7 | | -notion = Client(auth=os.environ["NOTION_SECRET"]) |
8 | | - |
9 | | -markdown_text = """ |
10 | | -Sure! Here's the entire explanation formatted properly in Markdown: |
11 | | -
|
12 | | ---- |
13 | | -
|
14 | | -# 🧠 Using Notional to Convert Markdown or HTML to Notion Pages |
15 | | -
|
16 | | -**Notional** is a powerful and Pythonic SDK for the Notion API. While it doesn't natively convert Markdown or HTML, it integrates well with other tools that handle this parsing for you. |
17 | | -
|
18 | | ---- |
19 | | -
|
20 | | -## 🧰 Recommended Tools for Markdown/HTML to Notion Conversion |
21 | | -
|
22 | | -| Package | Description | Notes | |
23 | | -| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------- | |
24 | | -| [`md2notionpage`](https://pypi.org/project/md2notionpage/) | Converts Markdown text into Notion pages, handling inline formatting and block structures. | Simplifies the process of creating Notion pages from Markdown. | |
25 | | -| [`html2notion`](https://github.com/selfboot/html2notion) | Transforms HTML content into Notion blocks, supporting various HTML tags. | Useful for importing HTML documents into Notion. | |
26 | | -| [`md2notion`](https://pypi.org/project/md2notion/) | Imports Markdown files into Notion, preserving formatting and structure. | Requires `notion-py`, which may have compatibility issues with the latest Notion API. | |
27 | | -
|
28 | | ---- |
29 | | -
|
30 | | -## 🧑💻 Example: Using `md2notionpage` with Notional |
31 | | -
|
32 | | -### 1. Install Required Packages |
33 | | -
|
34 | | -```bash |
35 | | -pip install notional md2notionpage |
36 | | -``` |
37 | | -
|
38 | | -### 2. Python Script |
39 | | -
|
40 | | -```python |
41 | | -import os |
42 | | -from notional import Notion |
43 | | -from md2notionpage import md2notionpage |
44 | | -
|
45 | | -# Initialize Notion client with your integration token |
46 | | -notion = Notion(auth=os.environ["NOTION_TOKEN"]) |
47 | | -
|
48 | | -# Read Markdown content from a file |
49 | | -with open("example.md", "r", encoding="utf-8") as md_file: |
50 | | - markdown_content = md_file.read() |
51 | | -
|
52 | | -# Convert Markdown to Notion blocks |
53 | | -notion_blocks = md2notionpage(markdown_content) |
54 | | -
|
55 | | -# Create a new page in your workspace |
56 | | -new_page = notion.pages.create( |
57 | | - parent={"type": "page_id", "page_id": os.environ["NOTION_PARENT_PAGE_ID"]}, |
58 | | - properties={ |
59 | | - "title": [ |
60 | | - { |
61 | | - "type": "text", |
62 | | - "text": {"content": "Imported Markdown Page"} |
63 | | - } |
64 | | - ] |
65 | | - }, |
66 | | - children=notion_blocks |
67 | | -) |
68 | | -
|
69 | | -print(f"Page created: {new_page['url']}") |
70 | | -``` |
71 | | -
|
72 | | -> **Note:** Ensure that you set the following environment variables: |
73 | | -> |
74 | | -> * `NOTION_TOKEN`: your Notion integration token |
75 | | -> * `NOTION_PARENT_PAGE_ID`: the ID of the parent page or database |
76 | | -
|
77 | | ---- |
78 | | -
|
79 | | -## 🔍 Summary |
80 | | -
|
81 | | -While **Notional** is excellent for interacting with the Notion API, combining it with tools like `md2notionpage` or `html2notion` enables seamless conversion of Markdown or HTML content into Notion pages. |
82 | | -
|
83 | | -These tools handle the parsing and block formatting for you, letting you focus on the actual content creation. |
84 | | -
|
85 | | ---- |
86 | | -
|
87 | | -Would you like me to provide a full working GitHub Gist or a CLI tool based on this? |
88 | | -
|
89 | | -""" |
90 | | - |
91 | | -title = "Markdown to Notion Page" |
92 | | - |
93 | | -parent_page_id = "1ec84ade96ac803bbe86e258a017466b" |
94 | | - |
95 | | -created_page = notion.pages.create( |
96 | | - parent={"type": "page_id", "page_id": parent_page_id}, properties={}, children=[] |
97 | | -) |
98 | | - |
99 | | -# notion_page_url = md2notionpage(markdown_text, title, parent_page_id) |
100 | | -notion.pages.update( |
101 | | - created_page["id"], |
102 | | - properties={"title": {"title": [{"type": "text", "text": {"content": title}}]}}, |
103 | | -) |
104 | | - |
105 | | -# Iterate through the parsed Markdown blocks and append them to the created page |
106 | | -for block in parse_md(markdown_text): |
107 | | - notion.blocks.children.append(created_page["id"], children=[block]) |
| 7 | +notion = Client(auth=os.environ["NOTION_TOKEN"]) |
| 8 | +anthropic = Anthropic() |
| 9 | + |
| 10 | +while True: |
| 11 | + query = input("Enter a query: ") |
| 12 | + if query == "": |
| 13 | + print("Goodbye!") |
| 14 | + break |
| 15 | + messages = [{"role": "user", "content": query}] |
| 16 | + |
| 17 | + # Initial Claude API call |
| 18 | + response = anthropic.messages.create( |
| 19 | + model="claude-3-5-haiku-20241022", |
| 20 | + max_tokens=1000, |
| 21 | + messages=messages, |
| 22 | + ) |
| 23 | + |
| 24 | + markdown_text = response.content[0].text |
| 25 | + print(f"Markdown text: {markdown_text}") |
| 26 | + |
| 27 | + parent_page_id = "1ec84ade96ac803bbe86e258a017466b" |
| 28 | + |
| 29 | + created_page = notion.pages.create( |
| 30 | + parent={"type": "page_id", "page_id": parent_page_id}, |
| 31 | + properties={}, |
| 32 | + children=[], |
| 33 | + ) |
| 34 | + |
| 35 | + # notion_page_url = md2notionpage(markdown_text, title, parent_page_id) |
| 36 | + notion.pages.update( |
| 37 | + created_page["id"], |
| 38 | + properties={"title": {"title": [{"type": "text", "text": {"content": query}}]}}, |
| 39 | + ) |
| 40 | + |
| 41 | + # Iterate through the parsed Markdown blocks and append them to the created page |
| 42 | + for block in parse_md(markdown_text): |
| 43 | + notion.blocks.children.append(created_page["id"], children=[block]) |
0 commit comments