Refactors path conversion to simplify logic

Replaces manual conversion of absolute paths to vault-relative paths with a utility method to improve code readability and maintainability across various modules. Removes redundant code and streamlines path handling operations.

This refactoring helps ensure consistent path management and reduces potential errors related to path conversion.

Relates to obsidian-bot-review
This commit is contained in:
AlexKucera committed 2025-11-08 19:01:42 +01:00
1 parent ec36b6334f
commit d56cb98d51
6 files changed
+14 -91

No files matched your search

+1 -9
View File
@@ -36,15 +36,7 @@ export class TemplateManager {
}
// Read directory contents - convert absolute path to vault-relative
const vaultBasePath = this.pathUtils.getVaultPath();
let vaultRelativePath = this.templatesPath;
// Convert absolute path to vault-relative path
if (this.templatesPath.startsWith(vaultBasePath)) {
vaultRelativePath = this.templatesPath.substring(vaultBasePath.length);
// Remove leading separators
vaultRelativePath = vaultRelativePath.replace(/^[/\\]+/, '');
}
const vaultRelativePath = this.pathUtils.toVaultRelative(this.templatesPath);
const list = await this.plugin.app.vault.adapter.list(vaultRelativePath);
const files = list.files.map(filePath => path.basename(filePath));
+2 -16
View File
@@ -58,14 +58,7 @@ export class EmbeddedTemplateManager {
try {
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
const vaultPath = this.pathUtils.getVaultPath();
let relativeTemplatesDir = this.templatesDir;
if (this.templatesDir.startsWith(vaultPath)) {
relativeTemplatesDir = this.templatesDir.substring(vaultPath.length);
if (relativeTemplatesDir.startsWith('/') || relativeTemplatesDir.startsWith('\\')) {
relativeTemplatesDir = relativeTemplatesDir.substring(1);
}
}
const relativeTemplatesDir = this.pathUtils.toVaultRelative(this.templatesDir);
await this.pathUtils.ensureDir(relativeTemplatesDir);
// Extract template content
@@ -114,14 +107,7 @@ export class EmbeddedTemplateManager {
const embeddedNames = getAllTemplateNames();
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
const vaultPath = this.pathUtils.getVaultPath();
let relativeTemplatesDir = this.templatesDir;
if (this.templatesDir.startsWith(vaultPath)) {
relativeTemplatesDir = this.templatesDir.substring(vaultPath.length);
if (relativeTemplatesDir.startsWith('/') || relativeTemplatesDir.startsWith('\\')) {
relativeTemplatesDir = relativeTemplatesDir.substring(1);
}
}
const relativeTemplatesDir = this.pathUtils.toVaultRelative(this.templatesDir);
await this.pathUtils.ensureDir(relativeTemplatesDir);
for (const templateName of embeddedNames) {