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;
|
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> {
|
private async processPdfEmbeds(processedResult: any, vaultBasePath: string, tempDir: string, currentFile?: TFile): Promise<void> {
|
||||||
const { PdfToImageConverter } = await import('./src/PdfToImageConverter');
|
const { PdfToImageConverter } = await import('./src/PdfToImageConverter');
|
||||||
const converter = PdfToImageConverter.getInstance(this);
|
const converter = PdfToImageConverter.getInstance(this);
|
||||||
@@ -737,17 +767,12 @@ ${dependencyResult.allAvailable
|
|||||||
// Use relative path to the original PDF from vault base for pdf.embed
|
// Use relative path to the original PDF from vault base for pdf.embed
|
||||||
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
||||||
|
|
||||||
// Create combined output with image and Typst pdf.embed
|
// Create combined output with image and Typst pdf.embed using helper
|
||||||
// Use raw Typst blocks for the pdf.embed call
|
const combinedOutput = this.generatePdfEmbedContent(
|
||||||
const combinedOutput = [
|
relativePdfPath,
|
||||||
``,
|
pdfEmbed.baseName,
|
||||||
'',
|
relativeImagePath
|
||||||
'```{=typst}',
|
);
|
||||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName}", mime-type: "application/pdf")`,
|
|
||||||
'```',
|
|
||||||
'',
|
|
||||||
`*PDF attached: ${pdfEmbed.baseName} - check your PDF reader's attachment panel*`
|
|
||||||
].join('\n');
|
|
||||||
|
|
||||||
// Replace the placeholder with the combined output
|
// Replace the placeholder with the combined output
|
||||||
updatedContent = updatedContent.replace(pdfEmbed.marker, combinedOutput);
|
updatedContent = updatedContent.replace(pdfEmbed.marker, combinedOutput);
|
||||||
@@ -757,26 +782,19 @@ ${dependencyResult.allAvailable
|
|||||||
console.warn(`Export: Failed to convert PDF to image: ${result.error}`);
|
console.warn(`Export: Failed to convert PDF to image: ${result.error}`);
|
||||||
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
const relativePdfPath = pathModule.relative(vaultBasePath, fullPdfPath);
|
||||||
// Even without preview image, still embed the PDF
|
// Even without preview image, still embed the PDF
|
||||||
const fallbackOutput = [
|
const fallbackOutput = this.generatePdfEmbedContent(relativePdfPath, pdfEmbed.baseName);
|
||||||
'```{=typst}',
|
|
||||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName}", mime-type: "application/pdf")`,
|
|
||||||
'```',
|
|
||||||
'',
|
|
||||||
`*PDF attached: ${pdfEmbed.baseName} - check your PDF reader's attachment panel*`
|
|
||||||
].join('\n');
|
|
||||||
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ExportErrorHandler.handleProcessingError('PDF embed', pdfEmbed.originalPath, error);
|
ExportErrorHandler.handleProcessingError('PDF embed', pdfEmbed.originalPath, error);
|
||||||
// Still try to embed the PDF even if there's a processing error
|
// Still try to embed the PDF even if there's a processing error
|
||||||
const relativePdfPath = pathModule.relative(vaultBasePath, pdfEmbed.originalPath);
|
const relativePdfPath = pathModule.relative(vaultBasePath, pdfEmbed.originalPath);
|
||||||
const fallbackOutput = [
|
const fallbackOutput = this.generatePdfEmbedContent(
|
||||||
'```{=typst}',
|
relativePdfPath,
|
||||||
`#pdf.embed("${relativePdfPath}", description: "${pdfEmbed.baseName} (error occurred)", mime-type: "application/pdf")`,
|
pdfEmbed.baseName,
|
||||||
'```',
|
undefined,
|
||||||
'',
|
'(error occurred)'
|
||||||
`*PDF attached: ${pdfEmbed.baseName} (processing error) - check your PDF reader's attachment panel*`
|
);
|
||||||
].join('\n');
|
|
||||||
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
updatedContent = updatedContent.replace(pdfEmbed.marker, fallbackOutput);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user