Refactors PDF embed logic for Typst
Introduces a helper function to generate Typst PDF embed syntax, including optional preview images and error handling. This improves code readability and maintainability by centralizing the PDF embed logic.
This commit is contained in:
1 file changed
+43
-25
@@ -685,6 +685,36 @@ ${dependencyResult.allAvailable
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Typst PDF embed syntax with optional preview image
|
||||
* @param relativePdfPath Relative path to PDF from vault base
|
||||
* @param baseName Base name of the PDF file
|
||||
* @param relativeImagePath Optional relative path to preview image
|
||||
* @param errorSuffix Optional error suffix for description
|
||||
* @returns Formatted content for PDF embed
|
||||
*/
|
||||
private generatePdfEmbedContent(
|
||||
relativePdfPath: string,
|
||||
baseName: string,
|
||||
relativeImagePath?: string,
|
||||
errorSuffix?: string
|
||||
): string {
|
||||
const description = `${baseName}${errorSuffix ? ` ${errorSuffix}` : ''}`;
|
||||
const attachmentNote = `*PDF attached: ${description} - check your PDF reader's attachment panel*`;
|
||||
|
||||
const content = [
|
||||
relativeImagePath ? `` : null,
|
||||
relativeImagePath ? '' : null,
|
||||
'```{=typst}',
|
||||
`#pdf.embed("${relativePdfPath}", description: "${description}", mime-type: "application/pdf")`,
|
||||
'```',
|
||||
'',
|
||||
attachmentNote
|
||||
].filter(line => line !== null).join('\n');
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
private async processPdfEmbeds(processedResult: any, vaultBasePath: string, tempDir: string, currentFile?: TFile): Promise<void> {
|
||||
const { PdfToImageConverter } = await import('./src/PdfToImageConverter');
|
||||
const converter = PdfToImageConverter.getInstance(this);
|
||||
@@ -737,17 +767,12 @@ ${dependencyResult.allAvailable
|
||||
// Use relative path to the original PDF from vault base for pdf.embed
|
||||
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
||||
|
||||
// Create combined output with image and Typst pdf.embed
|
||||
// Use raw Typst blocks for the pdf.embed call
|
||||
const combinedOutput = [
|
||||
``,
|
||||
'',
|
||||
'```{=typst}',
|
||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName}", mime-type: "application/pdf")`,
|
||||
'```',
|
||||
'',
|
||||
`*PDF attached: ${pdfEmbed.baseName} - check your PDF reader's attachment panel*`
|
||||
].join('\n');
|
||||
// Create combined output with image and Typst pdf.embed using helper
|
||||
const combinedOutput = this.generatePdfEmbedContent(
|
||||
relativePdfPath,
|
||||
pdfEmbed.baseName,
|
||||
relativeImagePath
|
||||
);
|
||||
|
||||
// Replace the placeholder with the combined output
|
||||
updatedContent = updatedContent.replace(pdfEmbed.marker, combinedOutput);
|
||||
@@ -757,26 +782,19 @@ ${dependencyResult.allAvailable
|
||||
console.warn(`Export: Failed to convert PDF to image: ${result.error}`);
|
||||
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
||||
// Even without preview image, still embed the PDF
|
||||
const fallbackOutput = [
|
||||
'```{=typst}',
|
||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName}", mime-type: "application/pdf")`,
|
||||
'```',
|
||||
'',
|
||||
`*PDF attached: ${pdfEmbed.baseName} - check your PDF reader's attachment panel*`
|
||||
].join('\n');
|
||||
const fallbackOutput = this.generatePdfEmbedContent(relativePdfPath, pdfEmbed.baseName);
|
||||
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
||||
}
|
||||
} catch (error) {
|
||||
ExportErrorHandler.handleProcessingError('PDF embed', pdfEmbed.originalPath, error);
|
||||
// Still try to embed the PDF even if there's a processing error
|
||||
const relativePdfPath = pathModule.relative(vaultBasePath, pdfEmbed.originalPath);
|
||||
const fallbackOutput = [
|
||||
'```{=typst}',
|
||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName} (error occurred)", mime-type: "application/pdf")`,
|
||||
'```',
|
||||
'',
|
||||
`*PDF attached: ${pdfEmbed.baseName} (processing error) - check your PDF reader's attachment panel*`
|
||||
].join('\n');
|
||||
const fallbackOutput = this.generatePdfEmbedContent(
|
||||
relativePdfPath,
|
||||
pdfEmbed.baseName,
|
||||
undefined,
|
||||
'(error occurred)'
|
||||
);
|
||||
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user