Skip to content

Commit bdf4489

Browse files
almas-xmaimaiti
andauthored
fix: resolve color tag parsing issue in #52 (PR #98)
Co-authored-by: maimaiti <maimaiti@archeros.com>
1 parent 4bcc6db commit bdf4489

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

color_tag.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (tp *TagParser) Parse(str string) string {
347347

348348
// item: 0 full text 1 tag name 2 tag content
349349
for _, item := range matched {
350-
full, tag, body := item[0], item[1], item[2]
350+
full, tag, body := repairMatchedTag(item[0], item[1], item[2])
351351

352352
// use defined color tag name: "<info>content</>" -> tag: "info"
353353
if !strings.ContainsRune(tag, '=') {
@@ -371,6 +371,19 @@ func (tp *TagParser) Parse(str string) string {
371371
return str
372372
}
373373

374+
func repairMatchedTag(full, tag, body string) (string, string, string) {
375+
if len(colorTags[tag]) == 0 && len(namedRgbMap[tag]) == 0 && len(ParseCodeFromAttr(tag)) == 0 {
376+
if matched := matchRegex.FindAllStringSubmatch(strings.TrimPrefix(full, "<"+tag+">"), -1); len(matched) > 0 {
377+
full = matched[0][0]
378+
tag = matched[0][1]
379+
body = matched[0][2]
380+
return repairMatchedTag(full, tag, body)
381+
}
382+
}
383+
384+
return full, tag, body
385+
}
386+
374387
// ReplaceTag parse string, replace color tag and return rendered string
375388
func ReplaceTag(str string) string {
376389
return tagParser.ParseByEnv(str)

0 commit comments

Comments
 (0)