From d40253e6c4c076fb6bb2631dc80db3ea00d4afe9 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 5 Sep 2025 10:41:31 +0200 Subject: [PATCH] Enhances template handling for Typst exports. Improves template resolution for Typst exports by adding the plugin's templates directory as a resource path. This allows Typst to correctly locate and utilize template files within the plugin. Updates the template path handling to use relative paths from the vault root, ensuring proper import behavior within Typst. --- main.ts | 2 +- src/converters/PandocTypstConverter.ts | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/main.ts b/main.ts index 90be292..b9ab7ec 100644 --- a/main.ts +++ b/main.ts @@ -448,8 +448,8 @@ export class obsidianTypstPDFExport extends Plugin { * Export a file with specific configuration */ private async exportFileWithConfig(file: TFile, config: ExportConfig): Promise { - const pluginDir = this.manifest.dir!; const vaultPath = (this.app.vault.adapter as any).basePath; + const pluginDir = path.join(vaultPath, this.manifest.dir!); // Create controller for this export (allows cancellation) this.currentExportController = new AbortController(); diff --git a/src/converters/PandocTypstConverter.ts b/src/converters/PandocTypstConverter.ts index 8d686ff..2e67dec 100644 --- a/src/converters/PandocTypstConverter.ts +++ b/src/converters/PandocTypstConverter.ts @@ -380,17 +380,22 @@ export class PandocTypstConverter { throw new Error(`Universal wrapper template not found at: ${wrapperPath}`); } + args.push('--template', wrapperPath); + // Add plugin templates directory as a resource path so Typst can find template files + const templatesDir = path.resolve(absolutePluginDir, 'templates'); + // Quote the path to handle spaces and special characters + args.push('--resource-path', `"${templatesDir}"`); + // Pass the actual template path as a variable - // Make the path relative to the vault (working directory) for Typst + // Use relative path from vault root for Typst import let templatePathForTypst = this.pandocOptions.template; - if (this.pandocOptions.vaultBasePath && path.isAbsolute(templatePathForTypst)) { - // If template path is absolute and inside vault, make it relative - if (templatePathForTypst.startsWith(this.pandocOptions.vaultBasePath)) { - templatePathForTypst = path.relative(this.pandocOptions.vaultBasePath, templatePathForTypst); - } + if (path.isAbsolute(templatePathForTypst) && this.pandocOptions.vaultBasePath) { + // Make template path relative to vault for Typst import + templatePathForTypst = path.relative(this.pandocOptions.vaultBasePath, templatePathForTypst); } + args.push('-V', `template_path=${templatePathForTypst}`); }