Improves export performance with resource path caching

Implements a cache for resource paths to avoid rescanning the vault directory on every export.

This significantly improves the speed of subsequent exports,
especially when the vault contains a large number of files. The cache
expires after a configurable interval (default: 5 minutes) to ensure
that new or removed resources are eventually reflected.
This commit is contained in:
AlexKucera committed 2025-09-04 15:17:35 +02:00
1 parent 6af3f74e68
commit 1b3f83e6e6
1 file changed
+30
+30
View File
@@ -18,6 +18,12 @@ export class PandocTypstConverter {
private cleanupHandlers: (() => void)[] = [];
private plugin: any; // Will be properly typed when we refactor the main plugin class
// Cache for directory scanning optimization
private resourcePathCache: string[] = [];
private resourcePathCacheTimestamp: number = 0;
private resourcePathCacheVaultPath: string = '';
private readonly CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes
/**
* Create a new PandocTypstConverter instance
* @param plugin The main plugin instance for accessing settings
@@ -301,6 +307,22 @@ export class PandocTypstConverter {
}
// Also scan for note-specific attachment folders (Obsidian often creates these)
// Use cache to avoid scanning entire vault on every export
const now = Date.now();
const cacheIsValid = this.resourcePathCacheVaultPath === this.pandocOptions.vaultBasePath &&
(now - this.resourcePathCacheTimestamp) < this.CACHE_EXPIRY_MS &&
this.resourcePathCache.length > 0;
if (cacheIsValid) {
// Use cached resource paths
for (const cachedPath of this.resourcePathCache) {
args.push('--resource-path', cachedPath);
console.log('Added cached resource path:', cachedPath);
}
} else {
// Cache is invalid or expired, perform fresh scan
const foundResourcePaths: string[] = [];
try {
const vaultContents = fs.readdirSync(this.pandocOptions.vaultBasePath);
for (const item of vaultContents) {
@@ -323,6 +345,7 @@ export class PandocTypstConverter {
/\.(png|jpg|jpeg|gif|svg|webp|bmp|ico|tiff)$/i.test(file)
);
if (hasImages) {
foundResourcePaths.push(itemPath);
args.push('--resource-path', itemPath);
console.log('Added note-specific resource path:', itemPath);
}
@@ -331,6 +354,13 @@ export class PandocTypstConverter {
}
}
}
// Update cache with fresh results
this.resourcePathCache = foundResourcePaths;
this.resourcePathCacheTimestamp = now;
this.resourcePathCacheVaultPath = this.pandocOptions.vaultBasePath;
console.log(`Export: Cached ${foundResourcePaths.length} resource paths for future use`);
}
} catch (e) {
console.warn('Could not scan vault for attachment directories:', e);
}