Enhances paper size selection and handling

Updates the paper size selection in settings and modals to use a predefined list of supported sizes, ensuring consistency across the plugin and compatibility with Typst.

It also maps the selected paper size to its Typst equivalent before passing it to the converter, preventing issues with unsupported sizes.
The change also ensures that template validation uses the correct paper sizes.
This commit is contained in:
AlexKucera committed 2025-09-03 14:35:34 +02:00
1 parent c749852125
commit 26ddbb7f35
6 files changed
+134 -24

No files matched your search

+14 -11
View File
@@ -24,6 +24,7 @@ import { ExportConfigModal } from './src/modal/ExportConfigModal';
import { ExportConfig, ExportConfigModalSettings } from './src/modal/types'; import { ExportConfig, ExportConfigModalSettings } from './src/modal/types';
import { TemplateManager } from './src/templates/TemplateManager'; import { TemplateManager } from './src/templates/TemplateManager';
import { FolderSuggest } from './src/components/FolderSuggest'; import { FolderSuggest } from './src/components/FolderSuggest';
import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
@@ -1205,17 +1206,19 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
new Setting(containerEl) new Setting(containerEl)
.setName('Page size') .setName('Page size')
.setDesc('Paper size for PDF output') .setDesc('Paper size for PDF output')
.addDropdown(dropdown => dropdown .addDropdown(dropdown => {
.addOption('a4', 'A4') // Add all supported paper sizes
.addOption('a5', 'A5') SUPPORTED_PAPER_SIZES.forEach(paperSize => {
.addOption('letter', 'US Letter') dropdown.addOption(paperSize.key, paperSize.displayName);
.addOption('legal', 'US Legal') });
.addOption('a3', 'A3')
.setValue(this.plugin.settings.pageSetup.size) return dropdown
.onChange(async (value) => { .setValue(this.plugin.settings.pageSetup.size)
this.plugin.settings.pageSetup.size = value; .onChange(async (value) => {
await this.plugin.saveSettings(); this.plugin.settings.pageSetup.size = value;
})); await this.plugin.saveSettings();
});
});
new Setting(containerEl) new Setting(containerEl)
.setName('Page orientation') .setName('Page orientation')
+8 -2
View File
@@ -8,6 +8,7 @@ import * as fs from 'fs';
import { promises as fsPromises } from 'fs'; import { promises as fsPromises } from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import { mapToTypstPaperSize } from '../utils/paperSizeMapper';
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './types'; import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './types';
import type { obsidianTypstPDFExportSettings } from '../core/settings'; import type { obsidianTypstPDFExportSettings } from '../core/settings';
@@ -364,6 +365,8 @@ export class PandocTypstConverter {
if (value !== null && value !== undefined && value.toString().trim() !== '') { if (value !== null && value !== undefined && value.toString().trim() !== '') {
// Handle special variable name mappings for Typst compatibility // Handle special variable name mappings for Typst compatibility
let variableName = key; let variableName = key;
let processedValue = value;
switch (key) { switch (key) {
case 'bodyFont': case 'bodyFont':
variableName = 'font'; variableName = 'font';
@@ -379,6 +382,8 @@ export class PandocTypstConverter {
break; break;
case 'pageSize': case 'pageSize':
variableName = 'paper'; variableName = 'paper';
// Convert to Typst-compatible paper size
processedValue = mapToTypstPaperSize(value.toString());
break; break;
case 'marginTop': case 'marginTop':
variableName = 'margin_top'; variableName = 'margin_top';
@@ -395,7 +400,7 @@ export class PandocTypstConverter {
// Keep other variables as-is (orientation, flipped, width, etc.) // 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 // Add page setup variables only if not already present
if (settings.pageSetup) { if (settings.pageSetup) {
if (!existingVars.pageSize && !existingVars.paper && settings.pageSetup.size) { 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) { if (!existingVars.orientation && settings.pageSetup.orientation) {
args.push('-V', `orientation=${settings.pageSetup.orientation}`); args.push('-V', `orientation=${settings.pageSetup.orientation}`);
+6 -5
View File
@@ -5,6 +5,7 @@
import { Setting } from 'obsidian'; import { Setting } from 'obsidian';
import { ModalSection, ModalState, ValidationResult } from '../types'; import { ModalSection, ModalState, ValidationResult } from '../types';
import { SUPPORTED_PAPER_SIZES } from '../../utils/paperSizeMapper';
export class LayoutSection implements ModalSection { export class LayoutSection implements ModalSection {
private container: HTMLElement | null = null; private container: HTMLElement | null = null;
@@ -26,12 +27,12 @@ export class LayoutSection implements ModalSection {
.setName('Page size') .setName('Page size')
.setDesc('Standard page sizes for the PDF document') .setDesc('Standard page sizes for the PDF document')
.addDropdown(dropdown => { .addDropdown(dropdown => {
// Add all supported paper sizes
SUPPORTED_PAPER_SIZES.forEach(paperSize => {
dropdown.addOption(paperSize.key, paperSize.displayName);
});
dropdown 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 .setValue('a4') // Default to A4
.onChange(value => { .onChange(value => {
state.updateTemplateVariables({ pageSize: value }); state.updateTemplateVariables({ pageSize: value });
+6 -5
View File
@@ -5,6 +5,7 @@
import { Setting } from 'obsidian'; import { Setting } from 'obsidian';
import { ModalSection, ModalState, ValidationResult } from '../types'; import { ModalSection, ModalState, ValidationResult } from '../types';
import { SUPPORTED_PAPER_SIZES } from '../../utils/paperSizeMapper';
export class PageSetupSection implements ModalSection { export class PageSetupSection implements ModalSection {
private container: HTMLElement | null = null; private container: HTMLElement | null = null;
@@ -26,12 +27,12 @@ export class PageSetupSection implements ModalSection {
.setName('Page size') .setName('Page size')
.setDesc('Standard page sizes for the PDF document') .setDesc('Standard page sizes for the PDF document')
.addDropdown(dropdown => { .addDropdown(dropdown => {
// Add all supported paper sizes
SUPPORTED_PAPER_SIZES.forEach(paperSize => {
dropdown.addOption(paperSize.key, paperSize.displayName);
});
dropdown 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') .setValue(state.templateVariables.pageSize || 'a4')
.onChange(value => { .onChange(value => {
state.updateTemplateVariables({ pageSize: value }); state.updateTemplateVariables({ pageSize: value });
+2 -1
View File
@@ -1,5 +1,6 @@
import { TemplateValidationResult, SubstitutionContext } from './types'; import { TemplateValidationResult, SubstitutionContext } from './types';
import { TemplateSubstitution } from './template-substitution'; import { TemplateSubstitution } from './template-substitution';
import { getValidTypstPaperSizes } from './utils/paperSizeMapper';
/** /**
* Comprehensive template validation system * Comprehensive template validation system
@@ -378,7 +379,7 @@ export class TemplateValidator {
// Check for invalid paper sizes // Check for invalid paper sizes
const paperSizePattern = /paper:\s*"([^"]+)"/g; const paperSizePattern = /paper:\s*"([^"]+)"/g;
const validPaperSizes = ['a4', 'a3', 'a5', 'us-letter', 'us-legal', 'iso-b4', 'iso-b5']; const validPaperSizes = getValidTypstPaperSizes();
let match; let match;
while ((match = paperSizePattern.exec(templateContent)) !== null) { while ((match = paperSizePattern.exec(templateContent)) !== null) {
+98
View File
@@ -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);
}