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
Showing only changes of commit 4498281b7d - Show all commits
+20 -19
View File
@@ -4,19 +4,21 @@
*/
import { obsidianTypstPDFExport } from '../../main';
import { PathUtils } from '../core/PathUtils';
import * as path from 'path';
import * as fs from 'fs';
export class TemplateManager {
private plugin: obsidianTypstPDFExport;
private templatesPath: string;
private pathUtils: PathUtils;
constructor(plugin: obsidianTypstPDFExport) {
this.plugin = plugin;
this.pathUtils = new PathUtils(plugin.app);
// Templates are stored in the plugin directory
this.templatesPath = path.join(
(this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath,
this.plugin.manifest.dir!,
this.templatesPath = this.pathUtils.joinPath(
this.pathUtils.getVaultPath(),
this.pathUtils.getPluginDir(this.plugin.manifest),
'templates'
);
}
@@ -27,24 +29,26 @@ export class TemplateManager {
async getAvailableTemplates(): Promise<string[]> {
coderabbitai[bot] commented 2025-09-25 13:05:41 +02:00 (Migrated from github.com)
Review

⚠️ Potential issue | 🟠 Major

List templates using a vault-relative path (adapter.list).

adapter.list works most reliably with vault-relative paths.

-      const list = await this.plugin.app.vault.adapter.list(this.templatesPath);
+      const templatesRelDir = this.pathUtils.joinPath(
+        this.pathUtils.getPluginDir(this.plugin.manifest),
+        'templates'
+      );
+      const list = await this.plugin.app.vault.adapter.list(templatesRelDir);
       const files = list.files.map(filePath => path.basename(filePath));
📝 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 templatesRelDir = this.pathUtils.joinPath(
				this.pathUtils.getPluginDir(this.plugin.manifest),
				'templates'
			);
			const list = await this.plugin.app.vault.adapter.list(templatesRelDir);
			const files = list.files.map(filePath => path.basename(filePath));
🤖 Prompt for AI Agents
In src/templates/TemplateManager.ts around lines 39-41, adapter.list is being
called with this.templatesPath which may not be vault-relative; change the call
to pass a vault-relative path (e.g. compute const vaultPath =
this.plugin.app.vault.getRelativePath?.(this.templatesPath) ??
this.templatesPath.replace(/^\/+/, '') or otherwise strip any
absolute/vault-root prefix) and then call await
this.plugin.app.vault.adapter.list(vaultPath); ensure you use the vault-relative
path variable in the subsequent map call.

✅ Addressed in commits a129380 to 731a76e

_⚠️ Potential issue_ | _🟠 Major_ **List templates using a vault-relative path (adapter.list).** `adapter.list` works most reliably with vault-relative paths. ```diff - const list = await this.plugin.app.vault.adapter.list(this.templatesPath); + const templatesRelDir = this.pathUtils.joinPath( + this.pathUtils.getPluginDir(this.plugin.manifest), + 'templates' + ); + const list = await this.plugin.app.vault.adapter.list(templatesRelDir); const files = list.files.map(filePath => path.basename(filePath)); ``` <!-- 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 templatesRelDir = this.pathUtils.joinPath( this.pathUtils.getPluginDir(this.plugin.manifest), 'templates' ); const list = await this.plugin.app.vault.adapter.list(templatesRelDir); const files = list.files.map(filePath => path.basename(filePath)); ``` </details> <!-- suggestion_end --> <details> <summary>🤖 Prompt for AI Agents</summary> ``` In src/templates/TemplateManager.ts around lines 39-41, adapter.list is being called with this.templatesPath which may not be vault-relative; change the call to pass a vault-relative path (e.g. compute const vaultPath = this.plugin.app.vault.getRelativePath?.(this.templatesPath) ?? this.templatesPath.replace(/^\/+/, '') or otherwise strip any absolute/vault-root prefix) and then call await this.plugin.app.vault.adapter.list(vaultPath); ensure you use the vault-relative path variable in the subsequent map call. ``` </details> <!-- fingerprinting:phantom:poseidon:chinchilla --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commits a129380 to 731a76e
try {
// Check if templates directory exists
if (!fs.existsSync(this.templatesPath)) {
const exists = await this.pathUtils.fileExists(this.templatesPath);
if (!exists) {
console.warn('Templates directory does not exist:', this.templatesPath);
return ['default.typ'];
}
// Read directory contents
const files = fs.readdirSync(this.templatesPath);
const list = await this.plugin.app.vault.adapter.list(this.templatesPath);
const files = list.files.map(filePath => path.basename(filePath));
// Filter for .typ and .pandoc.typ files
const templates = files.filter(file => {
return file.endsWith('.typ') || file.endsWith('.pandoc.typ');
});
// Always include default if not present
if (!templates.includes('default.typ')) {
templates.unshift('default.typ');
}
return templates;
} catch (error) {
console.error('Error loading templates:', error);
@@ -56,7 +60,7 @@ export class TemplateManager {
* Get the full path to a template file
*/
getTemplatePath(templateName: string): string {
return path.join(this.templatesPath, templateName);
return this.pathUtils.joinPath(this.templatesPath, templateName);
}
/**
@@ -64,11 +68,7 @@ export class TemplateManager {
*/
async templateExists(templateName: string): Promise<boolean> {
const templatePath = this.getTemplatePath(templateName);
try {
return fs.existsSync(templatePath);
} catch {
return false;
}
return await this.pathUtils.fileExists(templatePath);
}
/**
@@ -77,8 +77,9 @@ export class TemplateManager {
async loadTemplate(templateName: string): Promise<string | null> {
const templatePath = this.getTemplatePath(templateName);
try {
if (fs.existsSync(templatePath)) {
return fs.readFileSync(templatePath, 'utf-8');
const exists = await this.pathUtils.fileExists(templatePath);
if (exists) {
return await this.plugin.app.vault.adapter.read(templatePath);
}
} catch (error) {
console.error('Error loading template:', error);