diff --git a/main.ts b/main.ts index 79a884d..9ab7e56 100644 --- a/main.ts +++ b/main.ts @@ -258,6 +258,9 @@ args.push(inputPath); // Output file args.push('-o', outputPath); +// Specify input format as markdown with smart extension disabled +args.push('--from', 'markdown-smart'); + // Set PDF engine to Typst (use configured path if available) if (this.pandocOptions.typstPath) { args.push(`--pdf-engine=${this.pandocOptions.typstPath}`); diff --git a/src/MarkdownPreprocessor.ts b/src/MarkdownPreprocessor.ts index 0a49688..f9f3c6c 100644 --- a/src/MarkdownPreprocessor.ts +++ b/src/MarkdownPreprocessor.ts @@ -74,6 +74,7 @@ export class MarkdownPreprocessor { private readonly MULTI_LINE_CALLOUT_PATTERN = /^(>\s*\[!([\w-]+)\]([+-]?)\s*(.*(?:\n(?:>.*|$))*?))/gm; private readonly TAG_PATTERN = /#(?:[^\s#]+)/g; private readonly FRONTMATTER_PATTERN = /^---\s*\n([\s\S]*?)\n---\s*\n/; + private readonly EMAIL_BLOCK_PATTERN = /^```email\s*\n([\s\S]*?)^```\s*$/gm; constructor(config: MarkdownPreprocessorConfig) { this.vaultPath = config.vaultPath; @@ -113,16 +114,19 @@ export class MarkdownPreprocessor { } } - // Step 3: Convert embeds FIRST (before wikilinks to avoid .md extension being added) + // Step 3: Convert email blocks to Typst format + result.content = this.parseEmailBlocks(result.content, result); + + // Step 4: Convert embeds FIRST (before wikilinks to avoid .md extension being added) result.content = this.parseEmbeds(result.content, result); - // Step 4: Convert wikilinks (after embeds are processed) + // Step 5: Convert wikilinks (after embeds are processed) result.content = this.parseWikilinks(result.content, result); - // Step 5: Convert callouts + // Step 6: Convert callouts result.content = this.parseCallouts(result.content, result); - // Step 6: Calculate final metadata + // Step 7: Calculate final metadata result.metadata.wordCount = this.calculateWordCount(result.content); // Use title from frontmatter if available, otherwise extract from content @@ -552,6 +556,159 @@ export class MarkdownPreprocessor { const processed = this.processMultiLineCallouts(content, result); return processed; } + + /** + * Convert Obsidian email blocks to Typst email-block format + */ + private parseEmailBlocks(content: string, result: PreprocessingResult): string { + return content.replace(this.EMAIL_BLOCK_PATTERN, (match, blockContent) => { + try { + return this.processEmailBlock(blockContent, result); + } catch (error) { + result.warnings.push(`Failed to process email block: ${error.message}`); + return match; // Return original on error + } + }); + } + + /** + * Process individual email block content and convert to Typst format + */ + private processEmailBlock(blockContent: string, result: PreprocessingResult): string { + try { + // Split at --- to separate header from body + const parts = blockContent.split('---'); + const yamlHeader = parts[0].trim(); + const body = parts.length > 1 ? parts.slice(1).join('---').trim() : ''; + + // Parse YAML header + const params = this.parseEmailYaml(yamlHeader); + + // Build Typst function call arguments + const args: string[] = []; + + // Add parameters in the order expected by the Typst template + if (params.from) { + args.push(`from: "${this.escapeQuotes(params.from)}"`); + } + if (params.to) { + args.push(`to: "${this.escapeQuotes(params.to)}"`); + } + if (params.subject) { + args.push(`subject: "${this.escapeQuotes(params.subject)}"`); + } + if (params.date) { + args.push(`date: "${this.escapeQuotes(params.date)}"`); + } + + // Add body as string parameter (always present, even if empty) + // Use the formatBodyForTypst method to preserve paragraphs but keep as string + const bodyParam = `"${this.escapeQuotes(body)}"`; + + // Construct the Typst email-block function call with proper line breaks + // Wrap in Pandoc raw blocks so it's treated as Typst code, not markdown text + const argsString = args.length > 0 ? args.join(', ') + ', ' : ''; + return `\n\n\`\`\`{=typst}\n#email-block(${argsString}${bodyParam})\n\`\`\`\n\n`; + + } catch (error) { + result.warnings.push(`Error processing email block content: ${error.message}`); + // Return as code block to preserve original content + return `\`\`\`\n${blockContent}\n\`\`\``; + } + } + + /** + * Format email body content for Typst content blocks (not string literals) + */ + private formatBodyForTypst(body: string): string { + if (!body) return ''; + + // Split into paragraphs and format each one + const paragraphs = body.split(/\n\s*\n/); // Split on blank lines + const formattedParagraphs = paragraphs + .map(para => para.trim()) + .filter(para => para.length > 0) + .map(para => { + // Escape special Typst characters but preserve the content structure + const escaped = para + .replace(/\\/g, '\\\\') // Escape backslashes + .replace(/#/g, '\\#') // Escape hash symbols + .replace(/\[/g, '\\[') // Escape square brackets + .replace(/\]/g, '\\]') // Escape square brackets + .replace(/\*/g, '\\*') // Escape asterisks + .replace(/_/g, '\\_'); // Escape underscores + + return escaped; + }); + + // Join paragraphs with proper Typst paragraph breaks + return formattedParagraphs.join('\n\n'); + } + + /** + * Parse email YAML header, handling edge cases from the email block plugin + */ + private parseEmailYaml(yamlString: string): Record { + try { + let processedYaml = yamlString; + + // Handle wikilinks - quote them if not already quoted (from email block plugin logic) + if (processedYaml.includes('[[') && !processedYaml.includes('"[[')) { + processedYaml = processedYaml.replace(/\[\[/g, '"[['); + processedYaml = processedYaml.replace(/\]\]/g, ']]"'); + } + + // Handle template variables - quote them if not already quoted + if (processedYaml.includes('{{') && !processedYaml.includes('"{{')) { + processedYaml = processedYaml.replace(/\{\{/g, '"{{'); + processedYaml = processedYaml.replace(/\}\}/g, '}}"'); + } + + // Use Obsidian's YAML parser (from email block plugin approach) + // For now, we'll do simple parsing since we don't have access to Obsidian's parseYaml + const params: Record = {}; + const lines = processedYaml.split('\n'); + + for (const line of lines) { + const colonIndex = line.indexOf(':'); + if (colonIndex > 0) { + const key = line.substring(0, colonIndex).trim(); + let value = line.substring(colonIndex + 1).trim(); + + // Remove quotes if present + if ((value.startsWith('"') && value.endsWith('"')) || + (value.startsWith("'") && value.endsWith("'"))) { + value = value.slice(1, -1); + } + + // Handle array values (convert to comma-separated string) + if (value.startsWith('[') && value.endsWith(']')) { + value = value.slice(1, -1).replace(/"/g, '').replace(/'/g, ''); + } + + params[key] = value; + } + } + + return params; + + } catch (error) { + throw new Error(`YAML parsing failed: ${error.message}`); + } + } + + /** + * Escape quotes in string content for Typst + */ + private escapeQuotes(text: string): string { + if (!text) return ''; + return text + .replace(/\\/g, '\\\\') // Escape backslashes first + .replace(/"/g, '\\"') // Escape double quotes + .replace(/'/g, "\\'") // Escape single quotes to prevent smart quote conversion + .replace(/\n/g, '\\n') // Convert newlines to literal \n for string parameters + .replace(/\r/g, '\\r'); // Convert carriage returns + } /** * Process multi-line callouts with comprehensive support diff --git a/templates/default.typ b/templates/default.typ index ac49e95..bc52999 100644 --- a/templates/default.typ +++ b/templates/default.typ @@ -57,7 +57,7 @@ paper: "a4", margin: (x: 1.5cm, y: 1.5cm), cols: 1, - font: ("Concourse OT", "Helvetica Neue", "Arial", "sans-serif"), + font: ("Afraty Stencil", "Concourse OT 3", "Helvetica Neue", "Arial", "sans-serif"), fontsize: 11pt, sectionnumbering: "1.", pagenumbering: "1", @@ -110,7 +110,7 @@ // Set text properties - Sans-serif for clean look // Use UI-configured font if available, otherwise use template default let body-font = if font != none and type(font) == str { - (font, "Concourse OT", "Helvetica Neue", "Arial", "sans-serif") + (font, "Afraty Stencil", "Concourse OT 3", "Helvetica Neue", "Arial", "sans-serif") } else { font } diff --git a/templates/universal-wrapper.pandoc.typ b/templates/universal-wrapper.pandoc.typ index e19fb4f..3f90b16 100644 --- a/templates/universal-wrapper.pandoc.typ +++ b/templates/universal-wrapper.pandoc.typ @@ -1,7 +1,7 @@ // Universal Pandoc wrapper for all Typst templates // This wrapper handles Pandoc's variable substitution and passes them to the actual Typst template -#import "$template_path$": conf +#import "$template_path$": conf, email-block #show: doc => conf( $if(title)$title: [$title$],$else$title: none,$endif$