# Notion to Markdown Exporter

A simple Python Flask application that exports Notion page content to Markdown format using the Notion API.

## Features

- Export complete Notion page content to Markdown format
- Support for all major Notion block types:
  - Headings (H1, H2, H3)
  - Paragraphs with rich text formatting (bold, italic, strikethrough, code, links)
  - Lists (bulleted, numbered, to-do)
  - Quotes and callouts
  - Code blocks with syntax highlighting
  - Images and files
  - Tables
  - Bookmarks and embeds
  - Toggle blocks
  - Dividers
- Handles nested blocks and child content
- Proper pagination handling for large pages
- RESTful API with JSON error responses
- Comprehensive logging

## Setup

### 1. Install Dependencies

```bash
pip install -r requirements.txt
```

### 2. Set up Notion Integration

1. Go to [Notion Integrations](https://www.notion.so/my-integrations)
2. Click "New integration"
3. Give it a name and select your workspace
4. Copy the "Internal Integration Token"

### 3. Configure Environment Variables

Create a `.env` file in the project root:

```bash
cp .env.example .env
```

Edit the `.env` file and add your Notion token:

```
NOTION_TOKEN=your_notion_integration_token_here
```

### 4. Share Pages with Integration

For each Notion page you want to export:
1. Open the page in Notion
2. Click the "Share" button
3. Click "Add connections"
4. Select your integration

## Usage

### Start the Server

```bash
python app.py
```

The server will start on `http://localhost:5000`

### API Endpoints

#### Export Page to Markdown
```
GET /page/<page_id>
```

**Example:**
```bash
curl http://localhost:5000/page/897e5a76ae524b489fdfe71f5945d1af
```

**Response:** The page content in Markdown format

#### Health Check
```
GET /health
```

**Response:**
```json
{
  "status": "healthy",
  "service": "Notion to Markdown Exporter"
}
```

#### Service Information
```
GET /
```

**Response:**
```json
{
  "service": "Notion to Markdown Exporter",
  "usage": "Send GET request to /page/<page_id> to export a Notion page to Markdown",
  "example": "/page/897e5a76ae524b489fdfe71f5945d1af",
  "health_check": "/health"
}
```

### Finding Page IDs

To get a Notion page ID:
1. Open the page in your browser
2. The page ID is the 32-character string in the URL
3. Example: `https://notion.so/My-Page-897e5a76ae524b489fdfe71f5945d1af`
4. Page ID: `897e5a76ae524b489fdfe71f5945d1af`

## Supported Block Types

| Notion Block Type | Markdown Output |
|-------------------|-----------------|
| Paragraph | Plain text with formatting |
| Heading 1 | `# Heading` |
| Heading 2 | `## Heading` |
| Heading 3 | `### Heading` |
| Bulleted List | `- Item` |
| Numbered List | `1. Item` |
| To-do | `- [ ] Task` or `- [x] Task` |
| Quote | `> Quote text` |
| Code | ````language\ncode\n```` |
| Callout | `> 🔔 Callout text` |
| Divider | `---` |
| Image | `![alt text](url)` |
| File | `[filename](url)` |
| Bookmark | `[title](url)` |
| Embed | `[Embedded content](url)` |
| Table | Markdown table format |
| Toggle | `<details><summary>Title</summary></details>` |

## Rich Text Formatting

The exporter preserves Notion's rich text formatting:

- **Bold text** → `**Bold text**`
- *Italic text* → `*Italic text*`
- ~~Strikethrough~~ → `~~Strikethrough~~`
- `Inline code` → `` `Inline code` ``
- [Links](url) → `[Links](url)`

## Error Handling

The API returns appropriate HTTP status codes and JSON error messages:

- `400` - Invalid page ID format
- `404` - Page not found or not accessible
- `500` - Server configuration or processing errors

## Development

### Environment Variables

- `NOTION_TOKEN` - Your Notion integration token (required)
- `PORT` - Server port (default: 5000)
- `FLASK_ENV` - Set to `development` for debug mode

### Running in Development Mode

```bash
export FLASK_ENV=development
python app.py
```

## Requirements

- Python >= 3.7
- Flask
- notion-client
- python-dotenv

## License

MIT License
