arcade-notion-toolkit 0.1.6__tar.gz
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.
- arcade_notion_toolkit-0.1.6/.gitignore +175 -0
- arcade_notion_toolkit-0.1.6/.pre-commit-config.yaml +18 -0
- arcade_notion_toolkit-0.1.6/.ruff.toml +44 -0
- arcade_notion_toolkit-0.1.6/LICENSE +21 -0
- arcade_notion_toolkit-0.1.6/Makefile +55 -0
- arcade_notion_toolkit-0.1.6/PKG-INFO +19 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/__init__.py +0 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/block_to_markdown_converter.py +201 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/constants.py +17 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/enums.py +45 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/markdown_to_block_converter.py +158 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/tools/__init__.py +19 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/tools/pages.py +152 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/tools/search.py +225 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/types.py +105 -0
- arcade_notion_toolkit-0.1.6/arcade_notion_toolkit/utils.py +225 -0
- arcade_notion_toolkit-0.1.6/conftest.py +8 -0
- arcade_notion_toolkit-0.1.6/evals/eval_notion_pages.py +255 -0
- arcade_notion_toolkit-0.1.6/evals/eval_notion_search.py +254 -0
- arcade_notion_toolkit-0.1.6/pyproject.toml +56 -0
- arcade_notion_toolkit-0.1.6/tests/__init__.py +0 -0
- arcade_notion_toolkit-0.1.6/tests/test_block_to_markdown_converter.py +163 -0
- arcade_notion_toolkit-0.1.6/tests/test_tools_pages.py +169 -0
- arcade_notion_toolkit-0.1.6/tests/test_tools_search.py +6 -0
- arcade_notion_toolkit-0.1.6/tests/test_utils.py +86 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
.DS_Store
|
|
2
|
+
credentials.yaml
|
|
3
|
+
docker/credentials.yaml
|
|
4
|
+
|
|
5
|
+
*.lock
|
|
6
|
+
|
|
7
|
+
# example data
|
|
8
|
+
examples/data
|
|
9
|
+
scratch
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
docs/source
|
|
13
|
+
|
|
14
|
+
# From https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore
|
|
15
|
+
|
|
16
|
+
# Byte-compiled / optimized / DLL files
|
|
17
|
+
__pycache__/
|
|
18
|
+
*.py[cod]
|
|
19
|
+
*$py.class
|
|
20
|
+
|
|
21
|
+
# C extensions
|
|
22
|
+
*.so
|
|
23
|
+
|
|
24
|
+
# Distribution / packaging
|
|
25
|
+
.Python
|
|
26
|
+
build/
|
|
27
|
+
develop-eggs/
|
|
28
|
+
dist/
|
|
29
|
+
downloads/
|
|
30
|
+
eggs/
|
|
31
|
+
.eggs/
|
|
32
|
+
lib/
|
|
33
|
+
lib64/
|
|
34
|
+
parts/
|
|
35
|
+
sdist/
|
|
36
|
+
var/
|
|
37
|
+
wheels/
|
|
38
|
+
share/python-wheels/
|
|
39
|
+
*.egg-info/
|
|
40
|
+
.installed.cfg
|
|
41
|
+
*.egg
|
|
42
|
+
MANIFEST
|
|
43
|
+
|
|
44
|
+
# PyInstaller
|
|
45
|
+
# Usually these files are written by a python script from a template
|
|
46
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
47
|
+
*.manifest
|
|
48
|
+
*.spec
|
|
49
|
+
|
|
50
|
+
# Installer logs
|
|
51
|
+
pip-log.txt
|
|
52
|
+
pip-delete-this-directory.txt
|
|
53
|
+
|
|
54
|
+
# Unit test / coverage reports
|
|
55
|
+
htmlcov/
|
|
56
|
+
.tox/
|
|
57
|
+
.nox/
|
|
58
|
+
.coverage
|
|
59
|
+
.coverage.*
|
|
60
|
+
.cache
|
|
61
|
+
nosetests.xml
|
|
62
|
+
coverage.xml
|
|
63
|
+
*.cover
|
|
64
|
+
*.py,cover
|
|
65
|
+
.hypothesis/
|
|
66
|
+
.pytest_cache/
|
|
67
|
+
cover/
|
|
68
|
+
|
|
69
|
+
# Translations
|
|
70
|
+
*.mo
|
|
71
|
+
*.pot
|
|
72
|
+
|
|
73
|
+
# Django stuff:
|
|
74
|
+
*.log
|
|
75
|
+
local_settings.py
|
|
76
|
+
db.sqlite3
|
|
77
|
+
db.sqlite3-journal
|
|
78
|
+
|
|
79
|
+
# Flask stuff:
|
|
80
|
+
instance/
|
|
81
|
+
.webassets-cache
|
|
82
|
+
|
|
83
|
+
# Scrapy stuff:
|
|
84
|
+
.scrapy
|
|
85
|
+
|
|
86
|
+
# Sphinx documentation
|
|
87
|
+
docs/_build/
|
|
88
|
+
|
|
89
|
+
# PyBuilder
|
|
90
|
+
.pybuilder/
|
|
91
|
+
target/
|
|
92
|
+
|
|
93
|
+
# Jupyter Notebook
|
|
94
|
+
.ipynb_checkpoints
|
|
95
|
+
|
|
96
|
+
# IPython
|
|
97
|
+
profile_default/
|
|
98
|
+
ipython_config.py
|
|
99
|
+
|
|
100
|
+
# pyenv
|
|
101
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
102
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
103
|
+
# .python-version
|
|
104
|
+
|
|
105
|
+
# pipenv
|
|
106
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
107
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
108
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
109
|
+
# install all needed dependencies.
|
|
110
|
+
#Pipfile.lock
|
|
111
|
+
|
|
112
|
+
# poetry
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
114
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
115
|
+
# commonly ignored for libraries.
|
|
116
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
117
|
+
poetry.lock
|
|
118
|
+
|
|
119
|
+
# pdm
|
|
120
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
121
|
+
#pdm.lock
|
|
122
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
123
|
+
# in version control.
|
|
124
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
125
|
+
.pdm.toml
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.venv
|
|
140
|
+
env/
|
|
141
|
+
venv/
|
|
142
|
+
ENV/
|
|
143
|
+
env.bak/
|
|
144
|
+
venv.bak/
|
|
145
|
+
|
|
146
|
+
# Spyder project settings
|
|
147
|
+
.spyderproject
|
|
148
|
+
.spyproject
|
|
149
|
+
|
|
150
|
+
# Rope project settings
|
|
151
|
+
.ropeproject
|
|
152
|
+
|
|
153
|
+
# mkdocs documentation
|
|
154
|
+
/site
|
|
155
|
+
|
|
156
|
+
# mypy
|
|
157
|
+
.mypy_cache/
|
|
158
|
+
.dmypy.json
|
|
159
|
+
dmypy.json
|
|
160
|
+
|
|
161
|
+
# Pyre type checker
|
|
162
|
+
.pyre/
|
|
163
|
+
|
|
164
|
+
# pytype static type analyzer
|
|
165
|
+
.pytype/
|
|
166
|
+
|
|
167
|
+
# Cython debug symbols
|
|
168
|
+
cython_debug/
|
|
169
|
+
|
|
170
|
+
# PyCharm
|
|
171
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
172
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
173
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
174
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
175
|
+
#.idea/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
files: ^.*/notion/.*
|
|
2
|
+
repos:
|
|
3
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
4
|
+
rev: "v4.4.0"
|
|
5
|
+
hooks:
|
|
6
|
+
- id: check-case-conflict
|
|
7
|
+
- id: check-merge-conflict
|
|
8
|
+
- id: check-toml
|
|
9
|
+
- id: check-yaml
|
|
10
|
+
- id: end-of-file-fixer
|
|
11
|
+
- id: trailing-whitespace
|
|
12
|
+
|
|
13
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
14
|
+
rev: v0.6.7
|
|
15
|
+
hooks:
|
|
16
|
+
- id: ruff
|
|
17
|
+
args: [--fix]
|
|
18
|
+
- id: ruff-format
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
target-version = "py310"
|
|
2
|
+
line-length = 100
|
|
3
|
+
fix = true
|
|
4
|
+
|
|
5
|
+
[lint]
|
|
6
|
+
select = [
|
|
7
|
+
# flake8-2020
|
|
8
|
+
"YTT",
|
|
9
|
+
# flake8-bandit
|
|
10
|
+
"S",
|
|
11
|
+
# flake8-bugbear
|
|
12
|
+
"B",
|
|
13
|
+
# flake8-builtins
|
|
14
|
+
"A",
|
|
15
|
+
# flake8-comprehensions
|
|
16
|
+
"C4",
|
|
17
|
+
# flake8-debugger
|
|
18
|
+
"T10",
|
|
19
|
+
# flake8-simplify
|
|
20
|
+
"SIM",
|
|
21
|
+
# isort
|
|
22
|
+
"I",
|
|
23
|
+
# mccabe
|
|
24
|
+
"C90",
|
|
25
|
+
# pycodestyle
|
|
26
|
+
"E", "W",
|
|
27
|
+
# pyflakes
|
|
28
|
+
"F",
|
|
29
|
+
# pygrep-hooks
|
|
30
|
+
"PGH",
|
|
31
|
+
# pyupgrade
|
|
32
|
+
"UP",
|
|
33
|
+
# ruff
|
|
34
|
+
"RUF",
|
|
35
|
+
# tryceratops
|
|
36
|
+
"TRY",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[lint.per-file-ignores]
|
|
40
|
+
"**/tests/*" = ["S101"]
|
|
41
|
+
|
|
42
|
+
[format]
|
|
43
|
+
preview = true
|
|
44
|
+
skip-magic-trailing-comma = false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Arcade AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
.PHONY: help
|
|
2
|
+
|
|
3
|
+
help:
|
|
4
|
+
@echo "🛠️ github Commands:\n"
|
|
5
|
+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
|
6
|
+
|
|
7
|
+
.PHONY: install
|
|
8
|
+
install: ## Install the uv environment and install all packages with dependencies
|
|
9
|
+
@echo "🚀 Creating virtual environment and installing all packages using uv"
|
|
10
|
+
@uv sync --active --all-extras --no-sources
|
|
11
|
+
@if [ -f .pre-commit-config.yaml ]; then uv run --no-sources pre-commit install; fi
|
|
12
|
+
@echo "✅ All packages and dependencies installed via uv"
|
|
13
|
+
|
|
14
|
+
.PHONY: install-local
|
|
15
|
+
install-local: ## Install the uv environment and install all packages with dependencies with local Arcade sources
|
|
16
|
+
@echo "🚀 Creating virtual environment and installing all packages using uv"
|
|
17
|
+
@uv sync --active --all-extras
|
|
18
|
+
@if [ -f .pre-commit-config.yaml ]; then uv run pre-commit install; fi
|
|
19
|
+
@echo "✅ All packages and dependencies installed via uv"
|
|
20
|
+
|
|
21
|
+
.PHONY: build
|
|
22
|
+
build: clean-build ## Build wheel file using poetry
|
|
23
|
+
@echo "🚀 Creating wheel file"
|
|
24
|
+
uv build
|
|
25
|
+
|
|
26
|
+
.PHONY: clean-build
|
|
27
|
+
clean-build: ## clean build artifacts
|
|
28
|
+
@echo "🗑️ Cleaning dist directory"
|
|
29
|
+
rm -rf dist
|
|
30
|
+
|
|
31
|
+
.PHONY: test
|
|
32
|
+
test: ## Test the code with pytest
|
|
33
|
+
@echo "🚀 Testing code: Running pytest"
|
|
34
|
+
@uv run --no-sources pytest -W ignore -v --cov --cov-config=pyproject.toml --cov-report=xml
|
|
35
|
+
|
|
36
|
+
.PHONY: coverage
|
|
37
|
+
coverage: ## Generate coverage report
|
|
38
|
+
@echo "coverage report"
|
|
39
|
+
@uv run --no-sources coverage report
|
|
40
|
+
@echo "Generating coverage report"
|
|
41
|
+
@uv run --no-sources coverage html
|
|
42
|
+
|
|
43
|
+
.PHONY: bump-version
|
|
44
|
+
bump-version: ## Bump the version in the pyproject.toml file by a patch version
|
|
45
|
+
@echo "🚀 Bumping version in pyproject.toml"
|
|
46
|
+
uv version --no-sources --bump patch
|
|
47
|
+
|
|
48
|
+
.PHONY: check
|
|
49
|
+
check: ## Run code quality tools.
|
|
50
|
+
@if [ -f .pre-commit-config.yaml ]; then\
|
|
51
|
+
echo "🚀 Linting code: Running pre-commit";\
|
|
52
|
+
uv run --no-sources pre-commit run -a;\
|
|
53
|
+
fi
|
|
54
|
+
@echo "🚀 Static type checking: Running mypy"
|
|
55
|
+
@uv run --no-sources mypy --config-file=pyproject.toml
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: arcade_notion_toolkit
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary: Arcade.dev LLM tools for Notion
|
|
5
|
+
Author-email: Arcade <dev@arcade.dev>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Requires-Dist: arcade-tdk<3.0.0,>=2.0.0
|
|
9
|
+
Requires-Dist: httpx<1.0.0,>=0.27.2
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: arcade-ai[evals]<3.0.0,>=2.0.0; extra == 'dev'
|
|
12
|
+
Requires-Dist: arcade-serve<3.0.0,>=2.0.0; extra == 'dev'
|
|
13
|
+
Requires-Dist: mypy<1.6.0,>=1.5.1; extra == 'dev'
|
|
14
|
+
Requires-Dist: pre-commit<3.5.0,>=3.4.0; extra == 'dev'
|
|
15
|
+
Requires-Dist: pytest-asyncio<0.25.0,>=0.24.0; extra == 'dev'
|
|
16
|
+
Requires-Dist: pytest-cov<4.1.0,>=4.0.0; extra == 'dev'
|
|
17
|
+
Requires-Dist: pytest<8.4.0,>=8.3.0; extra == 'dev'
|
|
18
|
+
Requires-Dist: ruff<0.8.0,>=0.7.4; extra == 'dev'
|
|
19
|
+
Requires-Dist: tox<4.12.0,>=4.11.1; extra == 'dev'
|
|
File without changes
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from arcade_tdk import ToolContext
|
|
5
|
+
|
|
6
|
+
from arcade_notion_toolkit.enums import BlockType
|
|
7
|
+
from arcade_notion_toolkit.utils import get_page_url
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BlockToMarkdownConverter:
|
|
11
|
+
"""
|
|
12
|
+
A converter class that transforms Notion blocks into Markdown.
|
|
13
|
+
|
|
14
|
+
The class registers conversion handlers for different Notion block types.
|
|
15
|
+
If a block type does not have a handler, then the block's plain text is returned.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(self, context: ToolContext):
|
|
19
|
+
self.context = context
|
|
20
|
+
# block types whose conversion logic has been implemented
|
|
21
|
+
# TODO: implement conversion logic for more block types
|
|
22
|
+
self.handlers = {
|
|
23
|
+
BlockType.BULLETED_LIST_ITEM.value: self._convert_bulleted_list_item,
|
|
24
|
+
BlockType.EQUATION.value: self._convert_equation,
|
|
25
|
+
BlockType.HEADING_1.value: self._convert_heading_1,
|
|
26
|
+
BlockType.HEADING_2.value: self._convert_heading_2,
|
|
27
|
+
BlockType.HEADING_3.value: self._convert_heading_3,
|
|
28
|
+
BlockType.LINK_PREVIEW.value: self._convert_link_preview,
|
|
29
|
+
BlockType.NUMBERED_LIST_ITEM.value: self._convert_numbered_list_item,
|
|
30
|
+
BlockType.PARAGRAPH.value: self._convert_paragraph,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async def convert_block(self, block: dict[str, Any]) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Convert a single Notion block to a Markdown string
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
block (dict[str, Any]): A Notion block.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
str: A Markdown string.
|
|
42
|
+
"""
|
|
43
|
+
block_type = block.get("type")
|
|
44
|
+
if block_type in self.handlers:
|
|
45
|
+
converter = self.handlers[block_type]
|
|
46
|
+
if asyncio.iscoroutinefunction(converter):
|
|
47
|
+
md: str = await converter(block)
|
|
48
|
+
return md
|
|
49
|
+
else:
|
|
50
|
+
return converter(block)
|
|
51
|
+
elif block_type == BlockType.CHILD_PAGE.value:
|
|
52
|
+
return await self._convert_child_page(block)
|
|
53
|
+
else:
|
|
54
|
+
return self._get_plaintext(block)
|
|
55
|
+
|
|
56
|
+
@staticmethod
|
|
57
|
+
def rich_text_to_markdown(rich_text_items: list[dict[str, Any]]) -> str:
|
|
58
|
+
"""
|
|
59
|
+
Convert a list of rich text items (from a Notion block) into Markdown.
|
|
60
|
+
|
|
61
|
+
Handles formatting such as bold, italic, strikethrough, underline (via HTML),
|
|
62
|
+
inline code, text coloring, hyperlinks, and equations.
|
|
63
|
+
"""
|
|
64
|
+
md = ""
|
|
65
|
+
for item in rich_text_items:
|
|
66
|
+
annotations = item.get("annotations", {})
|
|
67
|
+
type_val = item.get("type", "text")
|
|
68
|
+
link = None
|
|
69
|
+
|
|
70
|
+
# Special handling for inline equations.
|
|
71
|
+
if type_val == "equation":
|
|
72
|
+
expression = item.get("equation", {}).get("expression", "")
|
|
73
|
+
md += f"${expression}$"
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
if type_val == "text":
|
|
77
|
+
text_obj = item.get("text", {})
|
|
78
|
+
text = text_obj.get("content", "")
|
|
79
|
+
link_obj = text_obj.get("link")
|
|
80
|
+
link = (
|
|
81
|
+
link_obj.get("url")
|
|
82
|
+
if (link_obj and isinstance(link_obj, dict))
|
|
83
|
+
else item.get("href")
|
|
84
|
+
)
|
|
85
|
+
elif type_val == "mention":
|
|
86
|
+
text = item.get("plain_text", "")
|
|
87
|
+
link = item.get("href")
|
|
88
|
+
else:
|
|
89
|
+
text = item.get("plain_text", "")
|
|
90
|
+
link = item.get("href")
|
|
91
|
+
|
|
92
|
+
if text.strip() == "":
|
|
93
|
+
continue
|
|
94
|
+
|
|
95
|
+
# Apply annotation formatting.
|
|
96
|
+
text = BlockToMarkdownConverter.apply_formatting(text, annotations, link)
|
|
97
|
+
|
|
98
|
+
md += text
|
|
99
|
+
|
|
100
|
+
return md
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def apply_formatting(text: str, annotations: dict[str, Any], link: str | None = None) -> str:
|
|
104
|
+
"""Apply formatting to a text string based on the annotations.
|
|
105
|
+
Used when converting rich text to markdown
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
text (str): The text to format.
|
|
109
|
+
annotations (dict[str, Any]): The annotations to apply to the text.
|
|
110
|
+
link (str | None): An optional link for a hyperlink.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
str: The formatted text.
|
|
114
|
+
"""
|
|
115
|
+
# If code block, wrap in backticks and skip other formatting.
|
|
116
|
+
if annotations.get("code"):
|
|
117
|
+
return f"`{text}`"
|
|
118
|
+
|
|
119
|
+
# Add underline
|
|
120
|
+
if annotations.get("underline"):
|
|
121
|
+
text = f"<u>{text}</u>"
|
|
122
|
+
|
|
123
|
+
# Apply color
|
|
124
|
+
color = annotations.get("color", "default")
|
|
125
|
+
if color != "default":
|
|
126
|
+
text = f'<span style="color: {color};">{text}</span>'
|
|
127
|
+
|
|
128
|
+
# Add bold, italic, and strikethrough
|
|
129
|
+
markers = [
|
|
130
|
+
marker
|
|
131
|
+
for key, marker in (("bold", "**"), ("italic", "*"), ("strikethrough", "~~"))
|
|
132
|
+
if annotations.get(key)
|
|
133
|
+
]
|
|
134
|
+
if markers:
|
|
135
|
+
text = "".join(markers) + text + "".join(reversed(markers))
|
|
136
|
+
|
|
137
|
+
# Add hyperlink
|
|
138
|
+
if link:
|
|
139
|
+
text = f"[{text}]({link})"
|
|
140
|
+
|
|
141
|
+
return text
|
|
142
|
+
|
|
143
|
+
def _get_plaintext(self, block: dict[str, Any]) -> str:
|
|
144
|
+
"""
|
|
145
|
+
Extract and return the plain text from a Notion block.
|
|
146
|
+
This acts as a fallback for unsupported block types.
|
|
147
|
+
"""
|
|
148
|
+
block_type: str = block.get("type", "")
|
|
149
|
+
content = block.get(block_type, {})
|
|
150
|
+
if isinstance(content, dict):
|
|
151
|
+
rich_text_items = content.get("rich_text", [])
|
|
152
|
+
return "".join(item.get("plain_text", "") for item in rich_text_items)
|
|
153
|
+
return ""
|
|
154
|
+
|
|
155
|
+
def _convert_text_block(self, block: dict[str, Any], element_key: str, prefix: str = "") -> str:
|
|
156
|
+
"""
|
|
157
|
+
Helper method to convert a Notion block's rich_text element into a Markdown string.
|
|
158
|
+
Optionally, a prefix (like a markdown list marker or heading hashes) is added.
|
|
159
|
+
"""
|
|
160
|
+
element = block.get(element_key, {})
|
|
161
|
+
rich_text_items = element.get("rich_text", [])
|
|
162
|
+
text = self.rich_text_to_markdown(rich_text_items)
|
|
163
|
+
return f"{prefix}{text} \n"
|
|
164
|
+
|
|
165
|
+
async def _convert_child_page(self, block: dict[str, Any]) -> str:
|
|
166
|
+
"""
|
|
167
|
+
Asynchronously convert a child page block. This requires fetching the page's URL.
|
|
168
|
+
"""
|
|
169
|
+
page_url = await get_page_url(self.context, block.get("id", ""))
|
|
170
|
+
child_page = block.get("child_page", {})
|
|
171
|
+
rich_text_items = child_page.get("rich_text", [])
|
|
172
|
+
if rich_text_items:
|
|
173
|
+
title = self.rich_text_to_markdown(rich_text_items)
|
|
174
|
+
else:
|
|
175
|
+
title = child_page.get("title", "")
|
|
176
|
+
return f"[{title}]({page_url}) \n"
|
|
177
|
+
|
|
178
|
+
def _convert_bulleted_list_item(self, block: dict[str, Any]) -> str:
|
|
179
|
+
return self._convert_text_block(block, "bulleted_list_item", "- ")
|
|
180
|
+
|
|
181
|
+
def _convert_equation(self, block: dict[str, Any]) -> str:
|
|
182
|
+
expression = block.get("equation", {}).get("expression", "")
|
|
183
|
+
return f"$$ {expression} $$ \n"
|
|
184
|
+
|
|
185
|
+
def _convert_heading_1(self, block: dict[str, Any]) -> str:
|
|
186
|
+
return self._convert_text_block(block, "heading_1", "# ")
|
|
187
|
+
|
|
188
|
+
def _convert_heading_2(self, block: dict[str, Any]) -> str:
|
|
189
|
+
return self._convert_text_block(block, "heading_2", "## ")
|
|
190
|
+
|
|
191
|
+
def _convert_heading_3(self, block: dict[str, Any]) -> str:
|
|
192
|
+
return self._convert_text_block(block, "heading_3", "### ")
|
|
193
|
+
|
|
194
|
+
def _convert_link_preview(self, block: dict[str, Any]) -> str:
|
|
195
|
+
return self._convert_text_block(block, "link_preview")
|
|
196
|
+
|
|
197
|
+
def _convert_numbered_list_item(self, block: dict[str, Any]) -> str:
|
|
198
|
+
return self._convert_text_block(block, "numbered_list_item", "1. ")
|
|
199
|
+
|
|
200
|
+
def _convert_paragraph(self, block: dict[str, Any]) -> str:
|
|
201
|
+
return self._convert_text_block(block, "paragraph")
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
NOTION_API_URL = "https://api.notion.com/v1"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
ENDPOINTS = {
|
|
5
|
+
"create_a_page": "/pages",
|
|
6
|
+
"retrieve_block_children": "/blocks/{block_id}/children",
|
|
7
|
+
"search_by_title": "/search",
|
|
8
|
+
"query_a_database": "/databases/{database_id}/query",
|
|
9
|
+
"update_page_properties": "/pages/{page_id}",
|
|
10
|
+
"append_block_children": "/blocks/{block_id}/children",
|
|
11
|
+
"retrieve_a_database": "/databases/{database_id}",
|
|
12
|
+
"create_comment": "/comments",
|
|
13
|
+
"retrieve_a_page": "/pages/{page_id}",
|
|
14
|
+
"retrieve_a_block": "/blocks/{block_id}",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
UNTITLED_TITLE = "New Page"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SortDirection(str, Enum):
|
|
5
|
+
ASCENDING = "ascending"
|
|
6
|
+
DESCENDING = "descending"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ObjectType(str, Enum):
|
|
10
|
+
PAGE = "page"
|
|
11
|
+
DATABASE = "database"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BlockType(str, Enum):
|
|
15
|
+
BOOKMARK = "bookmark"
|
|
16
|
+
BREADCRUMB = "breadcrumb"
|
|
17
|
+
BULLETED_LIST_ITEM = "bulleted_list_item"
|
|
18
|
+
CALLOUT = "callout"
|
|
19
|
+
CHILD_DATABASE = "child_database"
|
|
20
|
+
CHILD_PAGE = "child_page"
|
|
21
|
+
COLUMN = "column"
|
|
22
|
+
COLUMN_LIST = "column_list"
|
|
23
|
+
DIVIDER = "divider"
|
|
24
|
+
EMBED = "embed"
|
|
25
|
+
EQUATION = "equation"
|
|
26
|
+
FILE = "file"
|
|
27
|
+
HEADING_1 = "heading_1"
|
|
28
|
+
HEADING_2 = "heading_2"
|
|
29
|
+
HEADING_3 = "heading_3"
|
|
30
|
+
IMAGE = "image"
|
|
31
|
+
LINK_PREVIEW = "link_preview"
|
|
32
|
+
LINK_TO_PAGE = "link_to_page"
|
|
33
|
+
NUMBERED_LIST_ITEM = "numbered_list_item"
|
|
34
|
+
PARAGRAPH = "paragraph"
|
|
35
|
+
PDF = "pdf"
|
|
36
|
+
QUOTE = "quote"
|
|
37
|
+
SYNCED_BLOCK = "synced_block"
|
|
38
|
+
TABLE = "table"
|
|
39
|
+
TABLE_OF_CONTENTS = "table_of_contents"
|
|
40
|
+
TABLE_ROW = "table_row"
|
|
41
|
+
TEMPLATE = "template"
|
|
42
|
+
TO_DO = "to_do"
|
|
43
|
+
TOGGLE = "toggle"
|
|
44
|
+
UNSUPPORTED = "unsupported"
|
|
45
|
+
VIDEO = "video"
|