fixed bug in textstyleparser

* the textstyleparser now properly handles `***` and marks the
  text as bold and italic.

see #879
This commit is contained in:
Bernhard B 2026-08-16 22:51:31 +02:00
parent 5cc379fde0
commit a08db23a31
2 changed files with 35 additions and 8 deletions

View File

@ -23,6 +23,7 @@ const (
MonoSpaceBegin = 6
StrikethroughBegin = 8
SpoilerBegin = 9
BoldItalicBegin = 10
)
const EscapeCharacter rune = '\\'
@ -119,7 +120,14 @@ func (l *TextstyleParser) handleToken(tokenType int, signalCliStylingType string
} else {
if l.tokens.Peek().Token == tokenType {
tokenBeginState := l.tokens.Pop()
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(getUtf16StringLength(l.fullString)-tokenBeginState.BeginPos)+":"+signalCliStylingType)
length := getUtf16StringLength(l.fullString) - tokenBeginState.BeginPos
if tokenType == BoldItalicBegin {
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+Bold)
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+Italic)
} else {
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+signalCliStylingType)
}
} else {
l.tokens.Push(TokenState{BeginPos: getUtf16StringLength(l.fullString), Token: tokenType})
}
@ -137,14 +145,27 @@ func (l *TextstyleParser) Parse() (string, []string) {
nextRune := l.peek()
if c == '*' {
if nextRune == '*' { //Bold
if nextRune == '*' {
l.next()
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "**"
continue
if l.peek() == '*' { // Check for *** before treating it as **
l.next()
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "***"
continue
}
l.handleToken(BoldItalicBegin, "BOLD_ITALIC")
} else {
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "**"
continue
}
l.handleToken(BoldBegin, Bold)
}
l.handleToken(BoldBegin, Bold)
} else { //Italic
if prevChar == EscapeCharacter {
prevChar = c

View File

@ -155,5 +155,11 @@ func TestEscapeNew(t *testing.T) {
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "Test ** * ~ Escape")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestBoldItalic(t *testing.T) {
textstyleParser := NewTextstyleParser("***Bold Italic Text***")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "Bold Italic Text")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:16:BOLD", "0:16:ITALIC"})
}