refactor: migrate PandocTypstConverter.ts to use PathUtils
- Replace fs.promises operations with vault.adapter methods - Replace path.join with PathUtils.joinPath for temp file paths - Replace fsPromises.writeFile/unlink with vault.adapter.write/remove - Replace fsPromises.rmdir with PathUtils.cleanupDir - Remove unused path and os imports - Use TempDirectoryManager consistently for both fallback cases
This commit is contained in:
1 file changed
+13
-9
@@ -3,12 +3,10 @@
|
|||||||
* Handles the conversion of Markdown to PDF using Pandoc with Typst engine
|
* Handles the conversion of Markdown to PDF using Pandoc with Typst engine
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { promises as fsPromises } from 'fs';
|
|
||||||
import * as path from 'path';
|
|
||||||
import * as os from 'os';
|
|
||||||
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
||||||
import type { obsidianTypstPDFExport } from '../../main';
|
import type { obsidianTypstPDFExport } from '../../main';
|
||||||
import { TempDirectoryManager } from '../core/TempDirectoryManager';
|
import { TempDirectoryManager } from '../core/TempDirectoryManager';
|
||||||
|
import { PathUtils } from '../core/PathUtils';
|
||||||
|
|
||||||
import { PandocCommandBuilder } from './pandoc/PandocCommandBuilder';
|
import { PandocCommandBuilder } from './pandoc/PandocCommandBuilder';
|
||||||
import { PandocExecutor } from './pandoc/PandocExecutor';
|
import { PandocExecutor } from './pandoc/PandocExecutor';
|
||||||
@@ -104,9 +102,10 @@ export class PandocTypstConverter {
|
|||||||
|
|
||||||
// Create temporary markdown file
|
// Create temporary markdown file
|
||||||
await this.ensureTempDirectory();
|
await this.ensureTempDirectory();
|
||||||
const tempInputPath = path.join(this.tempDir!, `temp-${Date.now()}.md`);
|
const pathUtils = new PathUtils(this.plugin.app);
|
||||||
|
const tempInputPath = pathUtils.joinPath(this.tempDir!, `temp-${Date.now()}.md`);
|
||||||
|
|
||||||
await fsPromises.writeFile(tempInputPath, content, 'utf-8');
|
await this.plugin.app.vault.adapter.write(tempInputPath, content);
|
||||||
|
|
||||||
// Merge options
|
// Merge options
|
||||||
this.pandocOptions = { ...this.pandocOptions, ...options };
|
this.pandocOptions = { ...this.pandocOptions, ...options };
|
||||||
@@ -116,7 +115,7 @@ export class PandocTypstConverter {
|
|||||||
|
|
||||||
// Cleanup temp file
|
// Cleanup temp file
|
||||||
try {
|
try {
|
||||||
await fsPromises.unlink(tempInputPath);
|
await this.plugin.app.vault.adapter.remove(tempInputPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Failed to cleanup temp file:', error);
|
console.warn('Failed to cleanup temp file:', error);
|
||||||
}
|
}
|
||||||
@@ -162,7 +161,8 @@ export class PandocTypstConverter {
|
|||||||
|
|
||||||
// Remove temporary directory
|
// Remove temporary directory
|
||||||
if (this.tempDir) {
|
if (this.tempDir) {
|
||||||
await fsPromises.rmdir(this.tempDir, { recursive: true });
|
const pathUtils = new PathUtils(this.plugin.app);
|
||||||
|
await pathUtils.cleanupDir(this.tempDir);
|
||||||
this.tempDir = null;
|
this.tempDir = null;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -181,8 +181,12 @@ export class PandocTypstConverter {
|
|||||||
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir, undefined, this.plugin?.app);
|
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir, undefined, this.plugin?.app);
|
||||||
this.tempDir = await tempManager.ensureTempDir('pandoc');
|
this.tempDir = await tempManager.ensureTempDir('pandoc');
|
||||||
} else {
|
} else {
|
||||||
// Fallback to system temp if plugin folder not available
|
// Fallback to system temp if plugin folder not available - use TempDirectoryManager fallback
|
||||||
this.tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'obsidian-typst-'));
|
const pathUtils = new PathUtils(this.plugin.app);
|
||||||
|
const vaultPath = pathUtils.getVaultPath();
|
||||||
|
const configDir = this.plugin?.app?.vault?.configDir || '.obsidian';
|
||||||
|
const tempManager = new TempDirectoryManager({ vaultPath, configDir, app: this.plugin.app });
|
||||||
|
this.tempDir = await tempManager.ensureTempDir('pandoc');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.tempDir;
|
return this.tempDir;
|
||||||
|
|||||||
Reference in new issue
Block a user