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:
6 files changed
+14
-91
No files matched your search
@@ -416,15 +416,7 @@ export class PdfToImageConverter {
|
|||||||
// Ensure output directory exists (convert absolute path to vault-relative for vault.adapter)
|
// Ensure output directory exists (convert absolute path to vault-relative for vault.adapter)
|
||||||
if (this.plugin?.app) {
|
if (this.plugin?.app) {
|
||||||
const pathUtils = new PathUtils(this.plugin.app);
|
const pathUtils = new PathUtils(this.plugin.app);
|
||||||
const vaultPath = pathUtils.getVaultPath();
|
const relativeOutputDir = pathUtils.toVaultRelative(outputDir);
|
||||||
// Convert absolute path to vault-relative
|
|
||||||
let relativeOutputDir = outputDir;
|
|
||||||
if (outputDir.startsWith(vaultPath)) {
|
|
||||||
relativeOutputDir = outputDir.substring(vaultPath.length);
|
|
||||||
if (relativeOutputDir.startsWith('/') || relativeOutputDir.startsWith('\\')) {
|
|
||||||
relativeOutputDir = relativeOutputDir.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await pathUtils.ensureDir(relativeOutputDir);
|
await pathUtils.ensureDir(relativeOutputDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -218,19 +218,8 @@ export class ImageOptimizer {
|
|||||||
// Clean up the original PNG if conversion succeeded
|
// Clean up the original PNG if conversion succeeded
|
||||||
try {
|
try {
|
||||||
if (plugin?.app) {
|
if (plugin?.app) {
|
||||||
// Convert absolute path to vault-relative path for adapter.remove()
|
|
||||||
const pathUtils = new PathUtils(plugin.app);
|
const pathUtils = new PathUtils(plugin.app);
|
||||||
const vaultBasePath = pathUtils.getVaultPath();
|
const relativePath = pathUtils.toVaultRelative(inputImagePath);
|
||||||
|
|
||||||
let relativePath = inputImagePath;
|
|
||||||
if (inputImagePath.startsWith(vaultBasePath)) {
|
|
||||||
// Remove vault base path and leading separator
|
|
||||||
relativePath = inputImagePath.substring(vaultBasePath.length);
|
|
||||||
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
|
|
||||||
relativePath = relativePath.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await plugin.app.vault.adapter.remove(relativePath);
|
await plugin.app.vault.adapter.remove(relativePath);
|
||||||
} else {
|
} else {
|
||||||
await fs.unlink(inputImagePath);
|
await fs.unlink(inputImagePath);
|
||||||
@@ -244,27 +233,9 @@ export class ImageOptimizer {
|
|||||||
// Just rename the file to PNG format
|
// Just rename the file to PNG format
|
||||||
const pngFinalPath = finalOutputPath.replace(/\.jpeg?$/, '.png');
|
const pngFinalPath = finalOutputPath.replace(/\.jpeg?$/, '.png');
|
||||||
if (plugin?.app) {
|
if (plugin?.app) {
|
||||||
// Convert absolute paths to vault-relative paths for adapter operations
|
|
||||||
const pathUtils = new PathUtils(plugin.app);
|
const pathUtils = new PathUtils(plugin.app);
|
||||||
const vaultBasePath = pathUtils.getVaultPath();
|
const relativeInputPath = pathUtils.toVaultRelative(inputImagePath);
|
||||||
|
const relativePngPath = pathUtils.toVaultRelative(pngFinalPath);
|
||||||
let relativeInputPath = inputImagePath;
|
|
||||||
if (inputImagePath.startsWith(vaultBasePath)) {
|
|
||||||
// Remove vault base path and leading separator
|
|
||||||
relativeInputPath = inputImagePath.substring(vaultBasePath.length);
|
|
||||||
if (relativeInputPath.startsWith('/') || relativeInputPath.startsWith('\\')) {
|
|
||||||
relativeInputPath = relativeInputPath.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let relativePngPath = pngFinalPath;
|
|
||||||
if (pngFinalPath.startsWith(vaultBasePath)) {
|
|
||||||
// Remove vault base path and leading separator
|
|
||||||
relativePngPath = pngFinalPath.substring(vaultBasePath.length);
|
|
||||||
if (relativePngPath.startsWith('/') || relativePngPath.startsWith('\\')) {
|
|
||||||
relativePngPath = relativePngPath.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const content = await plugin.app.vault.adapter.readBinary(relativeInputPath);
|
const content = await plugin.app.vault.adapter.readBinary(relativeInputPath);
|
||||||
await plugin.app.vault.adapter.writeBinary(relativePngPath, content);
|
await plugin.app.vault.adapter.writeBinary(relativePngPath, content);
|
||||||
@@ -277,27 +248,9 @@ export class ImageOptimizer {
|
|||||||
} else {
|
} else {
|
||||||
// Keep as PNG, just rename to final location
|
// Keep as PNG, just rename to final location
|
||||||
if (plugin?.app) {
|
if (plugin?.app) {
|
||||||
// Convert absolute paths to vault-relative paths for adapter operations
|
|
||||||
const pathUtils = new PathUtils(plugin.app);
|
const pathUtils = new PathUtils(plugin.app);
|
||||||
const vaultBasePath = pathUtils.getVaultPath();
|
const relativeInputPath = pathUtils.toVaultRelative(inputImagePath);
|
||||||
|
const relativeFinalPath = pathUtils.toVaultRelative(finalOutputPath);
|
||||||
let relativeInputPath = inputImagePath;
|
|
||||||
if (inputImagePath.startsWith(vaultBasePath)) {
|
|
||||||
// Remove vault base path and leading separator
|
|
||||||
relativeInputPath = inputImagePath.substring(vaultBasePath.length);
|
|
||||||
if (relativeInputPath.startsWith('/') || relativeInputPath.startsWith('\\')) {
|
|
||||||
relativeInputPath = relativeInputPath.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let relativeFinalPath = finalOutputPath;
|
|
||||||
if (finalOutputPath.startsWith(vaultBasePath)) {
|
|
||||||
// Remove vault base path and leading separator
|
|
||||||
relativeFinalPath = finalOutputPath.substring(vaultBasePath.length);
|
|
||||||
if (relativeFinalPath.startsWith('/') || relativeFinalPath.startsWith('\\')) {
|
|
||||||
relativeFinalPath = relativeFinalPath.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const content = await plugin.app.vault.adapter.readBinary(relativeInputPath);
|
const content = await plugin.app.vault.adapter.readBinary(relativeInputPath);
|
||||||
await plugin.app.vault.adapter.writeBinary(relativeFinalPath, content);
|
await plugin.app.vault.adapter.writeBinary(relativeFinalPath, content);
|
||||||
|
|||||||
@@ -271,11 +271,11 @@ export class PathUtils {
|
|||||||
* const pathUtils = new PathUtils(app);
|
* const pathUtils = new PathUtils(app);
|
||||||
* const vaultPath = '/Users/name/vault';
|
* const vaultPath = '/Users/name/vault';
|
||||||
* const absolutePath = '/Users/name/vault/folder/file.md';
|
* const absolutePath = '/Users/name/vault/folder/file.md';
|
||||||
* const relative = pathUtils.toVaultRelativePath(absolutePath);
|
* const relative = pathUtils.toVaultRelative(absolutePath);
|
||||||
* // Returns: 'folder/file.md'
|
* // Returns: 'folder/file.md'
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
toVaultRelativePath(absolutePath: string): string {
|
toVaultRelative(absolutePath: string): string {
|
||||||
const vaultBasePath = this.getVaultPath();
|
const vaultBasePath = this.getVaultPath();
|
||||||
if (!absolutePath.startsWith(vaultBasePath)) {
|
if (!absolutePath.startsWith(vaultBasePath)) {
|
||||||
return absolutePath;
|
return absolutePath;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export class EmbedProcessor {
|
|||||||
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
||||||
|
|
||||||
// Convert absolute path to vault-relative for adapter operations
|
// Convert absolute path to vault-relative for adapter operations
|
||||||
const relativeOutputPath = this.pathUtils.toVaultRelativePath(outputPath);
|
const relativeOutputPath = this.pathUtils.toVaultRelative(outputPath);
|
||||||
|
|
||||||
await this.plugin.app.vault.adapter.writeBinary(relativeOutputPath, arrayBuffer);
|
await this.plugin.app.vault.adapter.writeBinary(relativeOutputPath, arrayBuffer);
|
||||||
|
|
||||||
@@ -561,8 +561,8 @@ export class EmbedProcessor {
|
|||||||
|
|
||||||
// Copy file using vault adapter
|
// Copy file using vault adapter
|
||||||
// Convert absolute paths to vault-relative paths for adapter operations
|
// Convert absolute paths to vault-relative paths for adapter operations
|
||||||
const relativeSourcePath = this.pathUtils.toVaultRelativePath(imagePath);
|
const relativeSourcePath = this.pathUtils.toVaultRelative(imagePath);
|
||||||
const relativeVaultImagePath = this.pathUtils.toVaultRelativePath(vaultImagePath);
|
const relativeVaultImagePath = this.pathUtils.toVaultRelative(vaultImagePath);
|
||||||
|
|
||||||
const sourceBuffer = await this.plugin.app.vault.adapter.readBinary(relativeSourcePath);
|
const sourceBuffer = await this.plugin.app.vault.adapter.readBinary(relativeSourcePath);
|
||||||
await this.plugin.app.vault.adapter.writeBinary(relativeVaultImagePath, sourceBuffer);
|
await this.plugin.app.vault.adapter.writeBinary(relativeVaultImagePath, sourceBuffer);
|
||||||
|
|||||||
@@ -36,15 +36,7 @@ export class TemplateManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read directory contents - convert absolute path to vault-relative
|
// Read directory contents - convert absolute path to vault-relative
|
||||||
const vaultBasePath = this.pathUtils.getVaultPath();
|
const vaultRelativePath = this.pathUtils.toVaultRelative(this.templatesPath);
|
||||||
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 list = await this.plugin.app.vault.adapter.list(vaultRelativePath);
|
const list = await this.plugin.app.vault.adapter.list(vaultRelativePath);
|
||||||
const files = list.files.map(filePath => path.basename(filePath));
|
const files = list.files.map(filePath => path.basename(filePath));
|
||||||
|
|||||||
@@ -58,14 +58,7 @@ export class EmbeddedTemplateManager {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
|
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
|
||||||
const vaultPath = this.pathUtils.getVaultPath();
|
const relativeTemplatesDir = this.pathUtils.toVaultRelative(this.templatesDir);
|
||||||
let relativeTemplatesDir = this.templatesDir;
|
|
||||||
if (this.templatesDir.startsWith(vaultPath)) {
|
|
||||||
relativeTemplatesDir = this.templatesDir.substring(vaultPath.length);
|
|
||||||
if (relativeTemplatesDir.startsWith('/') || relativeTemplatesDir.startsWith('\\')) {
|
|
||||||
relativeTemplatesDir = relativeTemplatesDir.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await this.pathUtils.ensureDir(relativeTemplatesDir);
|
await this.pathUtils.ensureDir(relativeTemplatesDir);
|
||||||
|
|
||||||
// Extract template content
|
// Extract template content
|
||||||
@@ -114,14 +107,7 @@ export class EmbeddedTemplateManager {
|
|||||||
const embeddedNames = getAllTemplateNames();
|
const embeddedNames = getAllTemplateNames();
|
||||||
|
|
||||||
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
|
// Ensure templates directory exists (convert absolute to vault-relative for vault.adapter)
|
||||||
const vaultPath = this.pathUtils.getVaultPath();
|
const relativeTemplatesDir = this.pathUtils.toVaultRelative(this.templatesDir);
|
||||||
let relativeTemplatesDir = this.templatesDir;
|
|
||||||
if (this.templatesDir.startsWith(vaultPath)) {
|
|
||||||
relativeTemplatesDir = this.templatesDir.substring(vaultPath.length);
|
|
||||||
if (relativeTemplatesDir.startsWith('/') || relativeTemplatesDir.startsWith('\\')) {
|
|
||||||
relativeTemplatesDir = relativeTemplatesDir.substring(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await this.pathUtils.ensureDir(relativeTemplatesDir);
|
await this.pathUtils.ensureDir(relativeTemplatesDir);
|
||||||
|
|
||||||
for (const templateName of embeddedNames) {
|
for (const templateName of embeddedNames) {
|
||||||
|
|||||||
Reference in new issue
Block a user