From ee79ed3f742c962c2f3ebd1903147ef440a84ee5 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 25 Sep 2025 12:27:33 +0200 Subject: [PATCH] refactor: migrate ResourcePathResolver.ts to use vault.adapter - Replace all fs operations with vault.adapter methods - Replace fs.readdirSync with vault.adapter.list for directory scanning - Replace fs.existsSync with PathUtils.fileExists - Replace path.join with PathUtils.joinPath for vault paths - Remove fs/fsPromises imports, add PathUtils import - Add plugin parameter to constructor for vault.adapter access - Update PandocCommandBuilder to pass plugin to ResourcePathResolver - Maintain resource discovery functionality for Pandoc --- src/converters/pandoc/PandocCommandBuilder.ts | 2 +- src/converters/pandoc/ResourcePathResolver.ts | 67 ++++++++++--------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/converters/pandoc/PandocCommandBuilder.ts b/src/converters/pandoc/PandocCommandBuilder.ts index 8976e61..577cd50 100644 --- a/src/converters/pandoc/PandocCommandBuilder.ts +++ b/src/converters/pandoc/PandocCommandBuilder.ts @@ -20,7 +20,7 @@ export class PandocCommandBuilder { constructor(plugin: obsidianTypstPDFExport) { this.plugin = plugin; this.variableMapper = new TypstVariableMapper(plugin); - this.resourcePathResolver = new ResourcePathResolver(); + this.resourcePathResolver = new ResourcePathResolver(plugin); this.pathResolver = new PathResolver(plugin); } diff --git a/src/converters/pandoc/ResourcePathResolver.ts b/src/converters/pandoc/ResourcePathResolver.ts index f1220d7..5f3edc2 100644 --- a/src/converters/pandoc/ResourcePathResolver.ts +++ b/src/converters/pandoc/ResourcePathResolver.ts @@ -1,36 +1,41 @@ -import * as path from 'path'; -import { promises as fsPromises } from 'fs'; +import { PathUtils } from '../../core/PathUtils'; +import type { obsidianTypstPDFExport } from '../../../main'; /** * Handles resource path resolution and caching for Pandoc conversions. * Discovers and caches attachment directories to help Pandoc find embedded resources. */ export class ResourcePathResolver { + private plugin: obsidianTypstPDFExport | undefined; private resourcePathCache: string[] = []; private resourcePathCacheTimestamp: number = 0; private resourcePathCacheVaultPath: string = ''; private readonly CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes + constructor(plugin?: obsidianTypstPDFExport) { + this.plugin = plugin; + } + /** * Get all resource paths for the given vault, using cache when possible * @param vaultBasePath The base path of the vault * @returns Array of resource paths for Pandoc to search */ async getResourcePaths(vaultBasePath: string): Promise { - if (!vaultBasePath) { + if (!vaultBasePath || !this.plugin?.app) { return []; } - const fs = require('fs'); + const pathUtils = new PathUtils(this.plugin.app); const resourcePaths: string[] = []; // Add vault root as primary resource path resourcePaths.push(vaultBasePath); // Add common attachment directories - const commonPaths = this.getCommonAttachmentPaths(vaultBasePath); + const commonPaths = this.getCommonAttachmentPaths(vaultBasePath, pathUtils); for (const attachPath of commonPaths) { - if (fs.existsSync(attachPath)) { + if (await pathUtils.fileExists(attachPath)) { resourcePaths.push(attachPath); } } @@ -42,7 +47,7 @@ export class ResourcePathResolver { resourcePaths.push(...this.resourcePathCache); } else { // Cache is invalid or expired, perform fresh scan - const scannedPaths = await this.scanForResourcePaths(vaultBasePath, fs, now); + const scannedPaths = await this.scanForResourcePaths(vaultBasePath, pathUtils, now); resourcePaths.push(...scannedPaths); } @@ -52,15 +57,16 @@ export class ResourcePathResolver { /** * Get common attachment directory paths that Obsidian users typically use * @param vaultBasePath The base path of the vault + * @param pathUtils PathUtils instance for path operations * @returns Array of common attachment directory paths */ - private getCommonAttachmentPaths(vaultBasePath: string): string[] { + private getCommonAttachmentPaths(vaultBasePath: string, pathUtils: PathUtils): string[] { return [ - path.join(vaultBasePath, 'attachments'), - path.join(vaultBasePath, 'assets'), - path.join(vaultBasePath, 'files'), - path.join(vaultBasePath, 'images'), - path.join(vaultBasePath, '.attachments') + pathUtils.joinPath(vaultBasePath, 'attachments'), + pathUtils.joinPath(vaultBasePath, 'assets'), + pathUtils.joinPath(vaultBasePath, 'files'), + pathUtils.joinPath(vaultBasePath, 'images'), + pathUtils.joinPath(vaultBasePath, '.attachments') ]; } @@ -79,36 +85,31 @@ export class ResourcePathResolver { /** * Scan the vault for directories containing images and cache the results * @param vaultBasePath The base path of the vault - * @param fs File system module + * @param pathUtils PathUtils instance for path operations * @param now Current timestamp * @returns Array of resource paths found during scan */ - private async scanForResourcePaths(vaultBasePath: string, fs: typeof import('fs'), now: number): Promise { + private async scanForResourcePaths(vaultBasePath: string, pathUtils: PathUtils, now: number): Promise { const foundResourcePaths: string[] = []; - + try { - const vaultContents = fs.readdirSync(vaultBasePath); - for (const item of vaultContents) { - const itemPath = path.join(vaultBasePath, item); - - let stat; - try { - stat = await fsPromises.stat(itemPath); - } catch (error) { - // File might have been deleted between readdirSync and statSync - console.warn(`Export: Unable to stat ${itemPath}:`, (error as Error).message); - continue; - } - - if (stat.isDirectory() && !item.startsWith('.') && !item.startsWith('_')) { + if (!this.plugin?.app) { + return foundResourcePaths; + } + + const vaultContents = await this.plugin.app.vault.adapter.list(vaultBasePath); + for (const item of vaultContents.folders) { + const itemName = item.split('/').pop() || item; + + if (!itemName.startsWith('.') && !itemName.startsWith('_')) { // Check if this directory contains images try { - const dirContents = fs.readdirSync(itemPath); - const hasImages = dirContents.some((file: string) => + const dirContents = await this.plugin.app.vault.adapter.list(item); + const hasImages = dirContents.files.some((file: string) => /\.(png|jpg|jpeg|gif|svg|webp|bmp|ico|tiff)$/i.test(file) ); if (hasImages) { - foundResourcePaths.push(itemPath); + foundResourcePaths.push(item); } } catch { // Ignore directories we can't read