Module

content_layer.sources.notion

NotionSource - Content source for Notion databases.

Fetches pages from Notion databases and converts them to markdown.

Requires: pip install bengal[notion] (installs aiohttp)

Classes

NotionSource
Content source for Notion databases. Fetches pages from a Notion database and converts them to mar…
9

Content source for Notion databases.

Fetches pages from a Notion database and converts them to markdown. Requires a Notion integration token with read access to the database.

Configuration:

database_id: str - Notion database ID (required)
token: str - Notion integration token (or NOTION_TOKEN env var)
property_mapping: dict - Map Notion properties to frontmatter fields
    Defaults: {"title": "Name", "date": "Date", "tags": "Tags"}
filter: dict - Notion filter object (optional)
sorts: list - Notion sorts array (optional)

Setup:

1. Create a Notion integration at https://www.notion.so/my-integrations
2. Share your database with the integration
3. Set NOTION_TOKEN environment variable or pass token in config
Inherits from ContentSource

Methods 2

fetch_all async
Fetch all pages from the database. Handles pagination automatically.
0 AsyncIterator[Conte…
async def fetch_all(self) -> AsyncIterator[ContentEntry]

Fetch all pages from the database.

Handles pagination automatically.

Returns

AsyncIterator[ContentEntry]

fetch_one async
Fetch a single page by ID.
1 ContentEntry | None
async def fetch_one(self, id: str) -> ContentEntry | None

Fetch a single page by ID.

Parameters 1
id str

Notion page ID

Returns

ContentEntry | None

ContentEntry if found, None otherwise

Internal Methods 7
__init__
Initialize Notion source.
2 None
def __init__(self, name: str, config: dict[str, Any]) -> None

Initialize Notion source.

Parameters 2
name str

Source name

config dict[str, Any]

Configuration with 'database_id' required

_page_to_entry async
Convert Notion page to ContentEntry.
2 ContentEntry | None
async def _page_to_entry(self, session: aiohttp.ClientSession, page: dict[str, Any]) -> ContentEntry | None

Convert Notion page to ContentEntry.

Parameters 2
session aiohttp.ClientSession

aiohttp session

page dict[str, Any]

Notion page object

Returns

ContentEntry | None

ContentEntry or None

_get_page_content async
Fetch and convert page blocks to markdown.
2 str
async def _get_page_content(self, session: aiohttp.ClientSession, page_id: str) -> str

Fetch and convert page blocks to markdown.

Parameters 2
session aiohttp.ClientSession

aiohttp session

page_id str

Notion page ID

Returns

str

Markdown content string

_blocks_to_markdown
Convert Notion blocks to markdown.
1 str
def _blocks_to_markdown(self, blocks: list[dict[str, Any]]) -> str

Convert Notion blocks to markdown.

Parameters 1
blocks list[dict[str, Any]]

List of Notion block objects

Returns

str

Markdown string

_rich_text_to_md
Convert Notion rich text to markdown.
1 str
def _rich_text_to_md(self, rich_text: list[dict[str, Any]]) -> str

Convert Notion rich text to markdown.

Parameters 1
rich_text list[dict[str, Any]]

List of rich text objects

Returns

str

Markdown string

_extract_properties
Extract Notion properties as frontmatter.
1 dict[str, Any]
def _extract_properties(self, page: dict[str, Any]) -> dict[str, Any]

Extract Notion properties as frontmatter.

Parameters 1
page dict[str, Any]

Notion page object

Returns

dict[str, Any]

Frontmatter dictionary

_title_to_slug
Convert title to URL-friendly slug.
1 str
def _title_to_slug(self, title: str) -> str

Convert title to URL-friendly slug.

Parameters 1
title str

Page title

Returns

str

URL-friendly slug