fixed escaping of formatting characters

see #757
This commit is contained in:
Bernhard B 2025-11-01 23:31:45 +01:00
parent 08cd2bd12c
commit d6488d6c8e
2 changed files with 21 additions and 6 deletions

View File

@ -141,12 +141,14 @@ func (l *TextstyleParser) Parse() (string, []string) {
l.next()
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "**"
continue
}
l.handleToken(BoldBegin, Bold)
} else { //Italic
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "*"
continue
}
l.handleToken(ItalicBegin, Italic)
@ -155,18 +157,21 @@ func (l *TextstyleParser) Parse() (string, []string) {
l.next()
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "||"
continue
}
l.handleToken(SpoilerBegin, Spoiler)
} else if c == '~' {
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "~"
continue
}
l.handleToken(StrikethroughBegin, Strikethrough)
} else if c == '`' {
if prevChar == EscapeCharacter {
prevChar = c
l.fullString += "`"
continue
}
l.handleToken(MonoSpaceBegin, Monospace)

View File

@ -1,7 +1,9 @@
package utils
import "testing"
import "reflect"
import (
"reflect"
"testing"
)
func expectMessageEqual(t *testing.T, message1 string, message2 string) {
if message1 != message2 {
@ -123,27 +125,35 @@ func TestBoldTextInsideSpoiler(t *testing.T) {
func TestEscapeAsterisks(t *testing.T) {
textstyleParser := NewTextstyleParser("\\*escaped text\\*")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "escaped text")
expectMessageEqual(t, message, "*escaped text*")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestEscapeAsterisks1(t *testing.T) {
textstyleParser := NewTextstyleParser("\\**escaped text\\**")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "escaped text")
expectMessageEqual(t, message, "**escaped text**")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestEscapeBackticks(t *testing.T) {
textstyleParser := NewTextstyleParser("\\`escaped text\\`")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "escaped text")
expectMessageEqual(t, message, "`escaped text`")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestEscapeTilde(t *testing.T) {
textstyleParser := NewTextstyleParser("\\~escaped text\\~")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "escaped text")
expectMessageEqual(t, message, "~escaped text~")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}
func TestEscapeNew(t *testing.T) {
textstyleParser := NewTextstyleParser("Test \\** \\* \\~ Escape")
message, signalCliFormatStrings := textstyleParser.Parse()
expectMessageEqual(t, message, "Test ** * ~ Escape")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
}