File size: 8,054 Bytes
e903a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Notion Importer

Complete Notion to MDX (Markdown + JSX) importer optimized for Astro with advanced media handling, interactive components, and seamless integration.

## ๐Ÿš€ Quick Start

```bash
# Install dependencies
npm install

# Setup environment variables
cp env.example .env
# Edit .env with your Notion token

# Complete Notion โ†’ MDX conversion with all features
node index.mjs

# For step-by-step debugging
node notion-converter.mjs    # Notion โ†’ Markdown
node mdx-converter.mjs       # Markdown โ†’ MDX
```

## ๐Ÿ“ Structure

```
notion-importer/
โ”œโ”€โ”€ index.mjs                    # Complete Notion โ†’ MDX pipeline
โ”œโ”€โ”€ notion-converter.mjs         # Notion โ†’ Markdown with notion-to-md v4
โ”œโ”€โ”€ mdx-converter.mjs           # Markdown โ†’ MDX with Astro components
โ”œโ”€โ”€ post-processor.mjs          # Markdown post-processing
โ”œโ”€โ”€ package.json                # Dependencies and scripts
โ”œโ”€โ”€ env.example                 # Environment variables template
โ”œโ”€โ”€ input/                      # Configuration
โ”‚   โ””โ”€โ”€ pages.json             # Notion pages to convert
โ””โ”€โ”€ output/                     # Results
    โ”œโ”€โ”€ *.md                   # Intermediate Markdown
    โ”œโ”€โ”€ *.mdx                  # Final MDX for Astro
    โ””โ”€โ”€ media/                 # Downloaded media files
```

## โœจ Key Features

### ๐ŸŽฏ **Advanced Media Handling**
- **Local download**: Automatic download of all Notion media (images, files, PDFs)
- **Path transformation**: Smart path conversion for web accessibility
- **Figure components**: Automatic conversion to Astro `Figure` components with zoom/download
- **Media organization**: Structured media storage by page ID

### ๐Ÿงฎ **Interactive Components**
- **Callouts โ†’ Notes**: Notion callouts converted to Astro `Note` components
- **Enhanced tables**: Tables wrapped in styled containers
- **Code blocks**: Enhanced with copy functionality
- **Automatic imports**: Smart component and image import generation

### ๐ŸŽจ **Smart Formatting**  
- **Link fixing**: Notion internal links converted to relative links
- **Artifact cleanup**: Removal of Notion-specific formatting artifacts
- **Frontmatter generation**: Automatic YAML frontmatter from Notion properties
- **Astro compatibility**: Full compatibility with Astro MDX processing

### ๐Ÿ”ง **Robust Pipeline**
- **Notion preprocessing**: Advanced page configuration and media strategy
- **Post-processing**: Markdown cleanup and optimization
- **MDX conversion**: Final transformation with Astro components
- **Auto-copy**: Automatic copying to Astro content directory

## ๐Ÿ“Š Example Workflow

```bash
# 1. Configure your Notion pages
# Edit input/pages.json with your page IDs

# 2. Complete automatic conversion
NOTION_TOKEN=your_token node index.mjs --clean

# 3. Generated results
ls output/
# โ†’ getting-started.md (Intermediate Markdown)  
# โ†’ getting-started.mdx (Final MDX for Astro)
# โ†’ media/ (downloaded images and files)
```

### ๐Ÿ“‹ Conversion Result

The pipeline generates MDX files optimized for Astro with:

```mdx
---
title: "Getting Started with Notion"
published: "2024-01-15"
tableOfContentsAutoCollapse: true
---

import Figure from '../components/Figure.astro';
import Note from '../components/Note.astro';
import gettingStartedImage from './media/getting-started/image1.png';

## Introduction

Here is some content with a callout:

<Note type="info" title="Important">
This is a converted Notion callout.
</Note>

And an image:

<Figure
  src={gettingStartedImage}
  alt="Getting started screenshot"
  zoomable
  downloadable
  layout="fixed"
/>
```

## โš™๏ธ Required Astro Configuration

To use the generated MDX files, ensure your Astro project has the required components:

```astro
// src/components/Figure.astro
---
export interface Props {
  src: any;
  alt?: string;
  caption?: string;
  zoomable?: boolean;
  downloadable?: boolean;
  layout?: string;
  id?: string;
}

const { src, alt, caption, zoomable, downloadable, layout, id } = Astro.props;
---

<figure {id} class="figure">
  <img src={src} alt={alt} />
  {caption && <figcaption>{caption}</figcaption>}
</figure>
```

## ๐Ÿ› ๏ธ Prerequisites

- **Node.js** with ESM support
- **Notion Integration**: Set up an integration in your Notion workspace
- **Notion Token**: Copy the "Internal Integration Token"
- **Shared Pages**: Share the specific Notion page(s) with your integration
- **Astro** to use the generated MDX

## ๐ŸŽฏ Technical Architecture

### 4-Stage Pipeline

1. **Notion Preprocessing** (`notion-converter.mjs`)
   - Configuration loading from `pages.json`
   - Notion API client initialization
   - Media download strategy configuration

2. **Notion-to-Markdown** (notion-to-md v4)
   - Page conversion with `NotionConverter`
   - Media downloading with `downloadMediaTo()`
   - File export with `DefaultExporter`

3. **Markdown Post-processing** (`post-processor.mjs`)
   - Notion artifact cleanup
   - Link fixing and optimization
   - Table and code block enhancement

4. **MDX Conversion** (`mdx-converter.mjs`)
   - Component transformation (Figure, Note)
   - Automatic import generation
   - Frontmatter enhancement
   - Astro compatibility optimization

## ๐Ÿ“Š Configuration Options

### Pages Configuration (`input/pages.json`)

```json
{
  "pages": [
    {
      "id": "your-notion-page-id",
      "title": "Page Title",
      "slug": "page-slug"
    }
  ]
}
```

### Environment Variables

Copy `env.example` to `.env` and configure:

```bash
cp env.example .env
# Edit .env with your actual Notion token
```

Required variables:
```bash
NOTION_TOKEN=secret_your_notion_integration_token_here
```

### Command Line Options

```bash
# Full workflow
node index.mjs --clean --token=your_token

# Notion to Markdown only
node index.mjs --notion-only

# Markdown to MDX only  
node index.mjs --mdx-only

# Custom paths
node index.mjs --input=my-pages.json --output=converted/
```

## ๐Ÿ“Š Conversion Statistics

For a typical Notion page:
- **Media files** automatically downloaded and organized
- **Callouts** converted to interactive Note components
- **Images** transformed to Figure components with zoom/download
- **Tables** enhanced with proper styling containers
- **Code blocks** enhanced with copy functionality
- **Links** fixed for proper internal navigation

## โœ… Project Status

### ๐ŸŽ‰ **Complete Features**
- โœ… **Notion โ†’ MDX Pipeline**: Full end-to-end functional conversion
- โœ… **Media Management**: Automatic download and path transformation
- โœ… **Component Integration**: Seamless Astro component integration
- โœ… **Smart Formatting**: Intelligent cleanup and optimization
- โœ… **Robustness**: Error handling and graceful degradation
- โœ… **Flexibility**: Modular pipeline with step-by-step options

### ๐Ÿš€ **Production Ready**
The toolkit is now **100% operational** for converting Notion pages to MDX/Astro with all advanced features (media handling, component integration, smart formatting).

## ๐Ÿ”— Integration with notion-to-md v4

This toolkit leverages the powerful [notion-to-md v4](https://notionconvert.com/docs/v4/guides/) library with:

- **Advanced Media Strategies**: Download, upload, and direct media handling
- **Custom Renderers**: Block transformers and annotation transformers  
- **Exporter Plugins**: File, buffer, and stdout output options
- **Database Support**: Full database property and frontmatter transformation
- **Page References**: Smart internal link handling

## ๐Ÿ“š Additional Resources

- [notion-to-md v4 Documentation](https://notionconvert.com/docs/v4/guides/)
- [Notion API Documentation](https://developers.notion.com/)
- [Astro MDX Documentation](https://docs.astro.build/en/guides/integrations-guide/mdx/)
- [Media Handling Strategies](https://notionconvert.com/blog/mastering-media-handling-in-notion-to-md-v4-download-upload-and-direct-strategies/)
- [Frontmatter Transformation](https://notionconvert.com/blog/how-to-convert-notion-properties-to-frontmatter-with-notion-to-md-v4/)