Add Generic File embedding

This commit is contained in:
AlexKucera committed 2025-09-05 17:41:18 +02:00
1 parent 2181b0cea3
commit 05f28f84f3
6 files changed
+308 -2

No files matched your search

+67 -2
View File
@@ -46,6 +46,15 @@ export interface PreprocessingResult {
sizeOrAlt?: string;
marker: string;
}>;
fileEmbeds?: Array<{
originalPath: string;
sanitizedPath: string;
fileName: string;
baseName: string;
fileType: string;
options?: string;
marker: string;
}>;
};
/** Processing errors and warnings */
errors: string[];
@@ -594,6 +603,9 @@ export class MarkdownPreprocessor {
case 'pdf':
return this.processPdfEmbed(cleanPath, sizeOrAlt, result);
case 'file':
return this.processFileEmbed(cleanPath, sizeOrAlt, result);
default:
// Fallback to link reference for unknown types
result.warnings.push(`Unknown file type for embed: ${cleanPath}`);
@@ -618,20 +630,39 @@ export class MarkdownPreprocessor {
/**
* Determine file type based on extension
*/
private determineFileType(extension: string): 'image' | 'video' | 'audio' | 'document' | 'pdf' | 'unknown' {
private determineFileType(extension: string): 'image' | 'video' | 'audio' | 'document' | 'pdf' | 'file' | 'unknown' {
const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.bmp', '.ico', '.tiff'];
const videoExtensions = ['.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.mkv', '.m4v'];
const audioExtensions = ['.mp3', '.wav', '.ogg', '.m4a', '.flac', '.aac', '.wma'];
const documentExtensions = ['.md', '.txt', '.doc', '.docx', '.rtf'];
const pdfExtensions = ['.pdf'];
// Additional file types that should be embedded
const fileExtensions = [
// Office documents
'.xlsx', '.xls', '.pptx', '.ppt', '.odt', '.ods', '.odp',
// Archives and compressed files
'.zip', '.rar', '.7z', '.tar', '.gz', '.bz2', '.xz',
// Data and configuration files
'.json', '.xml', '.csv', '.yaml', '.yml', '.toml', '.ini', '.cfg',
// Code files
'.js', '.ts', '.py', '.java', '.cpp', '.c', '.h', '.css', '.html', '.php', '.rb', '.go', '.rs', '.swift',
// Database files
'.db', '.sqlite', '.sql',
// Other common files
'.log', '.epub', '.mobi', '.ics', '.vcf'
];
if (imageExtensions.includes(extension)) return 'image';
if (videoExtensions.includes(extension)) return 'video';
if (audioExtensions.includes(extension)) return 'audio';
if (documentExtensions.includes(extension)) return 'document';
if (pdfExtensions.includes(extension)) return 'pdf';
if (fileExtensions.includes(extension)) return 'file';
return 'unknown';
// Default to 'file' for unknown extensions instead of 'unknown'
// This ensures all file types get embedded unless they're explicitly excluded
return extension ? 'file' : 'unknown';
}
/**
@@ -735,6 +766,40 @@ export class MarkdownPreprocessor {
return marker;
}
/**
* Process generic file embeds - Convert to attachment using Typst's pdf.embed
*/
private processFileEmbed(filePath: string, options: string | undefined, result: PreprocessingResult): string {
const sanitizedPath = this.sanitizeFilePath(filePath);
// For now, we'll mark this for async processing and return a placeholder
// The actual embedding will be handled in the main export process
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
const baseName = fileName.replace(/\.[^/.]+$/, ""); // Remove extension
const fileExtension = this.getFileExtension(fileName);
// Create a marker that the export process can detect and replace with Typst code
const marker = `FILE_EMBED_MARKER:${filePath}:${baseName}:${options || ''}`;
// Add to processing queue for later conversion
if (!result.metadata.fileEmbeds) {
result.metadata.fileEmbeds = [];
}
result.metadata.fileEmbeds.push({
originalPath: filePath,
sanitizedPath: sanitizedPath,
fileName: fileName,
baseName: baseName,
fileType: fileExtension,
options: options,
marker: marker
});
result.warnings.push(`File embed queued for processing with Typst pdf.embed: ${filePath}`);
return marker;
}
/**
* Convert Obsidian callouts to standard blockquotes with styling markers
*/
+3
View File
@@ -91,6 +91,8 @@ export interface obsidianTypstPDFExportSettings {
debugMode: boolean;
/** Whether to embed PDF files as attachments in the output PDF */
embedPdfFiles: boolean;
/** Whether to embed all file types as attachments in the output PDF */
embedAllFiles: boolean;
/** Display frontmatter as formatted text at the beginning of the document */
printFrontmatter: boolean;
};
@@ -151,6 +153,7 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = {
exportConcurrency: 3,
debugMode: false,
embedPdfFiles: true,
embedAllFiles: true,
printFrontmatter: false
}
};;;;
+2
View File
@@ -20,6 +20,8 @@ export interface ExportConfig {
preserveFolderStructure?: boolean;
/** Whether to embed PDF files as attachments in the output PDF */
embedPdfFiles?: boolean;
/** Whether to embed all file types as attachments in the output PDF */
embedAllFiles?: boolean;
/** Display frontmatter as formatted text at the beginning of the document */
printFrontmatter?: boolean;
}
+12
View File
@@ -55,6 +55,18 @@ export class BehaviorSection implements ModalSection {
});
});
// Embed all files toggle
new Setting(this.container)
.setName('Embed all file types as attachments')
.setDesc('Embed all referenced file types (office documents, archives, etc.) as PDF attachments')
.addToggle(toggle => {
toggle
.setValue(state.settings.embedAllFiles !== false)
.onChange(value => {
state.updateSettings({ embedAllFiles: value });
});
});
// Print frontmatter toggle
new Setting(this.container)
.setName('Print frontmatter')
+1
View File
@@ -86,6 +86,7 @@ export class ModalState implements IModalState {
openAfterExport: this.settings.openAfterExport,
preserveFolderStructure: this.settings.preserveFolderStructure,
embedPdfFiles: this.settings.embedPdfFiles,
embedAllFiles: this.settings.embedAllFiles,
printFrontmatter: this.settings.printFrontmatter
};