feat(markdown): add email block support for Obsidian to Typst conversion
- Implement email block detection and parsing in MarkdownPreprocessor - Convert Obsidian email blocks to Typst #email-block() function calls - Add Pandoc raw blocks wrapper for proper Typst code execution - Import email-block function in universal wrapper template - Disable smart quotes with --from markdown-smart flag Fixes email blocks appearing as plain text in PDF exports 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
4 files changed
+167
-7
No files matched your search
@@ -258,6 +258,9 @@ args.push(inputPath);
|
|||||||
// Output file
|
// Output file
|
||||||
args.push('-o', outputPath);
|
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)
|
// Set PDF engine to Typst (use configured path if available)
|
||||||
if (this.pandocOptions.typstPath) {
|
if (this.pandocOptions.typstPath) {
|
||||||
args.push(`--pdf-engine=${this.pandocOptions.typstPath}`);
|
args.push(`--pdf-engine=${this.pandocOptions.typstPath}`);
|
||||||
|
|||||||
+161
-4
@@ -74,6 +74,7 @@ export class MarkdownPreprocessor {
|
|||||||
private readonly MULTI_LINE_CALLOUT_PATTERN = /^(>\s*\[!([\w-]+)\]([+-]?)\s*(.*(?:\n(?:>.*|$))*?))/gm;
|
private readonly MULTI_LINE_CALLOUT_PATTERN = /^(>\s*\[!([\w-]+)\]([+-]?)\s*(.*(?:\n(?:>.*|$))*?))/gm;
|
||||||
private readonly TAG_PATTERN = /#(?:[^\s#]+)/g;
|
private readonly TAG_PATTERN = /#(?:[^\s#]+)/g;
|
||||||
private readonly FRONTMATTER_PATTERN = /^---\s*\n([\s\S]*?)\n---\s*\n/;
|
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) {
|
constructor(config: MarkdownPreprocessorConfig) {
|
||||||
this.vaultPath = config.vaultPath;
|
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);
|
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);
|
result.content = this.parseWikilinks(result.content, result);
|
||||||
|
|
||||||
// Step 5: Convert callouts
|
// Step 6: Convert callouts
|
||||||
result.content = this.parseCallouts(result.content, result);
|
result.content = this.parseCallouts(result.content, result);
|
||||||
|
|
||||||
// Step 6: Calculate final metadata
|
// Step 7: Calculate final metadata
|
||||||
result.metadata.wordCount = this.calculateWordCount(result.content);
|
result.metadata.wordCount = this.calculateWordCount(result.content);
|
||||||
|
|
||||||
// Use title from frontmatter if available, otherwise extract from content
|
// Use title from frontmatter if available, otherwise extract from content
|
||||||
@@ -553,6 +557,159 @@ export class MarkdownPreprocessor {
|
|||||||
return processed;
|
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<string, string> {
|
||||||
|
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<string, string> = {};
|
||||||
|
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
|
* Process multi-line callouts with comprehensive support
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
paper: "a4",
|
paper: "a4",
|
||||||
margin: (x: 1.5cm, y: 1.5cm),
|
margin: (x: 1.5cm, y: 1.5cm),
|
||||||
cols: 1,
|
cols: 1,
|
||||||
font: ("Concourse OT", "Helvetica Neue", "Arial", "sans-serif"),
|
font: ("Afraty Stencil", "Concourse OT 3", "Helvetica Neue", "Arial", "sans-serif"),
|
||||||
fontsize: 11pt,
|
fontsize: 11pt,
|
||||||
sectionnumbering: "1.",
|
sectionnumbering: "1.",
|
||||||
pagenumbering: "1",
|
pagenumbering: "1",
|
||||||
@@ -110,7 +110,7 @@
|
|||||||
// Set text properties - Sans-serif for clean look
|
// Set text properties - Sans-serif for clean look
|
||||||
// Use UI-configured font if available, otherwise use template default
|
// Use UI-configured font if available, otherwise use template default
|
||||||
let body-font = if font != none and type(font) == str {
|
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 {
|
} else {
|
||||||
font
|
font
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// Universal Pandoc wrapper for all Typst templates
|
// Universal Pandoc wrapper for all Typst templates
|
||||||
// This wrapper handles Pandoc's variable substitution and passes them to the actual Typst template
|
// 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(
|
#show: doc => conf(
|
||||||
$if(title)$title: [$title$],$else$title: none,$endif$
|
$if(title)$title: [$title$],$else$title: none,$endif$
|
||||||
|
|||||||
Reference in new issue
Block a user