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
This commit is contained in:
Jarvis committed 2025-09-25 12:27:33 +02:00
1 parent f8142f995a
commit ee79ed3f74
2 files changed
+35 -34

No files matched your search

@@ -20,7 +20,7 @@ export class PandocCommandBuilder {
constructor(plugin: obsidianTypstPDFExport) { constructor(plugin: obsidianTypstPDFExport) {
this.plugin = plugin; this.plugin = plugin;
this.variableMapper = new TypstVariableMapper(plugin); this.variableMapper = new TypstVariableMapper(plugin);
this.resourcePathResolver = new ResourcePathResolver(); this.resourcePathResolver = new ResourcePathResolver(plugin);
this.pathResolver = new PathResolver(plugin); this.pathResolver = new PathResolver(plugin);
} }
+34 -33
View File
@@ -1,36 +1,41 @@
import * as path from 'path'; import { PathUtils } from '../../core/PathUtils';
import { promises as fsPromises } from 'fs'; import type { obsidianTypstPDFExport } from '../../../main';
/** /**
* Handles resource path resolution and caching for Pandoc conversions. * Handles resource path resolution and caching for Pandoc conversions.
* Discovers and caches attachment directories to help Pandoc find embedded resources. * Discovers and caches attachment directories to help Pandoc find embedded resources.
*/ */
export class ResourcePathResolver { export class ResourcePathResolver {
private plugin: obsidianTypstPDFExport | undefined;
private resourcePathCache: string[] = []; private resourcePathCache: string[] = [];
private resourcePathCacheTimestamp: number = 0; private resourcePathCacheTimestamp: number = 0;
private resourcePathCacheVaultPath: string = ''; private resourcePathCacheVaultPath: string = '';
private readonly CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes 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 * Get all resource paths for the given vault, using cache when possible
* @param vaultBasePath The base path of the vault * @param vaultBasePath The base path of the vault
* @returns Array of resource paths for Pandoc to search * @returns Array of resource paths for Pandoc to search
*/ */
async getResourcePaths(vaultBasePath: string): Promise<string[]> { async getResourcePaths(vaultBasePath: string): Promise<string[]> {
if (!vaultBasePath) { if (!vaultBasePath || !this.plugin?.app) {
return []; return [];
} }
const fs = require('fs'); const pathUtils = new PathUtils(this.plugin.app);
const resourcePaths: string[] = []; const resourcePaths: string[] = [];
// Add vault root as primary resource path // Add vault root as primary resource path
resourcePaths.push(vaultBasePath); resourcePaths.push(vaultBasePath);
// Add common attachment directories // Add common attachment directories
const commonPaths = this.getCommonAttachmentPaths(vaultBasePath); const commonPaths = this.getCommonAttachmentPaths(vaultBasePath, pathUtils);
for (const attachPath of commonPaths) { for (const attachPath of commonPaths) {
if (fs.existsSync(attachPath)) { if (await pathUtils.fileExists(attachPath)) {
resourcePaths.push(attachPath); resourcePaths.push(attachPath);
} }
} }
@@ -42,7 +47,7 @@ export class ResourcePathResolver {
resourcePaths.push(...this.resourcePathCache); resourcePaths.push(...this.resourcePathCache);
} else { } else {
// Cache is invalid or expired, perform fresh scan // 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); resourcePaths.push(...scannedPaths);
} }
@@ -52,15 +57,16 @@ export class ResourcePathResolver {
/** /**
* Get common attachment directory paths that Obsidian users typically use * Get common attachment directory paths that Obsidian users typically use
* @param vaultBasePath The base path of the vault * @param vaultBasePath The base path of the vault
* @param pathUtils PathUtils instance for path operations
* @returns Array of common attachment directory paths * @returns Array of common attachment directory paths
*/ */
private getCommonAttachmentPaths(vaultBasePath: string): string[] { private getCommonAttachmentPaths(vaultBasePath: string, pathUtils: PathUtils): string[] {
return [ return [
path.join(vaultBasePath, 'attachments'), pathUtils.joinPath(vaultBasePath, 'attachments'),
path.join(vaultBasePath, 'assets'), pathUtils.joinPath(vaultBasePath, 'assets'),
path.join(vaultBasePath, 'files'), pathUtils.joinPath(vaultBasePath, 'files'),
path.join(vaultBasePath, 'images'), pathUtils.joinPath(vaultBasePath, 'images'),
path.join(vaultBasePath, '.attachments') pathUtils.joinPath(vaultBasePath, '.attachments')
]; ];
} }
@@ -79,36 +85,31 @@ export class ResourcePathResolver {
/** /**
* Scan the vault for directories containing images and cache the results * Scan the vault for directories containing images and cache the results
* @param vaultBasePath The base path of the vault * @param vaultBasePath The base path of the vault
* @param fs File system module * @param pathUtils PathUtils instance for path operations
* @param now Current timestamp * @param now Current timestamp
* @returns Array of resource paths found during scan * @returns Array of resource paths found during scan
*/ */
private async scanForResourcePaths(vaultBasePath: string, fs: typeof import('fs'), now: number): Promise<string[]> { private async scanForResourcePaths(vaultBasePath: string, pathUtils: PathUtils, now: number): Promise<string[]> {
const foundResourcePaths: string[] = []; const foundResourcePaths: string[] = [];
try { try {
const vaultContents = fs.readdirSync(vaultBasePath); if (!this.plugin?.app) {
for (const item of vaultContents) { return foundResourcePaths;
const itemPath = path.join(vaultBasePath, item); }
let stat; const vaultContents = await this.plugin.app.vault.adapter.list(vaultBasePath);
try { for (const item of vaultContents.folders) {
stat = await fsPromises.stat(itemPath); const itemName = item.split('/').pop() || item;
} catch (error) {
// File might have been deleted between readdirSync and statSync if (!itemName.startsWith('.') && !itemName.startsWith('_')) {
console.warn(`Export: Unable to stat ${itemPath}:`, (error as Error).message);
continue;
}
if (stat.isDirectory() && !item.startsWith('.') && !item.startsWith('_')) {
// Check if this directory contains images // Check if this directory contains images
try { try {
const dirContents = fs.readdirSync(itemPath); const dirContents = await this.plugin.app.vault.adapter.list(item);
const hasImages = dirContents.some((file: string) => const hasImages = dirContents.files.some((file: string) =>
/\.(png|jpg|jpeg|gif|svg|webp|bmp|ico|tiff)$/i.test(file) /\.(png|jpg|jpeg|gif|svg|webp|bmp|ico|tiff)$/i.test(file)
); );
if (hasImages) { if (hasImages) {
foundResourcePaths.push(itemPath); foundResourcePaths.push(item);
} }
} catch { } catch {
// Ignore directories we can't read // Ignore directories we can't read