Add Docstrings
This commit is contained in:
17 files changed
+5380
-177
No files matched your search
@@ -1,6 +1,17 @@
|
||||
/**
|
||||
* Pandoc to Typst PDF Converter
|
||||
* Handles the conversion of Markdown to PDF using Pandoc with Typst engine
|
||||
* Pandoc to Typst PDF Converter.
|
||||
*
|
||||
* This module provides the main conversion engine for transforming Markdown files
|
||||
* to PDF documents using the Pandoc → Typst → PDF pipeline. It coordinates all
|
||||
* aspects of the conversion process including:
|
||||
* - Temporary file and directory management
|
||||
* - Command building and execution
|
||||
* - Progress reporting
|
||||
* - Resource cleanup
|
||||
* - Error handling
|
||||
*
|
||||
* The converter supports both file-based and content-based conversions, automatically
|
||||
* managing temporary directories and cleanup on process termination.
|
||||
*/
|
||||
|
||||
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
||||
@@ -11,6 +22,52 @@ import { PathUtils } from '../core/PathUtils';
|
||||
import { PandocCommandBuilder } from './pandoc/PandocCommandBuilder';
|
||||
import { PandocExecutor } from './pandoc/PandocExecutor';
|
||||
|
||||
/**
|
||||
* Main converter class for Markdown → Typst → PDF conversion.
|
||||
*
|
||||
* This class orchestrates the PDF export process by delegating to specialized
|
||||
* components (PandocCommandBuilder, PandocExecutor) while managing temporary
|
||||
* resources and providing progress feedback.
|
||||
*
|
||||
* Key features:
|
||||
* - File and content-based conversion support
|
||||
* - Automatic temporary directory management
|
||||
* - Process cleanup on termination (exit, SIGINT, SIGTERM, exceptions)
|
||||
* - Progress callback support for UI updates
|
||||
* - Configurable Pandoc and Typst settings
|
||||
*
|
||||
* Lifecycle:
|
||||
* 1. Constructor initializes command builder and executor
|
||||
* 2. Conversion methods create temp directories and build commands
|
||||
* 3. Executor runs Pandoc with Typst engine
|
||||
* 4. Cleanup automatically removes temporary files
|
||||
* 5. dispose() for manual cleanup when done
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const converter = new PandocTypstConverter(
|
||||
* plugin,
|
||||
* { template: 'modern', variables: { ... } },
|
||||
* { ppi: 144 }
|
||||
* );
|
||||
*
|
||||
* try {
|
||||
* const result = await converter.convertToPDF(
|
||||
* 'input.md',
|
||||
* 'output.pdf',
|
||||
* (message, progress) => console.log(`${progress}%: ${message}`)
|
||||
* );
|
||||
*
|
||||
* if (result.success) {
|
||||
* console.log('PDF created:', result.outputPath);
|
||||
* } else {
|
||||
* console.error('Conversion failed:', result.error);
|
||||
* }
|
||||
* } finally {
|
||||
* await converter.dispose();
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class PandocTypstConverter {
|
||||
private tempDir: string | null = null;
|
||||
private cleanupHandlers: (() => void)[] = [];
|
||||
@@ -19,10 +76,34 @@ export class PandocTypstConverter {
|
||||
private pandocExecutor: PandocExecutor;
|
||||
|
||||
/**
|
||||
* Create a new PandocTypstConverter instance
|
||||
* @param plugin The main plugin instance for accessing settings
|
||||
* @param pandocOptions Configuration options for Pandoc
|
||||
* @param typstSettings Settings specific to Typst engine
|
||||
* Creates a new PandocTypstConverter instance with configuration.
|
||||
*
|
||||
* Initializes the converter with the plugin instance and configuration options
|
||||
* for both Pandoc and Typst. Sets up cleanup handlers to ensure temporary
|
||||
* files are removed on process termination.
|
||||
*
|
||||
* @param plugin - The main plugin instance for accessing app and settings
|
||||
* @param pandocOptions - Configuration options for Pandoc conversion (template, variables, etc.)
|
||||
* @param typstSettings - Settings specific to Typst rendering engine (ppi, font-paths, etc.)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const converter = new PandocTypstConverter(
|
||||
* this.plugin,
|
||||
* {
|
||||
* template: 'modern',
|
||||
* variables: {
|
||||
* pageSize: 'a4',
|
||||
* bodyFont: 'Georgia'
|
||||
* },
|
||||
* metadata: { title: 'My Document' }
|
||||
* },
|
||||
* {
|
||||
* ppi: 144,
|
||||
* fontPaths: ['/usr/share/fonts']
|
||||
* }
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
constructor(
|
||||
plugin: obsidianTypstPDFExport,
|
||||
@@ -37,15 +118,46 @@ export class PandocTypstConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a markdown file to PDF using Pandoc with Typst engine
|
||||
* @param inputPath Path to the input markdown file
|
||||
* @param outputPath Path where the PDF should be saved
|
||||
* @param progressCallback Optional callback for progress updates
|
||||
* @returns Promise resolving to conversion result
|
||||
* Converts a Markdown file to PDF using the Pandoc → Typst → PDF pipeline.
|
||||
*
|
||||
* This is the primary conversion method that handles the complete export process:
|
||||
* 1. Creates temporary directory for intermediate files
|
||||
* 2. Builds Pandoc command with configured options
|
||||
* 3. Executes Pandoc with Typst engine
|
||||
* 4. Reports progress via optional callback
|
||||
* 5. Returns result with success status and output path
|
||||
*
|
||||
* The method automatically manages temporary files and provides progress updates
|
||||
* at key stages. If conversion fails, returns error information in the result
|
||||
* object rather than throwing.
|
||||
*
|
||||
* @param inputPath - Absolute path to the input Markdown file
|
||||
* @param outputPath - Absolute path where the PDF should be saved
|
||||
* @param progressCallback - Optional callback for progress updates (message, percentage)
|
||||
* @returns Promise resolving to conversion result with success flag and optional error
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const result = await converter.convertToPDF(
|
||||
* '/path/to/note.md',
|
||||
* '/path/to/output.pdf',
|
||||
* (message, progress) => {
|
||||
* console.log(`${progress}%: ${message}`);
|
||||
* // Update UI progress bar
|
||||
* }
|
||||
* );
|
||||
*
|
||||
* if (result.success) {
|
||||
* console.log('PDF created at:', result.outputPath);
|
||||
* } else {
|
||||
* console.error('Conversion failed:', result.error);
|
||||
* console.log('Pandoc output:', result.stdout);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async convertToPDF(
|
||||
inputPath: string,
|
||||
outputPath: string,
|
||||
inputPath: string,
|
||||
outputPath: string,
|
||||
progressCallback?: ProgressCallback
|
||||
): Promise<ConversionResult> {
|
||||
try {
|
||||
@@ -89,7 +201,43 @@ export class PandocTypstConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert markdown content to PDF
|
||||
* Converts Markdown content (string) to PDF without requiring a file.
|
||||
*
|
||||
* This is a convenience method for converting Markdown content directly from
|
||||
* memory without needing an existing file. It creates a temporary file,
|
||||
* delegates to convertToPDF, then cleans up the temporary file.
|
||||
*
|
||||
* Useful for:
|
||||
* - Converting dynamically generated content
|
||||
* - Processing in-memory Markdown transformations
|
||||
* - Testing conversion without file system operations
|
||||
*
|
||||
* Note: This method merges the provided options with existing pandocOptions.
|
||||
*
|
||||
* @param content - Markdown content to convert
|
||||
* @param outputPath - Absolute path where the PDF should be saved
|
||||
* @param options - Additional Pandoc options to merge with existing settings
|
||||
* @param progressCallback - Optional callback for progress updates (message, percentage)
|
||||
* @returns Promise resolving to conversion result with success flag and optional error
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const markdown = `
|
||||
* # My Document
|
||||
* This is **dynamically generated** content.
|
||||
* `;
|
||||
*
|
||||
* const result = await converter.convertMarkdownToPDF(
|
||||
* markdown,
|
||||
* '/path/to/output.pdf',
|
||||
* { variables: { title: 'Generated Doc' } },
|
||||
* (msg, pct) => updateProgress(pct)
|
||||
* );
|
||||
*
|
||||
* if (result.success) {
|
||||
* console.log('Created PDF from content');
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async convertMarkdownToPDF(
|
||||
content: string,
|
||||
@@ -131,7 +279,17 @@ export class PandocTypstConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up cleanup handlers for temporary files
|
||||
* Sets up automatic cleanup handlers for process termination events.
|
||||
*
|
||||
* Registers the cleanup method to run on various process termination events
|
||||
* to ensure temporary files are removed even if the process exits unexpectedly.
|
||||
* Handlers are registered for:
|
||||
* - Normal exit
|
||||
* - SIGINT (Ctrl+C)
|
||||
* - SIGTERM (kill command)
|
||||
* - Uncaught exceptions
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private setupCleanup(): void {
|
||||
const cleanup = () => {
|
||||
@@ -146,7 +304,16 @@ export class PandocTypstConverter {
|
||||
|
||||
|
||||
/**
|
||||
* Clean up temporary files and directories
|
||||
* Cleans up temporary files and directories.
|
||||
*
|
||||
* This method executes all registered cleanup handlers and removes the temporary
|
||||
* directory created by the converter. Cleanup failures are logged but don't throw
|
||||
* to avoid interfering with normal operation or other cleanup operations.
|
||||
*
|
||||
* Called automatically on process termination or manually via dispose().
|
||||
*
|
||||
* @private
|
||||
* @returns Promise that resolves when cleanup is complete
|
||||
*/
|
||||
private async cleanup(): Promise<void> {
|
||||
try {
|
||||
@@ -171,7 +338,19 @@ export class PandocTypstConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure temporary directory exists
|
||||
* Ensures a temporary directory exists for intermediate conversion files.
|
||||
*
|
||||
* Creates a temporary directory on first call and reuses it for subsequent calls.
|
||||
* The directory is created inside the plugin folder if vaultBasePath is available,
|
||||
* otherwise falls back to vault-level temp directories.
|
||||
*
|
||||
* The temporary directory is used for:
|
||||
* - Temporary Markdown files
|
||||
* - Intermediate Typst files (if debug mode enabled)
|
||||
* - Processed resource files
|
||||
*
|
||||
* @private
|
||||
* @returns Promise resolving to the temporary directory path
|
||||
*/
|
||||
private async ensureTempDirectory(): Promise<string> {
|
||||
if (!this.tempDir) {
|
||||
@@ -194,7 +373,24 @@ export class PandocTypstConverter {
|
||||
|
||||
|
||||
/**
|
||||
* Dispose of the converter and clean up resources
|
||||
* Disposes of the converter and cleans up all resources.
|
||||
*
|
||||
* This method should be called when done with the converter to ensure temporary
|
||||
* files are removed. While cleanup happens automatically on process termination,
|
||||
* calling dispose explicitly allows for immediate cleanup and is recommended
|
||||
* when the converter is no longer needed.
|
||||
*
|
||||
* @returns Promise that resolves when cleanup is complete
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const converter = new PandocTypstConverter(plugin, options);
|
||||
* try {
|
||||
* await converter.convertToPDF(input, output);
|
||||
* } finally {
|
||||
* await converter.dispose(); // Cleanup even if conversion fails
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
async dispose(): Promise<void> {
|
||||
await this.cleanup();
|
||||
|
||||
Reference in new issue
Block a user