| 64 | * Parse template into frontmatter and body sections |
| 65 | */ |
| 66 | export function parseTemplateSections(templateContent: string): { |
| 67 | frontmatter: string | null; |
| 68 | body: string; |
| 69 | } { |
| 70 | const lines = templateContent.split("\n"); |
| 71 | |
| 72 | // Check if template starts with frontmatter |
| 73 | if (lines[0]?.trim() === "---") { |
| 74 | // Find the closing --- |
| 75 | let endIndex = -1; |
| 76 | for (let i = 1; i < lines.length; i++) { |
| 77 | if (lines[i]?.trim() === "---") { |
| 78 | endIndex = i; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (endIndex > 0) { |
| 84 | // Extract frontmatter content (between the --- lines) |
| 85 | const frontmatterLines = lines.slice(1, endIndex); |
| 86 | const frontmatter = frontmatterLines.join("\n"); |
| 87 | |
| 88 | // Extract body content (after the closing ---) |
| 89 | const bodyLines = lines.slice(endIndex + 1); |
| 90 | const body = bodyLines.join("\n"); |
| 91 | |
| 92 | return { |
| 93 | frontmatter: frontmatter.trim() || null, |
| 94 | body: body, |
| 95 | }; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // No frontmatter found, entire content is body |
| 100 | return { |
| 101 | frontmatter: null, |
| 102 | body: templateContent, |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Process template variables in frontmatter |