diff --git a/main.ts b/main.ts index 15f649e..bb8cd76 100644 --- a/main.ts +++ b/main.ts @@ -24,6 +24,7 @@ import { ExportConfigModal } from './src/modal/ExportConfigModal'; import { ExportConfig, ExportConfigModalSettings } from './src/modal/types'; import { TemplateManager } from './src/templates/TemplateManager'; import { FolderSuggest } from './src/components/FolderSuggest'; +import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper'; import * as path from 'path'; import * as fs from 'fs'; @@ -1205,17 +1206,19 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { new Setting(containerEl) .setName('Page size') .setDesc('Paper size for PDF output') - .addDropdown(dropdown => dropdown - .addOption('a4', 'A4') - .addOption('a5', 'A5') - .addOption('letter', 'US Letter') - .addOption('legal', 'US Legal') - .addOption('a3', 'A3') - .setValue(this.plugin.settings.pageSetup.size) - .onChange(async (value) => { - this.plugin.settings.pageSetup.size = value; - await this.plugin.saveSettings(); - })); + .addDropdown(dropdown => { + // Add all supported paper sizes + SUPPORTED_PAPER_SIZES.forEach(paperSize => { + dropdown.addOption(paperSize.key, paperSize.displayName); + }); + + return dropdown + .setValue(this.plugin.settings.pageSetup.size) + .onChange(async (value) => { + this.plugin.settings.pageSetup.size = value; + await this.plugin.saveSettings(); + }); + }); new Setting(containerEl) .setName('Page orientation') diff --git a/src/converters/PandocTypstConverter.ts b/src/converters/PandocTypstConverter.ts index a3dea0c..868cad8 100644 --- a/src/converters/PandocTypstConverter.ts +++ b/src/converters/PandocTypstConverter.ts @@ -8,6 +8,7 @@ import * as fs from 'fs'; import { promises as fsPromises } from 'fs'; import * as path from 'path'; import * as os from 'os'; +import { mapToTypstPaperSize } from '../utils/paperSizeMapper'; import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './types'; import type { obsidianTypstPDFExportSettings } from '../core/settings'; @@ -364,6 +365,8 @@ export class PandocTypstConverter { 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'; @@ -379,6 +382,8 @@ export class PandocTypstConverter { break; case 'pageSize': variableName = 'paper'; + // Convert to Typst-compatible paper size + processedValue = mapToTypstPaperSize(value.toString()); break; case 'marginTop': variableName = 'margin_top'; @@ -395,7 +400,7 @@ export class PandocTypstConverter { // Keep other variables as-is (orientation, flipped, width, etc.) } - args.push('-V', `${variableName}=${value}`); + args.push('-V', `${variableName}=${processedValue}`); } } } @@ -437,7 +442,8 @@ export class PandocTypstConverter { // Add page setup variables only if not already present if (settings.pageSetup) { if (!existingVars.pageSize && !existingVars.paper && settings.pageSetup.size) { - args.push('-V', `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}`); diff --git a/src/modal/sections/LayoutSection.ts b/src/modal/sections/LayoutSection.ts index 75d5ba1..cb58117 100644 --- a/src/modal/sections/LayoutSection.ts +++ b/src/modal/sections/LayoutSection.ts @@ -5,6 +5,7 @@ import { Setting } from 'obsidian'; import { ModalSection, ModalState, ValidationResult } from '../types'; +import { SUPPORTED_PAPER_SIZES } from '../../utils/paperSizeMapper'; export class LayoutSection implements ModalSection { private container: HTMLElement | null = null; @@ -26,12 +27,12 @@ export class LayoutSection implements ModalSection { .setName('Page size') .setDesc('Standard page sizes for the PDF document') .addDropdown(dropdown => { + // Add all supported paper sizes + SUPPORTED_PAPER_SIZES.forEach(paperSize => { + dropdown.addOption(paperSize.key, paperSize.displayName); + }); + dropdown - .addOption('a4', 'A4 (210 × 297 mm)') - .addOption('letter', 'Letter (8.5 × 11 in)') - .addOption('legal', 'Legal (8.5 × 14 in)') - .addOption('a3', 'A3 (297 × 420 mm)') - .addOption('a5', 'A5 (148 × 210 mm)') .setValue('a4') // Default to A4 .onChange(value => { state.updateTemplateVariables({ pageSize: value }); diff --git a/src/modal/sections/PageSetupSection.ts b/src/modal/sections/PageSetupSection.ts index f125a42..f44b845 100644 --- a/src/modal/sections/PageSetupSection.ts +++ b/src/modal/sections/PageSetupSection.ts @@ -5,6 +5,7 @@ import { Setting } from 'obsidian'; import { ModalSection, ModalState, ValidationResult } from '../types'; +import { SUPPORTED_PAPER_SIZES } from '../../utils/paperSizeMapper'; export class PageSetupSection implements ModalSection { private container: HTMLElement | null = null; @@ -26,12 +27,12 @@ export class PageSetupSection implements ModalSection { .setName('Page size') .setDesc('Standard page sizes for the PDF document') .addDropdown(dropdown => { + // Add all supported paper sizes + SUPPORTED_PAPER_SIZES.forEach(paperSize => { + dropdown.addOption(paperSize.key, paperSize.displayName); + }); + dropdown - .addOption('a4', 'A4 (210 × 297 mm)') - .addOption('letter', 'Letter (8.5 × 11 in)') - .addOption('legal', 'Legal (8.5 × 14 in)') - .addOption('a3', 'A3 (297 × 420 mm)') - .addOption('a5', 'A5 (148 × 210 mm)') .setValue(state.templateVariables.pageSize || 'a4') .onChange(value => { state.updateTemplateVariables({ pageSize: value }); diff --git a/src/template-validator.ts b/src/template-validator.ts index 54f413d..6b08106 100644 --- a/src/template-validator.ts +++ b/src/template-validator.ts @@ -1,5 +1,6 @@ import { TemplateValidationResult, SubstitutionContext } from './types'; import { TemplateSubstitution } from './template-substitution'; +import { getValidTypstPaperSizes } from './utils/paperSizeMapper'; /** * Comprehensive template validation system @@ -378,7 +379,7 @@ export class TemplateValidator { // Check for invalid paper sizes const paperSizePattern = /paper:\s*"([^"]+)"/g; - const validPaperSizes = ['a4', 'a3', 'a5', 'us-letter', 'us-legal', 'iso-b4', 'iso-b5']; + const validPaperSizes = getValidTypstPaperSizes(); let match; while ((match = paperSizePattern.exec(templateContent)) !== null) { diff --git a/src/utils/paperSizeMapper.ts b/src/utils/paperSizeMapper.ts new file mode 100644 index 0000000..d518d3c --- /dev/null +++ b/src/utils/paperSizeMapper.ts @@ -0,0 +1,98 @@ +/** + * Paper size mapping utilities for Typst compatibility + */ + +export interface PaperSizeInfo { + /** The value used internally and in UI */ + key: string; + /** Display name for UI dropdowns */ + displayName: string; + /** The actual Typst paper size value to use */ + typstValue: string; +} + +/** + * Supported paper sizes with their Typst-compatible mappings + */ +export const SUPPORTED_PAPER_SIZES: PaperSizeInfo[] = [ + { + key: 'a3', + displayName: 'A3 (297 × 420 mm)', + typstValue: 'a3' + }, + { + key: 'a4', + displayName: 'A4 (210 × 297 mm)', + typstValue: 'a4' + }, + { + key: 'a5', + displayName: 'A5 (148 × 210 mm)', + typstValue: 'a5' + }, + { + key: 'a6', + displayName: 'A6 (105 × 148 mm)', + typstValue: 'a6' + }, + { + key: 'eu-business-card', + displayName: 'EU Business Card (85 × 55 mm)', + typstValue: 'eu-business-card' + }, + { + key: 'us-letter', + displayName: 'US Letter (8.5 × 11 in)', + typstValue: 'us-letter' + }, + { + key: 'us-legal', + displayName: 'US Legal (8.5 × 14 in)', + typstValue: 'us-legal' + }, + { + key: 'us-business-card', + displayName: 'US Business Card (3.5 × 2 in)', + typstValue: 'us-business-card' + } +]; + +/** + * Default paper size + */ +export const DEFAULT_PAPER_SIZE = 'a4'; + +/** + * Map a paper size key to its Typst-compatible value + * @param paperSizeKey - The paper size key from UI/settings + * @returns The Typst-compatible paper size value + */ +export function mapToTypstPaperSize(paperSizeKey: string): string { + // Find the paper size in our supported list + const paperSize = SUPPORTED_PAPER_SIZES.find(size => size.key === paperSizeKey); + + if (paperSize) { + return paperSize.typstValue; + } + + // Fallback to default if unknown + console.warn(`Unknown paper size: ${paperSizeKey}. Using default: ${DEFAULT_PAPER_SIZE}`); + return SUPPORTED_PAPER_SIZES.find(size => size.key === DEFAULT_PAPER_SIZE)?.typstValue || 'a4'; +} + +/** + * Get all valid Typst paper size values for validation + * @returns Array of valid Typst paper size strings + */ +export function getValidTypstPaperSizes(): string[] { + return SUPPORTED_PAPER_SIZES.map(size => size.typstValue); +} + +/** + * Check if a paper size key is supported + * @param paperSizeKey - The paper size key to check + * @returns True if the paper size is supported + */ +export function isSupportedPaperSize(paperSizeKey: string): boolean { + return SUPPORTED_PAPER_SIZES.some(size => size.key === paperSizeKey); +} \ No newline at end of file