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

View File

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