refactor: extract ResourcePathResolver from PandocCommandBuilder
Create dedicated ResourcePathResolver to handle resource path discovery and caching: - Extract resource path caching logic (~110 lines) - Move vault scanning and attachment directory discovery - Eliminate code duplication: remove duplicate resolveExecutablePath() - Simplify PandocCommandBuilder from ~287 to ~204 lines Changes: - NEW: src/converters/pandoc/ResourcePathResolver.ts - MODIFIED: PandocCommandBuilder - use ResourcePathResolver and PathResolver - MODIFIED: PandocTypstConverter - use centralized PathResolver Benefits: - Clear separation of concerns - Reusable resource path logic - Reduced code duplication - Better maintainability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
3 files changed
+154
-124
No files matched your search
@@ -0,0 +1,138 @@
|
||||
import * as path from 'path';
|
||||
import { promises as fsPromises } from 'fs';
|
||||
|
||||
/**
|
||||
* Handles resource path resolution and caching for Pandoc conversions.
|
||||
* Discovers and caches attachment directories to help Pandoc find embedded resources.
|
||||
*/
|
||||
export class ResourcePathResolver {
|
||||
private resourcePathCache: string[] = [];
|
||||
private resourcePathCacheTimestamp: number = 0;
|
||||
private resourcePathCacheVaultPath: string = '';
|
||||
private readonly CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
/**
|
||||
* 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<string[]> {
|
||||
if (!vaultBasePath) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const fs = require('fs');
|
||||
const resourcePaths: string[] = [];
|
||||
|
||||
// Add vault root as primary resource path
|
||||
resourcePaths.push(vaultBasePath);
|
||||
|
||||
// Add common attachment directories
|
||||
const commonPaths = this.getCommonAttachmentPaths(vaultBasePath);
|
||||
for (const attachPath of commonPaths) {
|
||||
if (fs.existsSync(attachPath)) {
|
||||
resourcePaths.push(attachPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Add cached or freshly scanned resource paths
|
||||
const now = Date.now();
|
||||
if (this.isCacheValid(vaultBasePath, now)) {
|
||||
// Use cached resource paths
|
||||
resourcePaths.push(...this.resourcePathCache);
|
||||
} else {
|
||||
// Cache is invalid or expired, perform fresh scan
|
||||
const scannedPaths = await this.scanForResourcePaths(vaultBasePath, fs, now);
|
||||
resourcePaths.push(...scannedPaths);
|
||||
}
|
||||
|
||||
return resourcePaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get common attachment directory paths that Obsidian users typically use
|
||||
* @param vaultBasePath The base path of the vault
|
||||
* @returns Array of common attachment directory paths
|
||||
*/
|
||||
private getCommonAttachmentPaths(vaultBasePath: string): string[] {
|
||||
return [
|
||||
path.join(vaultBasePath, 'attachments'),
|
||||
path.join(vaultBasePath, 'assets'),
|
||||
path.join(vaultBasePath, 'files'),
|
||||
path.join(vaultBasePath, 'images'),
|
||||
path.join(vaultBasePath, '.attachments')
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the resource path cache is still valid
|
||||
* @param vaultBasePath The vault path to check against
|
||||
* @param now Current timestamp
|
||||
* @returns True if cache is valid and can be used
|
||||
*/
|
||||
private isCacheValid(vaultBasePath: string, now: number): boolean {
|
||||
return this.resourcePathCacheVaultPath === vaultBasePath &&
|
||||
(now - this.resourcePathCacheTimestamp) < this.CACHE_EXPIRY_MS &&
|
||||
this.resourcePathCache.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 now Current timestamp
|
||||
* @returns Array of resource paths found during scan
|
||||
*/
|
||||
private async scanForResourcePaths(vaultBasePath: string, fs: any, now: number): Promise<string[]> {
|
||||
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('_')) {
|
||||
// Check if this directory contains images
|
||||
try {
|
||||
const dirContents = fs.readdirSync(itemPath);
|
||||
const hasImages = dirContents.some((file: string) =>
|
||||
/\.(png|jpg|jpeg|gif|svg|webp|bmp|ico|tiff)$/i.test(file)
|
||||
);
|
||||
if (hasImages) {
|
||||
foundResourcePaths.push(itemPath);
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore directories we can't read
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update cache with fresh results
|
||||
this.resourcePathCache = foundResourcePaths;
|
||||
this.resourcePathCacheTimestamp = now;
|
||||
this.resourcePathCacheVaultPath = vaultBasePath;
|
||||
} catch (e) {
|
||||
console.warn('Could not scan vault for attachment directories:', e);
|
||||
}
|
||||
|
||||
return foundResourcePaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the resource path cache
|
||||
*/
|
||||
clearCache(): void {
|
||||
this.resourcePathCache = [];
|
||||
this.resourcePathCacheTimestamp = 0;
|
||||
this.resourcePathCacheVaultPath = '';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user