diff --git a/main.ts b/main.ts index 2a7ecc8..095dd70 100644 --- a/main.ts +++ b/main.ts @@ -1440,6 +1440,12 @@ try { console.log(`Export: Processing ${processedResult.metadata.pdfEmbeds.length} PDF embeds`); await this.processPdfEmbeds(processedResult, vaultBasePath, tempDir, file); } + + // Process image embeds if any were found + if (processedResult.metadata.imageEmbeds && processedResult.metadata.imageEmbeds.length > 0) { + console.log(`Export: Processing ${processedResult.metadata.imageEmbeds.length} image embeds`); + await this.processImageEmbeds(processedResult, vaultBasePath, tempDir, file); + } // Debug: Log processed content to see what image paths look like console.log('Export: Original content length:', content.length); @@ -1676,6 +1682,145 @@ try { // Update the processed result with the new content processedResult.content = updatedContent; } + + /** + * Process regular image embeds by copying them to vault location for Typst access + */ + private async processImageEmbeds(processedResult: any, vaultBasePath: string, tempDir: string, currentFile?: TFile): Promise { + const path = require('path'); + const fs = require('fs'); + + console.log(`Export: Processing ${processedResult.metadata.imageEmbeds.length} image embeds`); + + let updatedContent = processedResult.content; + + // Create vault temp images directory + const vaultTempImagesDir = path.join(vaultBasePath, '.obsidian', 'plugins', 'obsidian-typst-pdf-export', 'temp-images'); + const fs_promises = require('fs').promises; + await fs_promises.mkdir(vaultTempImagesDir, { recursive: true }); + + for (const imageEmbed of processedResult.metadata.imageEmbeds) { + try { + console.log(`Export: Processing image embed: ${imageEmbed.originalPath}`); + + // Decode the URL-encoded sanitized path back to normal characters + const decodedPath = decodeURIComponent(imageEmbed.sanitizedPath); + + // Try multiple path resolution strategies + const possiblePaths = [ + // Strategy 1: Relative to vault root (standard Obsidian behavior) + path.resolve(vaultBasePath, decodedPath), + // Strategy 2: Direct path if it's just a filename (check vault root) + path.resolve(vaultBasePath, imageEmbed.fileName), + // Strategy 3: Relative to current file's directory (for local attachments) + currentFile ? path.resolve(vaultBasePath, path.dirname(currentFile.path), decodedPath) : null, + currentFile ? path.resolve(vaultBasePath, path.dirname(currentFile.path), imageEmbed.fileName) : null, + // Strategy 4: Check common attachment directories + path.resolve(vaultBasePath, 'attachments', imageEmbed.fileName), + path.resolve(vaultBasePath, 'assets', imageEmbed.fileName), + path.resolve(vaultBasePath, 'images', imageEmbed.fileName) + ].filter(p => p !== null); + + let fullImagePath = null; + let validPath = null; + + // Try each possible path until we find one that exists + for (const possiblePath of possiblePaths) { + console.log(`Export: Checking path: ${possiblePath}`); + if (fs.existsSync(possiblePath)) { + fullImagePath = possiblePath; + validPath = possiblePath; + console.log(`Export: Found image at: ${fullImagePath}`); + break; + } + } + + if (!fullImagePath) { + console.warn(`Export: Image file not found in any of the expected locations: ${possiblePaths.join(', ')}`); + // Replace marker with fallback link or broken image indicator + const fallbackOutput = `![⚠️ Image not found: ${imageEmbed.baseName}](${decodedPath})`; + updatedContent = updatedContent.replace(imageEmbed.marker, fallbackOutput); + continue; + } + + // Copy image to vault temp directory (convert WebP to PNG if needed) + const originalImageName = path.basename(fullImagePath); + const fileExtension = path.extname(originalImageName).toLowerCase(); + + let vaultImagePath; + let finalImageName; + + if (fileExtension === '.webp') { + // Convert WebP to PNG using ImageMagick + const pngFileName = originalImageName.replace(/\.webp$/i, '.png'); + vaultImagePath = path.join(vaultTempImagesDir, pngFileName); + finalImageName = pngFileName; + + console.log(`Export: Converting WebP to PNG: ${originalImageName} -> ${pngFileName}`); + + const { exec } = require('child_process'); + const util = require('util'); + const execAsync = util.promisify(exec); + + try { + // Use full path to convert command to ensure it's found + const convertPath = '/opt/homebrew/bin/convert'; + await execAsync(`"${convertPath}" "${fullImagePath}" "${vaultImagePath}"`); + console.log(`Export: Successfully converted WebP to PNG: ${pngFileName}`); + } catch (convertError) { + console.error(`Export: Failed to convert WebP image: ${convertError.message}`); + throw convertError; + } + } else { + // For non-WebP images, copy directly + vaultImagePath = path.join(vaultTempImagesDir, originalImageName); + finalImageName = originalImageName; + await fs_promises.copyFile(fullImagePath, vaultImagePath); + } + + // Get relative path for the copied image from vault base + const relativeImagePath = path.relative(vaultBasePath, vaultImagePath); + + // Create the appropriate markdown output based on size/alt text + let imageOutput; + if (imageEmbed.sizeOrAlt) { + // Check if it's size information (width x height pattern) + const sizeMatch = imageEmbed.sizeOrAlt.match(/^(\d+)(?:x(\d+))?$/); + if (sizeMatch) { + const width = sizeMatch[1]; + const height = sizeMatch[2]; + + // Create image with size attributes (optimized for Pandoc → Typst conversion) + if (height) { + imageOutput = `${imageEmbed.baseName}`; + } else { + imageOutput = `${imageEmbed.baseName}`; + } + } else { + // Treat as alt text + imageOutput = `![${imageEmbed.sizeOrAlt}](${relativeImagePath})`; + } + } else { + // Standard image reference + imageOutput = `![${imageEmbed.baseName}](${relativeImagePath})`; + } + + // Replace the placeholder with the image output + updatedContent = updatedContent.replace(imageEmbed.marker, imageOutput); + + console.log(`Export: Successfully processed image embed: ${imageEmbed.baseName} -> ${finalImageName}`); + } catch (error) { + console.error(`Export: Error processing image embed ${imageEmbed.baseName}:`, error); + // Replace with fallback image reference + const decodedPath = decodeURIComponent(imageEmbed.sanitizedPath); + const fallbackOutput = `![⚠️ Error loading: ${imageEmbed.baseName}](${decodedPath})`; + updatedContent = updatedContent.replace(imageEmbed.marker, fallbackOutput); + } + } + + // Update the processed result with the new content + processedResult.content = updatedContent; + } } /** diff --git a/src/MarkdownPreprocessor.ts b/src/MarkdownPreprocessor.ts index 0d30cf6..0a49688 100644 --- a/src/MarkdownPreprocessor.ts +++ b/src/MarkdownPreprocessor.ts @@ -36,6 +36,14 @@ export interface PreprocessingResult { options?: string; marker: string; }>; + imageEmbeds?: Array<{ + originalPath: string; + sanitizedPath: string; + fileName: string; + baseName: string; + sizeOrAlt?: string; + marker: string; + }>; }; /** Processing errors and warnings */ errors: string[]; @@ -439,41 +447,34 @@ export class MarkdownPreprocessor { * Process image embeds with size parameters */ private processImageEmbed(imagePath: string, sizeOrAlt: string | undefined, result: PreprocessingResult): string { - // Don't sanitize the path - keep it as-is for Pandoc to resolve - // Pandoc with --resource-path will handle the path resolution - let resolvedPath = imagePath; - - // If path doesn't start with / or contain :, it's likely a vault-relative path - // Keep it as-is since Pandoc will resolve it using --resource-path - if (!imagePath.startsWith('/') && !imagePath.includes(':')) { - // This is a vault-relative path - keep it exactly as Obsidian stores it - resolvedPath = imagePath; - } - - // Parse size information if provided - if (sizeOrAlt) { - const sizeMatch = sizeOrAlt.match(this.EMBED_SIZE_PATTERN); - if (sizeMatch) { - const width = sizeMatch[1]; - const height = sizeMatch[2]; - - // Create image with size attributes (optimized for Pandoc → Typst conversion) - // Pandoc will convert these to #figure(image("path", width: X, height: Y)) for Typst - if (height) { - return ``; - } else { - return ``; - } - } else { - // Treat as alt text - return `![${sizeOrAlt}](${resolvedPath})`; + const sanitizedPath = this.sanitizeFilePath(imagePath); + + // For now, we'll mark this for async processing and return a placeholder + // The actual copying will be handled in the main export process + const fileName = imagePath.substring(imagePath.lastIndexOf('/') + 1); + const baseName = fileName.replace(/\.[^/.]+$/, ""); // Remove extension + + // Create a marker that the export process can detect and replace + const marker = `IMAGE_EMBED_MARKER:${imagePath}:${baseName}:${sizeOrAlt || ''}`; + + // Add to processing queue for later copying + if (!result.metadata.imageEmbeds) { + result.metadata.imageEmbeds = []; } + result.metadata.imageEmbeds.push({ + originalPath: imagePath, + sanitizedPath: sanitizedPath, + fileName: fileName, + baseName: baseName, + sizeOrAlt: sizeOrAlt, + marker: marker + }); + + result.warnings.push(`Image embed queued for processing: ${imagePath}`); + + return marker; } - // Standard image reference - Pandoc will convert to #figure(image("path")) for Typst - return `![](${resolvedPath})`; -} - /** * Process video embeds */