|
| 1 | +--- |
| 2 | +status: planned |
| 3 | +created: '2025-11-03' |
| 4 | +tags: [bug, templates, frontmatter, enhancement] |
| 5 | +priority: high |
| 6 | +--- |
| 7 | + |
| 8 | +# Template Variable Synchronization |
| 9 | + |
| 10 | +> **Status**: 📅 Planned · **Priority**: Medium · **Created**: 2025-11-03 |
| 11 | +
|
| 12 | +**Project**: lean-spec |
| 13 | +**Team**: Core Development |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +When creating a spec with `--field priority=high`, the frontmatter is correctly updated but the template body still shows hardcoded values. For example: |
| 18 | + |
| 19 | +```markdown |
| 20 | +--- |
| 21 | +priority: high # ✓ Correct (from --field) |
| 22 | +--- |
| 23 | + |
| 24 | +> **Priority**: Medium # ✗ Wrong (hardcoded in template) |
| 25 | +``` |
| 26 | + |
| 27 | +**Why this matters:** |
| 28 | +- Inconsistent data between frontmatter and body content |
| 29 | +- Users expect `--field` values to populate throughout the spec |
| 30 | +- Manual editing required to fix mismatches |
| 31 | +- Affects `priority`, `status`, and potentially other fields |
| 32 | + |
| 33 | +**Root cause:** Templates have hardcoded values in body content instead of using variable placeholders like `{priority}`, `{status}`, etc. |
| 34 | + |
| 35 | +## Design |
| 36 | + |
| 37 | +### Solution 1: Add Frontmatter Variables to Variable Resolver |
| 38 | + |
| 39 | +Extend `variable-resolver.ts` to support frontmatter field variables: |
| 40 | + |
| 41 | +```typescript |
| 42 | +export interface VariableContext { |
| 43 | + name?: string; |
| 44 | + date?: string; |
| 45 | + projectName?: string; |
| 46 | + gitInfo?: GitInfo; |
| 47 | + customVariables?: Record<string, string>; |
| 48 | + frontmatter?: Record<string, unknown>; // NEW |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +**Variable resolution flow:** |
| 53 | +1. Parse template to extract frontmatter |
| 54 | +2. Merge frontmatter fields into variable context |
| 55 | +3. Resolve variables in body (including `{priority}`, `{status}`, etc.) |
| 56 | +4. Support nested fields like `{tags}` (array → comma-separated string) |
| 57 | + |
| 58 | +**Template update:** |
| 59 | +```markdown |
| 60 | +--- |
| 61 | +status: planned |
| 62 | +priority: medium |
| 63 | +tags: [] |
| 64 | +--- |
| 65 | + |
| 66 | +# {name} |
| 67 | + |
| 68 | +> **Status**: {status} · **Priority**: {priority} · **Created**: {date} |
| 69 | +``` |
| 70 | + |
| 71 | +### Solution 2: Post-Process After Frontmatter Update |
| 72 | + |
| 73 | +Alternative approach - update body content after frontmatter is modified: |
| 74 | + |
| 75 | +```typescript |
| 76 | +// In create.ts, after updating frontmatter: |
| 77 | +if (options.priority) { |
| 78 | + parsed.data.priority = options.priority; |
| 79 | + // Also update body content |
| 80 | + content = content.replace(/Priority[:\s]+\w+/gi, `Priority: ${capitalize(options.priority)}`); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +**Pros**: Simple, no template changes needed |
| 85 | +**Cons**: Fragile (regex-based), doesn't handle all cases |
| 86 | + |
| 87 | +### Recommended: Solution 1 |
| 88 | + |
| 89 | +More robust, consistent with existing variable system, enables richer templates. |
| 90 | + |
| 91 | +## Plan |
| 92 | + |
| 93 | +- [ ] Extend `VariableContext` to include frontmatter fields |
| 94 | +- [ ] Update `buildVariableContext()` to parse frontmatter from template |
| 95 | +- [ ] Update `resolveVariables()` to handle frontmatter field variables |
| 96 | +- [ ] Add special handling for arrays (e.g., `{tags}` → comma-separated) |
| 97 | +- [ ] Add special handling for status/priority display formatting |
| 98 | +- [ ] Update all templates to use variables instead of hardcoded values |
| 99 | + - [ ] `templates/standard/spec-template.md` |
| 100 | + - [ ] `templates/minimal/spec-template.md` |
| 101 | + - [ ] `templates/enterprise/spec-template.md` |
| 102 | + - [ ] `.lspec/templates/spec-template.md` (if exists) |
| 103 | +- [ ] Update docs to document available frontmatter variables |
| 104 | +- [ ] Add tests for frontmatter variable resolution |
| 105 | +- [ ] Test with various field types (string, number, boolean, array) |
| 106 | + |
| 107 | +## Test |
| 108 | + |
| 109 | +### Variable Resolution |
| 110 | +- [ ] `{priority}` resolves to frontmatter priority value |
| 111 | +- [ ] `{status}` resolves to frontmatter status value |
| 112 | +- [ ] `{tags}` resolves to comma-separated tag list |
| 113 | +- [ ] Custom fields like `{epic}` resolve correctly |
| 114 | +- [ ] Missing fields don't break template (empty string or default) |
| 115 | + |
| 116 | +### Integration |
| 117 | +- [ ] `lspec create test --field priority=high` shows "Priority: high" in body |
| 118 | +- [ ] `lspec create test --field status=in-progress` shows correct status |
| 119 | +- [ ] Array fields like tags display correctly |
| 120 | +- [ ] Existing specs without variables still work |
| 121 | + |
| 122 | +### Edge Cases |
| 123 | +- [ ] Boolean fields (true/false) resolve as strings |
| 124 | +- [ ] Number fields resolve as strings |
| 125 | +- [ ] Nested objects are handled gracefully |
| 126 | +- [ ] Variables in frontmatter values don't cause recursion |
| 127 | + |
| 128 | +## Notes |
| 129 | + |
| 130 | +**Complexity**: Medium - requires careful handling of frontmatter parsing order |
| 131 | + |
| 132 | +**Workaround (current)**: Users can manually edit the body after creation, or update templates locally to match their needs |
| 133 | + |
| 134 | +**Alternative considered**: Use a more sophisticated template engine (Handlebars, Nunjucks), but that adds dependencies and complexity for a simple use case |
| 135 | + |
| 136 | +**Timeline**: Should be implemented before v1.0 release to avoid breaking template changes later |
0 commit comments