diff --git a/plan.md b/plan.md index 10c388e..34632d0 100644 --- a/plan.md +++ b/plan.md @@ -101,7 +101,7 @@ The main.ts file is the plugin entry point and most critical to get right. **CHECKPOINT: Complete test of all Phase 1 functionality** - DONE -## PHASE 2: Refactor MarkdownPreprocessor.ts - IN PROGRESS +## PHASE 2: Refactor MarkdownPreprocessor.ts - DONE ### Step 2.1: Create FrontmatterProcessor.ts - DONE **Location:** `src/converters/preprocessors/FrontmatterProcessor.ts` (~200 lines) @@ -165,9 +165,9 @@ The main.ts file is the plugin entry point and most critical to get right. **CHECKPOINT: Test various note types with different content** -## PHASE 3: Refactor PandocTypstConverter.ts +## PHASE 3: Refactor PandocTypstConverter.ts - IN PROGRESS -### Step 3.1: Create PandocCommandBuilder.ts +### Step 3.1: Create PandocCommandBuilder.ts - DONE **Location:** `src/converters/pandoc/PandocCommandBuilder.ts` (~200 lines) **Extract:** - Command building logic @@ -176,7 +176,7 @@ The main.ts file is the plugin entry point and most critical to get right. **STOP** → Verify build → Test Pandoc command generation -### Step 3.2: Create TypstVariableMapper.ts +### Step 3.2: Create TypstVariableMapper.ts - DONE **Location:** `src/converters/pandoc/TypstVariableMapper.ts` (~200 lines) **Extract:** - Variable mapping logic @@ -185,7 +185,7 @@ The main.ts file is the plugin entry point and most critical to get right. **STOP** → Verify build → Test variable application in templates -### Step 3.3: Create ResourcePathResolver.ts +### Step 3.3: Create ResourcePathResolver.ts - IN PROGRESS **Location:** `src/converters/pandoc/ResourcePathResolver.ts` (~150 lines) **Extract:** - `resolveResourcePaths()` method diff --git a/src/converters/pandoc/PandocCommandBuilder.ts b/src/converters/pandoc/PandocCommandBuilder.ts index fd472d7..425136c 100644 --- a/src/converters/pandoc/PandocCommandBuilder.ts +++ b/src/converters/pandoc/PandocCommandBuilder.ts @@ -6,12 +6,12 @@ import * as path from 'path'; import { promises as fsPromises } from 'fs'; import { spawnSync } from 'child_process'; -import { mapToTypstPaperSize } from '../../utils/paperSizeMapper'; import { PandocOptions } from '../converterTypes'; -import type { obsidianTypstPDFExportSettings } from '../../core/settings'; +import { TypstVariableMapper } from './TypstVariableMapper'; export class PandocCommandBuilder { private plugin: any; + private variableMapper: TypstVariableMapper; // Cache for directory scanning optimization private resourcePathCache: string[] = []; @@ -21,6 +21,7 @@ export class PandocCommandBuilder { constructor(plugin: any) { this.plugin = plugin; + this.variableMapper = new TypstVariableMapper(plugin); } /** @@ -54,11 +55,8 @@ export class PandocCommandBuilder { // Handle template configuration await this.addTemplateConfiguration(args, pandocOptions); - // Add variables from the ExportConfig (these take priority) - this.addExportConfigVariables(args, pandocOptions); - - // Add fallback variables from plugin settings - this.addPluginSettingsVariables(args, pandocOptions); + // Add all variables using the variable mapper + this.addTypstVariables(args, pandocOptions); // Add Typst engine options this.addTypstEngineOptions(args, pandocOptions); @@ -202,127 +200,15 @@ export class PandocCommandBuilder { } /** - * Add variables from ExportConfig (these take priority) + * Add all Typst variables using the variable mapper */ - private addExportConfigVariables(args: string[], pandocOptions: PandocOptions): void { - if (!pandocOptions.variables) { - return; - } - - for (const [key, value] of Object.entries(pandocOptions.variables)) { - if (value !== null && value !== undefined && value.toString().trim() !== '') { - // Handle special variable name mappings for Typst compatibility - let variableName = key; - let processedValue = value; - - switch (key) { - case 'bodyFont': - variableName = 'font'; - break; - case 'headingFont': - variableName = 'heading_font'; - break; - case 'monospaceFont': - variableName = 'monospace_font'; - break; - case 'bodyFontSize': - variableName = 'fontsize'; - processedValue = value + 'pt'; - break; - case 'pageSize': - variableName = 'paper'; - // Convert to Typst-compatible paper size - processedValue = mapToTypstPaperSize(value.toString()); - break; - case 'marginTop': - variableName = 'margin_top'; - processedValue = value + 'cm'; - break; - case 'marginBottom': - variableName = 'margin_bottom'; - processedValue = value + 'cm'; - break; - case 'marginLeft': - variableName = 'margin_left'; - processedValue = value + 'cm'; - break; - case 'marginRight': - variableName = 'margin_right'; - processedValue = value + 'cm'; - break; - // Keep other variables as-is (orientation, flipped, width, etc.) - } - - args.push('-V', `${variableName}=${processedValue}`); - } - } - } - - /** - * Add fallback typography and page setup from plugin settings - */ - private addPluginSettingsVariables(args: string[], pandocOptions: PandocOptions): void { - if (!this.plugin || !this.plugin.settings) { - return; - } - - const settings: obsidianTypstPDFExportSettings = this.plugin.settings; - const existingVars = pandocOptions.variables || {}; + private addTypstVariables(args: string[], pandocOptions: PandocOptions): void { + // Map all variables (ExportConfig + plugin settings fallbacks) using the variable mapper + const typstVariables = this.variableMapper.mapAllVariablesToTypst(pandocOptions); - // Add typography variables only if not already present in variables - if (settings.typography) { - if (settings.typography.fonts) { - // Only add if not already in variables from ExportConfig - if (!existingVars.bodyFont && !existingVars.font && settings.typography.fonts.body) { - args.push('-V', `font=${settings.typography.fonts.body}`); - } - if (!existingVars.headingFont && !existingVars.heading_font && settings.typography.fonts.heading) { - args.push('-V', `heading_font=${settings.typography.fonts.heading}`); - } - if (!existingVars.monospaceFont && !existingVars.monospace_font && settings.typography.fonts.monospace) { - args.push('-V', `monospace_font=${settings.typography.fonts.monospace}`); - } - } - - if (settings.typography.fontSizes) { - if (!existingVars.bodyFontSize && !existingVars.fontsize && settings.typography.fontSizes.body) { - args.push('-V', `fontsize=${settings.typography.fontSizes.body}pt`); - } - } - } - - // Add page setup variables only if not already present - if (settings.pageSetup) { - if (!existingVars.pageSize && !existingVars.paper && settings.pageSetup.size) { - const typstPaperSize = mapToTypstPaperSize(settings.pageSetup.size); - args.push('-V', `paper=${typstPaperSize}`); - } - if (!existingVars.orientation && settings.pageSetup.orientation) { - args.push('-V', `orientation=${settings.pageSetup.orientation}`); - } - - // Add margin fallbacks only if not already specified - if (settings.pageSetup.margins) { - const margins = settings.pageSetup.margins; - if (!existingVars.marginTop && !existingVars.margin_top && margins.top !== undefined) { - args.push('-V', `margin_top=${margins.top}cm`); - } - if (!existingVars.marginRight && !existingVars.margin_right && margins.right !== undefined) { - args.push('-V', `margin_right=${margins.right}cm`); - } - if (!existingVars.marginBottom && !existingVars.margin_bottom && margins.bottom !== undefined) { - args.push('-V', `margin_bottom=${margins.bottom}cm`); - } - if (!existingVars.marginLeft && !existingVars.margin_left && margins.left !== undefined) { - args.push('-V', `margin_left=${margins.left}cm`); - } - } - } - - // Add export format variable - only use settings default if not already provided - if (!existingVars.export_format && settings.exportDefaults && settings.exportDefaults.format) { - args.push('-V', `export_format=${settings.exportDefaults.format}`); - } + // Convert to pandoc arguments and add to args + const variableArgs = this.variableMapper.convertVariablesToPandocArgs(typstVariables); + args.push(...variableArgs); } /** diff --git a/src/converters/pandoc/TypstVariableMapper.ts b/src/converters/pandoc/TypstVariableMapper.ts new file mode 100644 index 0000000..453b834 --- /dev/null +++ b/src/converters/pandoc/TypstVariableMapper.ts @@ -0,0 +1,323 @@ +/** + * Typst Variable Mapper + * Handles mapping of ExportConfig and plugin settings to Typst template variables + */ + +import { mapToTypstPaperSize } from '../../utils/paperSizeMapper'; +import { PandocOptions } from '../converterTypes'; +import type { obsidianTypstPDFExportSettings } from '../../core/settings'; + +export interface TypstVariable { + name: string; + value: string; +} + +export class TypstVariableMapper { + private plugin: any; + + constructor(plugin: any) { + this.plugin = plugin; + } + + /** + * Map all variables from ExportConfig and plugin settings to Typst variables + * ExportConfig variables take priority over plugin settings + */ + mapAllVariablesToTypst(pandocOptions: PandocOptions): TypstVariable[] { + const variables: TypstVariable[] = []; + + // Add variables from ExportConfig (these take priority) + const exportConfigVariables = this.mapExportConfigVariables(pandocOptions); + variables.push(...exportConfigVariables); + + // Add fallback variables from plugin settings (only if not already present) + const pluginSettingsVariables = this.mapPluginSettingsVariables(pandocOptions); + variables.push(...pluginSettingsVariables); + + return variables; + } + + /** + * Map ExportConfig variables to Typst variables with transformations + */ + private mapExportConfigVariables(pandocOptions: PandocOptions): TypstVariable[] { + const variables: TypstVariable[] = []; + + if (!pandocOptions.variables) { + return variables; + } + + for (const [key, value] of Object.entries(pandocOptions.variables)) { + if (value !== null && value !== undefined && value.toString().trim() !== '') { + const mappedVariable = this.transformExportConfigVariable(key, value); + if (mappedVariable) { + variables.push(mappedVariable); + } + } + } + + return variables; + } + + /** + * Transform a single ExportConfig variable to Typst variable with name mapping and unit conversion + */ + private transformExportConfigVariable(key: string, value: string | number | boolean): TypstVariable | null { + // Handle special variable name mappings for Typst compatibility + let variableName = key; + let processedValue = value; + + switch (key) { + case 'bodyFont': + variableName = 'font'; + break; + case 'headingFont': + variableName = 'heading_font'; + break; + case 'monospaceFont': + variableName = 'monospace_font'; + break; + case 'bodyFontSize': + variableName = 'fontsize'; + processedValue = value + 'pt'; + break; + case 'pageSize': + variableName = 'paper'; + // Convert to Typst-compatible paper size + processedValue = mapToTypstPaperSize(value.toString()); + break; + case 'marginTop': + variableName = 'margin_top'; + processedValue = value + 'cm'; + break; + case 'marginBottom': + variableName = 'margin_bottom'; + processedValue = value + 'cm'; + break; + case 'marginLeft': + variableName = 'margin_left'; + processedValue = value + 'cm'; + break; + case 'marginRight': + variableName = 'margin_right'; + processedValue = value + 'cm'; + break; + // Keep other variables as-is (orientation, flipped, width, etc.) + } + + return { + name: variableName, + value: processedValue.toString() + }; + } + + /** + * Map plugin settings to Typst variables as fallbacks (only if not already present in ExportConfig) + */ + private mapPluginSettingsVariables(pandocOptions: PandocOptions): TypstVariable[] { + const variables: TypstVariable[] = []; + + if (!this.plugin || !this.plugin.settings) { + return variables; + } + + const settings: obsidianTypstPDFExportSettings = this.plugin.settings; + const existingVars = pandocOptions.variables || {}; + + // Add typography variables only if not already present in variables + variables.push(...this.mapTypographyVariables(settings, existingVars)); + + // Add page setup variables only if not already present + variables.push(...this.mapPageSetupVariables(settings, existingVars)); + + // Add export format variable - only use settings default if not already provided + if (!existingVars.export_format && settings.exportDefaults && settings.exportDefaults.format) { + variables.push({ + name: 'export_format', + value: settings.exportDefaults.format + }); + } + + return variables; + } + + /** + * Map typography settings to Typst variables + */ + private mapTypographyVariables(settings: obsidianTypstPDFExportSettings, existingVars: Record): TypstVariable[] { + const variables: TypstVariable[] = []; + + if (!settings.typography) { + return variables; + } + + // Font variables + if (settings.typography.fonts) { + // Only add if not already in variables from ExportConfig + if (!existingVars.bodyFont && !existingVars.font && settings.typography.fonts.body) { + variables.push({ + name: 'font', + value: settings.typography.fonts.body + }); + } + if (!existingVars.headingFont && !existingVars.heading_font && settings.typography.fonts.heading) { + variables.push({ + name: 'heading_font', + value: settings.typography.fonts.heading + }); + } + if (!existingVars.monospaceFont && !existingVars.monospace_font && settings.typography.fonts.monospace) { + variables.push({ + name: 'monospace_font', + value: settings.typography.fonts.monospace + }); + } + } + + // Font size variables + if (settings.typography.fontSizes) { + if (!existingVars.bodyFontSize && !existingVars.fontsize && settings.typography.fontSizes.body) { + variables.push({ + name: 'fontsize', + value: `${settings.typography.fontSizes.body}pt` + }); + } + } + + return variables; + } + + /** + * Map page setup settings to Typst variables + */ + private mapPageSetupVariables(settings: obsidianTypstPDFExportSettings, existingVars: Record): TypstVariable[] { + const variables: TypstVariable[] = []; + + if (!settings.pageSetup) { + return variables; + } + + // Paper size variable + if (!existingVars.pageSize && !existingVars.paper && settings.pageSetup.size) { + const typstPaperSize = mapToTypstPaperSize(settings.pageSetup.size); + variables.push({ + name: 'paper', + value: typstPaperSize + }); + } + + // Orientation variable + if (!existingVars.orientation && settings.pageSetup.orientation) { + variables.push({ + name: 'orientation', + value: settings.pageSetup.orientation + }); + } + + // Margin variables - only add if not already specified + if (settings.pageSetup.margins) { + variables.push(...this.mapMarginVariables(settings.pageSetup.margins, existingVars)); + } + + return variables; + } + + /** + * Map margin settings to Typst variables + */ + private mapMarginVariables(margins: any, existingVars: Record): TypstVariable[] { + const variables: TypstVariable[] = []; + + if (!existingVars.marginTop && !existingVars.margin_top && margins.top !== undefined) { + variables.push({ + name: 'margin_top', + value: `${margins.top}cm` + }); + } + if (!existingVars.marginRight && !existingVars.margin_right && margins.right !== undefined) { + variables.push({ + name: 'margin_right', + value: `${margins.right}cm` + }); + } + if (!existingVars.marginBottom && !existingVars.margin_bottom && margins.bottom !== undefined) { + variables.push({ + name: 'margin_bottom', + value: `${margins.bottom}cm` + }); + } + if (!existingVars.marginLeft && !existingVars.margin_left && margins.left !== undefined) { + variables.push({ + name: 'margin_left', + value: `${margins.left}cm` + }); + } + + return variables; + } + + /** + * Get a set of variable names that are already defined in ExportConfig + * Used for priority checking between ExportConfig and plugin settings + */ + getExportConfigVariableNames(pandocOptions: PandocOptions): Set { + const names = new Set(); + + if (!pandocOptions.variables) { + return names; + } + + for (const [key, value] of Object.entries(pandocOptions.variables)) { + if (value !== null && value !== undefined && value.toString().trim() !== '') { + // Add both original key and mapped name + names.add(key); + + // Add mapped names for priority checking + switch (key) { + case 'bodyFont': + names.add('font'); + break; + case 'headingFont': + names.add('heading_font'); + break; + case 'monospaceFont': + names.add('monospace_font'); + break; + case 'bodyFontSize': + names.add('fontsize'); + break; + case 'pageSize': + names.add('paper'); + break; + case 'marginTop': + names.add('margin_top'); + break; + case 'marginBottom': + names.add('margin_bottom'); + break; + case 'marginLeft': + names.add('margin_left'); + break; + case 'marginRight': + names.add('margin_right'); + break; + } + } + } + + return names; + } + + /** + * Convert TypstVariable array to pandoc argument format + */ + convertVariablesToPandocArgs(variables: TypstVariable[]): string[] { + const args: string[] = []; + + for (const variable of variables) { + args.push('-V', `${variable.name}=${variable.value}`); + } + + return args; + } +} \ No newline at end of file