* Apply template to task (both frontmatter and body) if enabled in settings
( taskData: TaskCreationData )
| 287 | * Apply template to task (both frontmatter and body) if enabled in settings |
| 288 | */ |
| 289 | private async applyTemplate( |
| 290 | taskData: TaskCreationData |
| 291 | ): Promise<{ frontmatter: Record<string, unknown>; body: string }> { |
| 292 | const defaults = this.plugin.settings.taskCreationDefaults; |
| 293 | |
| 294 | // Check if body template is enabled and configured |
| 295 | if (!defaults.useBodyTemplate || !defaults.bodyTemplate?.trim()) { |
| 296 | // No template configured, return empty frontmatter and details as body |
| 297 | return { |
| 298 | frontmatter: {}, |
| 299 | body: taskData.details?.trim() || "", |
| 300 | }; |
| 301 | } |
| 302 | |
| 303 | try { |
| 304 | // Normalize the template path and ensure it has .md extension |
| 305 | let templatePath = normalizePath(defaults.bodyTemplate.trim()); |
| 306 | if (!templatePath.endsWith(".md")) { |
| 307 | templatePath += ".md"; |
| 308 | } |
| 309 | |
| 310 | // Try to load the template file |
| 311 | const templateFile = this.plugin.app.vault.getAbstractFileByPath(templatePath); |
| 312 | if (templateFile instanceof TFile) { |
| 313 | const templateContent = await this.plugin.app.vault.read(templateFile); |
| 314 | |
| 315 | // Prepare task data for template variables (with all final values) |
| 316 | const templateTaskData: TemplateData = { |
| 317 | title: taskData.title || "", |
| 318 | priority: taskData.priority || "", |
| 319 | status: taskData.status || "", |
| 320 | contexts: Array.isArray(taskData.contexts) ? taskData.contexts : [], |
| 321 | tags: Array.isArray(taskData.tags) ? taskData.tags : [], |
| 322 | timeEstimate: taskData.timeEstimate || 0, |
| 323 | dueDate: taskData.due || "", |
| 324 | scheduledDate: taskData.scheduled || "", |
| 325 | details: taskData.details || "", |
| 326 | parentNote: taskData.parentNote || "", |
| 327 | }; |
| 328 | |
| 329 | // Process the complete template (frontmatter + body) |
| 330 | return processTemplate(templateContent, templateTaskData); |
| 331 | } else { |
| 332 | // Template file not found, log error and return details as-is |
| 333 | |
| 334 | tasknotesLogger.warn(`Task body template not found: ${templatePath}`, { |
| 335 | category: "persistence", |
| 336 | operation: "task-body-template-not-found", |
| 337 | }); |
| 338 | publishUserNotice( |
| 339 | this.plugin.emitter, |
| 340 | this.translate("services.task.notices.templateNotFound", { path: templatePath }) |
| 341 | ); |
| 342 | return { |
| 343 | frontmatter: {}, |
| 344 | body: taskData.details?.trim() || "", |
| 345 | }; |
| 346 | } |
no test coverage detected