Enhances path handling and file resolution #5

Merged
AlexKucera merged 36 commits from 1.3.0 into master 2025-09-25 16:47:12 +02:00
2 changed files with 35 additions and 34 deletions
Showing only changes of commit ee79ed3f74 - Show all commits

No files matched your search

@@ -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);
}
1
+34 -33
View File
@@ -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<string[]> {
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 {
/**
coderabbitai[bot] commented 2025-09-25 13:05:39 +02:00 (Migrated from github.com)
Review

⚠️ Potential issue | 🔴 Critical

Fix: adapter.list must take a vault‑relative path; also return absolute paths for consistency.

  • FileSystemAdapter.list expects a vault‑relative path ('' or '/'), not an absolute filesystem path.
  • Return absolute paths from scan to match other entries and avoid reliance on Pandoc’s cwd.

Apply this diff:

-			const vaultContents = await this.plugin.app.vault.adapter.list(vaultBasePath);
+			const vaultContents = await this.plugin.app.vault.adapter.list('');
 			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 = 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(item);
+							foundResourcePaths.push(pathUtils.joinPath(vaultBasePath, item));
 						}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

			const vaultContents = await this.plugin.app.vault.adapter.list('');
			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 = 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(pathUtils.joinPath(vaultBasePath, item));

✅ Addressed in commits 3782f0a to d20e8cf

_⚠️ Potential issue_ | _🔴 Critical_ **Fix: adapter.list must take a vault‑relative path; also return absolute paths for consistency.** - FileSystemAdapter.list expects a vault‑relative path ('' or '/'), not an absolute filesystem path. - Return absolute paths from scan to match other entries and avoid reliance on Pandoc’s cwd. Apply this diff: ```diff - const vaultContents = await this.plugin.app.vault.adapter.list(vaultBasePath); + const vaultContents = await this.plugin.app.vault.adapter.list(''); 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 = 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(item); + foundResourcePaths.push(pathUtils.joinPath(vaultBasePath, item)); } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion const vaultContents = await this.plugin.app.vault.adapter.list(''); 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 = 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(pathUtils.joinPath(vaultBasePath, item)); ``` </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:medusa:chinchilla --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commits 3782f0a to d20e8cf
* 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<string[]> {
private async scanForResourcePaths(vaultBasePath: string, pathUtils: PathUtils, 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('_')) {
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