File size: 7,946 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 |
#!/usr/bin/env node
import { config } from 'dotenv';
import { Client } from '@notionhq/client';
import { NotionConverter } from 'notion-to-md';
import { DefaultExporter } from 'notion-to-md/plugins/exporter';
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { join, dirname, basename } from 'path';
import { fileURLToPath } from 'url';
import { postProcessMarkdown } from './post-processor.mjs';
import { createCustomCodeRenderer } from './custom-code-renderer.mjs';
// Load environment variables from .env file
config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Configuration
const DEFAULT_INPUT = join(__dirname, 'input', 'pages.json');
const DEFAULT_OUTPUT = join(__dirname, 'output');
function parseArgs() {
const args = process.argv.slice(2);
const config = {
input: DEFAULT_INPUT,
output: DEFAULT_OUTPUT,
clean: false,
token: process.env.NOTION_TOKEN
};
for (const arg of args) {
if (arg.startsWith('--input=')) {
config.input = arg.split('=')[1];
} else if (arg.startsWith('--output=')) {
config.output = arg.split('=')[1];
} else if (arg.startsWith('--token=')) {
config.token = arg.split('=')[1];
} else if (arg === '--clean') {
config.clean = true;
}
}
return config;
}
function ensureDirectory(dir) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}
function loadPagesConfig(configFile) {
if (!existsSync(configFile)) {
console.error(`β Configuration file not found: ${configFile}`);
console.log('π Create a pages.json file with your Notion page IDs:');
console.log(`
{
"pages": [
{
"id": "your-notion-page-id-1",
"title": "Page Title 1",
"slug": "page-1"
},
{
"id": "your-notion-page-id-2",
"title": "Page Title 2",
"slug": "page-2"
}
]
}
`);
process.exit(1);
}
try {
const config = JSON.parse(readFileSync(configFile, 'utf8'));
return config.pages || [];
} catch (error) {
console.error(`β Error reading configuration: ${error.message}`);
process.exit(1);
}
}
/**
* Convert a single Notion page to Markdown with advanced media handling
* @param {Object} notion - Notion client
* @param {string} pageId - Notion page ID
* @param {string} outputDir - Output directory
* @param {string} pageTitle - Page title for file naming
* @returns {Promise<string>} - Path to generated markdown file
*/
async function convertNotionPage(notion, pageId, outputDir, pageTitle) {
console.log(`π Converting Notion page: ${pageTitle} (${pageId})`);
try {
// Create media directory for this page
const mediaDir = join(outputDir, 'media', pageId);
ensureDirectory(mediaDir);
// Configure the DefaultExporter to save to a file
const outputFile = join(outputDir, `${pageTitle}.md`);
const exporter = new DefaultExporter({
outputType: 'file',
outputPath: outputFile,
});
// Create the converter with media downloading strategy
const n2m = new NotionConverter(notion)
.withExporter(exporter)
// Download media to local directory with path transformation
.downloadMediaTo({
outputDir: mediaDir,
// Transform paths to be web-accessible
transformPath: (localPath) => `/media/${pageId}/${basename(localPath)}`,
});
// Convert the page
const result = await n2m.convert(pageId);
console.log(` β
Converted to: ${outputFile}`);
console.log(` π Content length: ${result.content.length} characters`);
console.log(` πΌοΈ Media saved to: ${mediaDir}`);
return outputFile;
} catch (error) {
console.error(` β Failed to convert page ${pageId}: ${error.message}`);
throw error;
}
}
/**
* Process Notion pages with advanced configuration
* @param {string} inputFile - Path to pages configuration
* @param {string} outputDir - Output directory
* @param {string} notionToken - Notion API token
*/
export async function convertNotionToMarkdown(inputFile, outputDir, notionToken) {
console.log('π Notion to Markdown Converter');
console.log(`π Input: ${inputFile}`);
console.log(`π Output: ${outputDir}`);
// Validate Notion token
if (!notionToken) {
console.error('β NOTION_TOKEN not found. Please set it as environment variable or use --token=YOUR_TOKEN');
process.exit(1);
}
// Ensure output directory exists
ensureDirectory(outputDir);
try {
// Initialize Notion client
const notion = new Client({
auth: notionToken,
});
// Load pages configuration
const pages = loadPagesConfig(inputFile);
console.log(`π Found ${pages.length} page(s) to convert`);
const convertedFiles = [];
// Convert each page
for (const page of pages) {
try {
const outputFile = await convertNotionPage(
notion,
page.id,
outputDir,
page.slug || page.title?.toLowerCase().replace(/\s+/g, '-') || page.id
);
convertedFiles.push(outputFile);
} catch (error) {
console.error(`β Failed to convert page ${page.id}: ${error.message}`);
// Continue with other pages
}
}
// Post-process all converted files
console.log('π§ Post-processing converted files...');
for (const file of convertedFiles) {
try {
let content = readFileSync(file, 'utf8');
content = postProcessMarkdown(content);
writeFileSync(file, content);
console.log(` β
Post-processed: ${basename(file)}`);
} catch (error) {
console.error(` β Failed to post-process ${file}: ${error.message}`);
}
}
console.log(`β
Conversion completed! ${convertedFiles.length} file(s) generated`);
} catch (error) {
console.error('β Conversion failed:', error.message);
process.exit(1);
}
}
function main() {
const config = parseArgs();
if (config.clean) {
console.log('π§Ή Cleaning output directory...');
// Clean output directory logic would go here
}
convertNotionToMarkdown(config.input, config.output, config.token);
console.log('π Notion conversion completed!');
}
// Show help if requested
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log(`
π Notion to Markdown Converter
Usage:
node notion-converter.mjs [options]
Options:
--input=PATH Input pages configuration file (default: input/pages.json)
--output=PATH Output directory (default: output/)
--token=TOKEN Notion API token (or set NOTION_TOKEN env var)
--clean Clean output directory before conversion
--help, -h Show this help
Environment Variables:
NOTION_TOKEN Your Notion integration token
Examples:
# Basic conversion with environment token
NOTION_TOKEN=your_token node notion-converter.mjs
# Custom paths and token
node notion-converter.mjs --input=my-pages.json --output=converted/ --token=your_token
# Clean output first
node notion-converter.mjs --clean
Configuration File Format (pages.json):
{
"pages": [
{
"id": "your-notion-page-id",
"title": "Page Title",
"slug": "page-slug"
}
]
}
`);
process.exit(0);
}
// Run CLI if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}
|