import { App, normalizePath, TFile, TFolder, Notice } from 'obsidian'; import { Template, TemplateVariable, TemplateMetadata, TemplateValidationResult, SubstitutionContext } from './types'; import { TemplateSubstitution } from './template-substitution'; import { TemplateValidator } from './template-validator'; /** * Manages Typst templates, including built-in and custom user templates */ export class TemplateManager { private app: App; private builtInTemplates: Map = new Map(); private customTemplates: Map = new Map(); private customTemplateDirectory: string = 'Typst Templates'; constructor(app: App, customTemplateDirectory?: string) { this.app = app; if (customTemplateDirectory) { this.customTemplateDirectory = customTemplateDirectory; } this.initializeBuiltInTemplates(); } /** * Initialize built-in Typst templates with modern 0.11+ syntax */ private initializeBuiltInTemplates(): void { // Default template - basic document this.builtInTemplates.set('default.typ', `#set page( paper: "a4", margin: (x: 2.5cm, y: 2cm) ) #set text( font: "Liberation Serif", size: 12pt, lang: "en" ) #set par(justify: true, leading: 0.65em) #set heading(numbering: "1.") #if "$title$" != "" [ #align(center, text(18pt, weight: "bold")[$title$]) #v(1em) ] #if "$author$" != "" [ #align(center, text(14pt)[$author$]) #v(0.5em) ] #if "$date$" != "" [ #align(center, text(12pt)[$date$]) #v(2em) ] $body$`); // Article template - academic paper style this.builtInTemplates.set('article.typ', `#set page( paper: "a4", margin: (x: 2.5cm, y: 2cm), numbering: "1" ) #set text( font: "Liberation Serif", size: 11pt, lang: "en" ) #set par(justify: true, leading: 0.65em, first-line-indent: 1.2em) #set heading(numbering: "1.") #show heading.where(level: 1): it => block( width: 100%, below: 1.5em, above: 2em, )[ #set align(center) #set text(16pt, weight: "bold") #it.body ] #show heading.where(level: 2): it => block( width: 100%, below: 1em, above: 1.5em, )[ #set text(13pt, weight: "bold") #it ] #if "$title$" != "" [ #align(center, text(20pt, weight: "bold")[$title$]) #v(1em) ] #if "$author$" != "" [ #align(center, text(14pt)[$author$]) #v(0.5em) ] #if "$date$" != "" [ #align(center, text(12pt)[$date$]) #v(2em) ] $body$`); // Report template - formal report style this.builtInTemplates.set('report.typ', `#set page( paper: "a4", margin: (x: 2.5cm, y: 2.5cm), numbering: "1", header: context { if counter(page).get().first() > 1 [ #align(right)[$title$] #line(length: 100%) ] } ) #set text( font: "Liberation Serif", size: 12pt, lang: "en" ) #set par(justify: true, leading: 0.65em) #set heading(numbering: "1.") #show heading.where(level: 1): it => pagebreak(weak: true) + block( width: 100%, below: 2em, above: 0pt, )[ #set align(center) #set text(18pt, weight: "bold") #counter(heading).display() #h(0.5em) #it.body ] #show heading.where(level: 2): it => block( width: 100%, below: 1em, above: 1.5em, )[ #set text(14pt, weight: "bold") #it ] // Title page #page()[ #align(center + horizon)[ #if "$title$" != "" [ #text(24pt, weight: "bold")[$title$] #v(2em) ] #if "$author$" != "" [ #text(16pt)[$author$] #v(1em) ] #if "$date$" != "" [ #text(14pt)[$date$] ] ] ] $body$`); // Single-page template - auto-adjusting height this.builtInTemplates.set('single-page.typ', `#set page( paper: "a4", margin: (x: 2.5cm, y: 2cm), height: auto ) #set text( font: "Liberation Serif", size: 12pt, lang: "en" ) #set par(justify: true, leading: 0.65em) #set heading(numbering: "1.") #show heading.where(level: 1): it => block( width: 100%, below: 1.5em, above: 1.5em, )[ #set text(16pt, weight: "bold") #it ] #show heading.where(level: 2): it => block( width: 100%, below: 1em, above: 1em, )[ #set text(14pt, weight: "bold") #it ] #if "$title$" != "" [ #align(center, text(18pt, weight: "bold")[$title$]) #v(1em) ] #if "$author$" != "" [ #align(center, text(14pt)[$author$]) #v(0.5em) ] #if "$date$" != "" [ #align(center, text(12pt)[$date$]) #v(2em) ] $body$`); } /** * Get available template names */ getAvailableTemplates(): string[] { const builtIn = Array.from(this.builtInTemplates.keys()); const custom = Array.from(this.customTemplates.keys()).map(name => `${name}.typ`); return [...builtIn, ...custom]; } /** * Get template content by name */ getTemplate(templateName: string): string | null { // Check built-in templates first if (this.builtInTemplates.has(templateName)) { return this.builtInTemplates.get(templateName) || null; } // Check custom templates const customKey = templateName.replace('.typ', ''); if (this.customTemplates.has(customKey)) { const template = this.customTemplates.get(customKey); // For custom templates, we'll need to load the file content // This will be implemented in the next subtask return template?.filePath || null; } return null; } /** * Check if a template exists */ hasTemplate(templateName: string): boolean { return this.builtInTemplates.has(templateName) || this.customTemplates.has(templateName.replace('.typ', '')); } /** * Get template metadata */ getTemplateInfo(templateName: string): Partial