wrapExpressionsInTemplateConditionals transforms template conditionals by wrapping expressions in ${{ }}. For example: {{#if github.event.issue.number}} becomes {{#if ${{ github.event.issue.number }} }} {{#elseif github.actor}} becomes {{#elseif ${{ github.actor }} }}
(markdown string)
| 17 | // {{#if github.event.issue.number}} becomes {{#if ${{ github.event.issue.number }} }} |
| 18 | // {{#elseif github.actor}} becomes {{#elseif ${{ github.actor }} }} |
| 19 | func wrapExpressionsInTemplateConditionals(markdown string) string { |
| 20 | templateLog.Print("Wrapping expressions in template conditionals") |
| 21 | |
| 22 | // wrapTagExpr applies the wrapping logic to a single extracted expression and |
| 23 | // returns the full reconstructed tag. re must be the same regex that produced |
| 24 | // match (so FindStringSubmatch reliably extracts capture group 1 = the expression). |
| 25 | // prefix is the canonical opening tag text without the expression |
| 26 | // (e.g. "{{#if " or "{{#elseif "), used to rebuild the output tag. |
| 27 | wrapTagExpr := func(re *regexp.Regexp, match, prefix string) string { |
| 28 | submatches := re.FindStringSubmatch(match) |
| 29 | if len(submatches) < 2 { |
| 30 | return match |
| 31 | } |
| 32 | |
| 33 | expr := strings.TrimSpace(submatches[1]) |
| 34 | |
| 35 | // Empty expressions are treated as false and wrapped as such |
| 36 | if expr == "" { |
| 37 | templateLog.Print("Empty expression detected, wrapping as false") |
| 38 | return prefix + "${{ false }} }}" |
| 39 | } |
| 40 | |
| 41 | // Already wrapped in ${{ ... }} — return as-is |
| 42 | if strings.HasPrefix(expr, "${{") { |
| 43 | templateLog.Print("Expression already wrapped, skipping") |
| 44 | return match |
| 45 | } |
| 46 | |
| 47 | // Environment variable reference (starts with ${) — already evaluated |
| 48 | if strings.HasPrefix(expr, "${") { |
| 49 | templateLog.Print("Environment variable reference detected, skipping wrap") |
| 50 | return match |
| 51 | } |
| 52 | |
| 53 | // Placeholder reference (starts with __) — substituted at runtime |
| 54 | if strings.HasPrefix(expr, "__") { |
| 55 | templateLog.Print("Placeholder reference detected, skipping wrap") |
| 56 | return match |
| 57 | } |
| 58 | |
| 59 | templateLog.Printf("Wrapping expression: %s", expr) |
| 60 | return prefix + "${{ " + expr + " }} }}" |
| 61 | } |
| 62 | |
| 63 | // Process {{#if ...}} tags |
| 64 | result := TemplateIfPattern.ReplaceAllStringFunc(markdown, func(match string) string { |
| 65 | return wrapTagExpr(TemplateIfPattern, match, "{{#if ") |
| 66 | }) |
| 67 | |
| 68 | // Process all elseif variant tags — normalise to canonical {{#elseif ...}} form after wrapping |
| 69 | result = TemplateElseIfPattern.ReplaceAllStringFunc(result, func(match string) string { |
| 70 | submatches := TemplateElseIfPattern.FindStringSubmatch(match) |
| 71 | if len(submatches) < 2 { |
| 72 | return match |
| 73 | } |
| 74 | wrapped := wrapTagExpr(TemplateElseIfPattern, match, "{{#elseif ") |
| 75 | // If the original tag used a non-canonical form (else-if / else_if / without #), |
| 76 | // the prefix replacement above already normalises it to {{#elseif ...}}. |