From 99f55b7ecb47501f51fba1108796f7d4cd1958d8 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Mon, 1 Sep 2025 11:50:16 +0200 Subject: [PATCH] feat: implement universal wrapper for proper export format variable passing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add universal Pandoc wrapper template to handle variable substitution - Fix export_format variable not reaching Typst templates via -V flag - Implement single-page/multi-page format selection functionality - Update all templates to use conditional height parameter with spread operator - Add pluginDir to PandocOptions for proper wrapper path resolution Technical changes: - Create templates/universal-wrapper.pandoc.typ for Pandoc template system - Modify buildPandocArgs to use --template instead of -V template= - Update page layout logic in all templates using ..if export_format == "single-page" - Replace duplicate conditional set page() blocks with single streamlined approach - Fix type checking issues (string -> str) for consistency Templates updated: - default.typ: Clean minimal style with conditional height - article.typ: Magazine-style layout with conditional height - report.typ: Professional business format with conditional height - modern.typ: Renamed from single-page.typ for better naming consistency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- main.ts | 33 ++++++++++++++++++----- templates/article.typ | 6 +++-- templates/default.typ | 7 +++-- templates/{single-page.typ => modern.typ} | 29 +++++++++++++------- templates/report.typ | 4 ++- templates/universal-wrapper.pandoc.typ | 31 +++++++++++++++++++++ 6 files changed, 90 insertions(+), 20 deletions(-) rename templates/{single-page.typ => modern.typ} (91%) create mode 100644 templates/universal-wrapper.pandoc.typ diff --git a/main.ts b/main.ts index 3b2d988..79a884d 100644 --- a/main.ts +++ b/main.ts @@ -26,6 +26,8 @@ interface PandocOptions { generateIntermediateTypst?: boolean; /** Vault base path for resolving attachment paths */ vaultBasePath?: string; + /** Plugin directory path for resolving plugin-relative paths */ + pluginDir?: string; } /** @@ -321,9 +323,24 @@ if (this.pandocOptions.vaultBasePath) { } } -// Use -V template= syntax for Typst templates (not --template) +// Use universal wrapper with --template and pass actual template as template_path variable if (this.pandocOptions.template) { - args.push('-V', `template=${this.pandocOptions.template}`); + // Get absolute path to universal wrapper using plugin directory from pandocOptions + const path = require('path'); + const fs = require('fs'); + const absolutePluginDir = this.pandocOptions.pluginDir || ''; + const wrapperPath = path.resolve(absolutePluginDir, 'templates', 'universal-wrapper.pandoc.typ'); + console.log('Universal wrapper path:', wrapperPath); + + // Verify wrapper exists + if (!fs.existsSync(wrapperPath)) { + throw new Error(`Universal wrapper template not found at: ${wrapperPath}`); + } + + args.push('--template', wrapperPath); + + // Pass the actual template path as a variable + args.push('-V', `template_path=${this.pandocOptions.template}`); } // Add variables for document metadata @@ -1599,9 +1616,13 @@ try { pandocPath: this.settings.pandocPath, typstPath: this.settings.typstPath, template: relativeTemplatePath, // Use relative path from vault directory - variables: config.templateVariables || {}, + variables: { + ...config.templateVariables || {}, + export_format: config.format // Pass the format selection to control page height + }, timeout: 60000, - vaultBasePath: vaultBasePath // Add vault base path for attachment resolution + vaultBasePath: vaultBasePath, // Add vault base path for attachment resolution + pluginDir: absolutePluginDir // Add plugin directory for universal wrapper path resolution } as any; console.log('Export: Pandoc options:', pandocOptions); @@ -2660,7 +2681,7 @@ export class ExportConfigModal extends Modal { 'default.typ', 'article.typ', 'report.typ', - 'single-page.typ' + 'modern.typ' ]; } @@ -3161,7 +3182,7 @@ export class ExportConfigModal extends Modal { 'default': 'Default', 'article': 'Article', 'report': 'Report', - 'single-page': 'Single Page' + 'modern': 'Modern' }; return displayNames[templateKey] || templateKey.charAt(0).toUpperCase() + templateKey.slice(1); diff --git a/templates/article.typ b/templates/article.typ index 9bb27b5..69ee16d 100644 --- a/templates/article.typ +++ b/templates/article.typ @@ -96,9 +96,11 @@ margin } + // Set page layout with conditional height based on export format set page( paper: paper, margin: page-margin, + ..if export_format == "single-page" { (height: auto,) } else { (:) }, numbering: pagenumbering, header: context { if counter(page).get().first() > 1 [ @@ -118,7 +120,7 @@ // Mixed fonts - serif body, sans-serif headings // Use UI-configured font if available, otherwise use template default - let body-font = if font != none and type(font) == "string" { + let body-font = if font != none and type(font) == str { (font, "Concourse OT", "Georgia", "Times New Roman", "serif") } else { font @@ -143,7 +145,7 @@ // Magazine-style heading styles // Use UI-configured heading font and size if available - let heading-font = if heading_font != none and type(heading_font) == "string" { + let heading-font = if heading_font != none and type(heading_font) == str { (heading_font, "SF Pro Text", "Helvetica Neue", "Arial", "sans-serif") } else if heading_font != none { heading_font diff --git a/templates/default.typ b/templates/default.typ index 72553e1..ac49e95 100644 --- a/templates/default.typ +++ b/templates/default.typ @@ -93,9 +93,12 @@ margin } + + // Set page layout with conditional height based on export format set page( paper: paper, margin: page-margin, + ..if export_format == "single-page" { (height: auto,) } else { (:) }, numbering: pagenumbering, header: context { if counter(page).get().first() > 1 [ @@ -106,7 +109,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) == "string" { + let body-font = if font != none and type(font) == str { (font, "Concourse OT", "Helvetica Neue", "Arial", "sans-serif") } else { font @@ -131,7 +134,7 @@ // Clean heading styles // Use UI-configured heading font and size if available - let heading-font = if heading_font != none and type(heading_font) == "string" { + let heading-font = if heading_font != none and type(heading_font) == str { (heading_font, "SF Pro Text", "Helvetica Neue", "Arial", "sans-serif") } else if heading_font != none { heading_font diff --git a/templates/single-page.typ b/templates/modern.typ similarity index 91% rename from templates/single-page.typ rename to templates/modern.typ index f6aeb38..bed0ee9 100644 --- a/templates/single-page.typ +++ b/templates/modern.typ @@ -91,17 +91,28 @@ margin } - set page( - paper: paper, - margin: page-margin, - height: auto, - numbering: pagenumbering, - fill: rgb("#fafafa") - ) + // Set page layout with conditional height based on export format + if export_format == "single-page" { + set page( + paper: paper, + margin: page-margin, + height: auto, // Auto height for continuous single page + numbering: pagenumbering, + fill: rgb("#fafafa") + ) + } else { + set page( + paper: paper, + margin: page-margin, + // No height specified = uses default paper size height + numbering: pagenumbering, + fill: rgb("#fafafa") + ) + } // Modern sans-serif fonts // Use UI-configured font if available, otherwise use template default - let body-font = if font != none and type(font) == "string" { + let body-font = if font != none and type(font) == str { (font, "Concourse OT", "Helvetica Neue", "Arial", "sans-serif") } else { font @@ -126,7 +137,7 @@ // Modern heading styles with color accents // Use UI-configured heading font and size if available - let heading-font = if heading_font != none and type(heading_font) == "string" { + let heading-font = if heading_font != none and type(heading_font) == str { (heading_font, "SF Pro Text", "Helvetica Neue", "Arial", "sans-serif") } else if heading_font != none { heading_font diff --git a/templates/report.typ b/templates/report.typ index 15d1727..b996a50 100644 --- a/templates/report.typ +++ b/templates/report.typ @@ -102,9 +102,11 @@ margin } + // Set page layout with conditional height based on export format set page( paper: paper, margin: page-margin, + ..if export_format == "single-page" { (height: auto,) } else { (:) }, numbering: pagenumbering, header: context { let page-num = counter(page).get().first() @@ -136,7 +138,7 @@ // Business document font // Use UI-configured font if available, otherwise use template default - let body-font = if font != none and type(font) == "string" { + let body-font = if font != none and type(font) == str { (font, "Concourse OT", "Helvetica Neue", "Arial", "sans-serif") } else { font diff --git a/templates/universal-wrapper.pandoc.typ b/templates/universal-wrapper.pandoc.typ new file mode 100644 index 0000000..e19fb4f --- /dev/null +++ b/templates/universal-wrapper.pandoc.typ @@ -0,0 +1,31 @@ +// 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 + +#show: doc => conf( + $if(title)$title: [$title$],$else$title: none,$endif$ + $if(authors)$authors: ($for(authors)$(name: "$authors.name$", email: "$authors.email$", affiliation: "$authors.affiliation$")$sep$, $endfor$),$else$authors: (),$endif$ + $if(date)$date: [$date$],$else$date: none,$endif$ + $if(lang)$lang: "$lang$",$else$lang: "en",$endif$ + $if(region)$region: "$region$",$else$region: "US",$endif$ + $if(paper)$paper: "$paper$",$else$paper: "a4",$endif$ + $if(margin)$margin: (x: $margin.x$, y: $margin.y$),$else$$if(margin_top)$margin: (top: $margin_top$, right: $if(margin_right)$$margin_right$$else$1.5cm$endif$, bottom: $if(margin_bottom)$$margin_bottom$$else$1.5cm$endif$, left: $if(margin_left)$$margin_left$$else$1.5cm$endif$),$else$margin: (x: 1.5cm, y: 1.5cm),$endif$$endif$ + $if(cols)$cols: $cols$,$else$cols: 1,$endif$ + $if(font)$font: "$font$",$else$font: ("Concourse OT", "Helvetica Neue", "Arial", "sans-serif"),$endif$ + $if(fontsize)$fontsize: $fontsize$,$else$fontsize: 11pt,$endif$ + $if(sectionnumbering)$sectionnumbering: "$sectionnumbering$",$else$sectionnumbering: "1.",$endif$ + $if(pagenumbering)$pagenumbering: "$pagenumbering$",$else$pagenumbering: "1",$endif$ + // Support for UI-configurable options + $if(heading_font)$heading_font: "$heading_font$",$else$heading_font: none,$endif$ + $if(monospace_font)$monospace_font: "$monospace_font$",$else$monospace_font: none,$endif$ + $if(heading_fontsize)$heading_fontsize: $heading_fontsize$,$else$heading_fontsize: none,$endif$ + $if(small_fontsize)$small_fontsize: $small_fontsize$,$else$small_fontsize: none,$endif$ + $if(margin_top)$margin_top: $margin_top$,$else$margin_top: none,$endif$ + $if(margin_right)$margin_right: $margin_right$,$else$margin_right: none,$endif$ + $if(margin_bottom)$margin_bottom: $margin_bottom$,$else$margin_bottom: none,$endif$ + $if(margin_left)$margin_left: $margin_left$,$else$margin_left: none,$endif$ + $if(orientation)$orientation: "$orientation$",$else$orientation: "portrait",$endif$ + export_format: "$if(export_format)$$export_format$$else$standard$endif$", + [$body$] +) \ No newline at end of file