From 2bf0e57cdcc291d979e04a8f43da6a821c19ba11 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 19:52:37 +0200 Subject: [PATCH] Refactors PDF path resolution Simplifies PDF embed processing by introducing a dedicated `resolvePdfPath` method. This improves code readability and maintainability by encapsulating the logic for finding PDF files using different path resolution strategies. It also makes it easier to test the path resolution logic independently. --- main.ts | 60 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/main.ts b/main.ts index 1eb00f0..95bc081 100644 --- a/main.ts +++ b/main.ts @@ -651,6 +651,40 @@ ${dependencyResult.allAvailable /** * Process PDF embeds - convert PDF pages to images for inclusion */ + /** + * Resolve PDF path using multiple strategies + * @param sanitizedPath The sanitized path from the embed + * @param vaultBasePath Base path of the vault + * @param currentFile Current file being processed (optional) + * @returns Full path to PDF file if found, null otherwise + */ + private resolvePdfPath(sanitizedPath: string, vaultBasePath: string, currentFile?: TFile): string | null { + const pathModule = require('path'); + const fs = require('fs'); + + // Decode the URL-encoded sanitized path back to normal characters + const decodedPath = decodeURIComponent(sanitizedPath); + + // Try multiple path resolution strategies + const possiblePaths = [ + // Strategy 1: Relative to vault root (standard Obsidian behavior) + pathModule.resolve(vaultBasePath, decodedPath), + // Strategy 2: Relative to current file's directory (for local attachments) + currentFile ? pathModule.resolve(vaultBasePath, pathModule.dirname(currentFile.path), decodedPath) : null + ].filter(p => p !== 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)) { + console.log(`Export: Found PDF at: ${possiblePath}`); + return possiblePath; + } + } + + return null; + } + private async processPdfEmbeds(processedResult: any, vaultBasePath: string, tempDir: string, currentFile?: TFile): Promise { const { PdfToImageConverter } = await import('./src/PdfToImageConverter'); const converter = PdfToImageConverter.getInstance(this); @@ -665,31 +699,11 @@ ${dependencyResult.allAvailable try { console.log(`Export: Processing PDF embed: ${pdfEmbed.originalPath}`); - // Decode the URL-encoded sanitized path back to normal characters - const decodedPath = decodeURIComponent(pdfEmbed.sanitizedPath); - - // Try multiple path resolution strategies - const possiblePaths = [ - // Strategy 1: Relative to vault root (standard Obsidian behavior) - pathModule.resolve(vaultBasePath, decodedPath), - // Strategy 2: Relative to current file's directory (for local attachments) - currentFile ? pathModule.resolve(vaultBasePath, pathModule.dirname(currentFile.path), decodedPath) : null - ].filter(p => p !== null); - - let fullPdfPath = 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)) { - fullPdfPath = possiblePath; - console.log(`Export: Found PDF at: ${fullPdfPath}`); - break; - } - } + // Resolve PDF path using helper method + const fullPdfPath = this.resolvePdfPath(pdfEmbed.sanitizedPath, vaultBasePath, currentFile); if (!fullPdfPath) { - console.warn(`Export: PDF file not found: ${decodedPath}`); + console.warn(`Export: PDF file not found: ${decodeURIComponent(pdfEmbed.sanitizedPath)}`); // Replace marker with fallback message const fallbackOutput = `*⚠️ PDF not found: ${pdfEmbed.baseName}*`; updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);