Skip to content

Commit

Permalink
chore: somewhat unfinished fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 28, 2023
1 parent bf9f0cf commit b900c4f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions render/format.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package render

import (
"regexp"
"strings"

"github.com/charmbracelet/log"
"github.com/fatih/color"
"github.com/samber/lo"
)

var variableRegex = regexp.MustCompile(`\{\{(.*)\}\}`)
var underline = color.New(color.Underline).Sprint

// format markdown
Expand All @@ -29,8 +28,10 @@ func format(md string) []string {
res = append(res, color.HiGreenString(line[1:]))
} else if strings.HasPrefix(line, "`") {
// example
line = highLightVariable(line[1 : len(line)-1])
res = append(res, " "+color.CyanString(line))
line = line[1 : len(line)-1]
line = highLightVariable(line)
line = color.CyanString(line)
res = append(res, " "+line)
}
}
res = append(res, "")
Expand All @@ -42,5 +43,16 @@ func format(md string) []string {

// parse an `example`, remove `{{`, `}}` and underline the variable
func highLightVariable(line string) string {
return variableRegex.ReplaceAllString(line, underline("$1"))
lines := strings.Split(line, "}}")
lines = lo.Map(lines, func(line string, _ int) string {
l := strings.Split(line, "{{")
if len(l) > 1 {
l[1] = underline(l[1])
}
return strings.Join(l, "{{")
})
line = strings.Join(lines, "}}")
line = strings.ReplaceAll(line, "{{", "")
line = strings.ReplaceAll(line, "}}", "")
return line
}

0 comments on commit b900c4f

Please # to comment.