added strikethrough and monspace text styling options

see #382
This commit is contained in:
Bernhard B
2023-09-23 18:57:32 +02:00
parent 35e2e903df
commit 221e129ad4
2 changed files with 49 additions and 11 deletions

View File

@@ -8,6 +8,8 @@ const (
Normal string = "NORMAL" Normal string = "NORMAL"
Bold = "BOLD" Bold = "BOLD"
Italic = "ITALIC" Italic = "ITALIC"
Monospace = "MONOSPACE"
Strikethrough = "STRIKETHROUGH"
) )
const ( const (
@@ -17,13 +19,17 @@ const (
BoldBegin = 3 BoldBegin = 3
BoldEnd1 = 4 BoldEnd1 = 4
BoldEnd2 = 5 BoldEnd2 = 5
MonoSpaceBegin = 6
MonoSpaceEnd = 7
StrikethroughBegin = 8
StrikethroughEnd = 9
) )
func ParseMarkdownMessage(message string) (string, []string) { func ParseMarkdownMessage(message string) (string, []string) {
textFormat := Normal textFormat := Normal
textFormatBegin := 0 textFormatBegin := 0
textFormatLength := 0 textFormatLength := 0
numOfAsterisks := 0 numOfControlChars := 0
state := None state := None
signalCliFormatStrings := []string{} signalCliFormatStrings := []string{}
fullString := "" fullString := ""
@@ -37,7 +43,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
if lastChar == "*" { if lastChar == "*" {
state = BoldBegin state = BoldBegin
textFormat = Bold textFormat = Bold
textFormatBegin = i - numOfAsterisks textFormatBegin = i - numOfControlChars
textFormatLength = 0 textFormatLength = 0
} else { } else {
state = ItalicEnd state = ItalicEnd
@@ -45,21 +51,41 @@ func ParseMarkdownMessage(message string) (string, []string) {
} else if state == None { } else if state == None {
state = ItalicBegin state = ItalicBegin
textFormat = Italic textFormat = Italic
textFormatBegin = i - numOfAsterisks textFormatBegin = i - numOfControlChars
textFormatLength = 0 textFormatLength = 0
} else if state == BoldBegin { } else if state == BoldBegin {
state = BoldEnd1 state = BoldEnd1
} else if state == BoldEnd1 { } else if state == BoldEnd1 {
state = BoldEnd2 state = BoldEnd2
} }
numOfAsterisks += 1 numOfControlChars += 1
} else if v == '`' {
if state == None {
state = MonoSpaceBegin
textFormat = Monospace
textFormatBegin = i - numOfControlChars
textFormatLength = 0
} else if state == MonoSpaceBegin {
state = MonoSpaceEnd
}
numOfControlChars += 1
} else if v == '~' {
if state == None {
state = StrikethroughBegin
textFormat = Strikethrough
textFormatBegin = i - numOfControlChars
textFormatLength = 0
} else if state == StrikethroughBegin {
state = StrikethroughEnd
}
numOfControlChars += 1
} else { } else {
textFormatLength += 1 textFormatLength += 1
fullString += string(v) fullString += string(v)
} }
lastChar = string(v) lastChar = string(v)
if state == ItalicEnd || state == BoldEnd2 { if state == ItalicEnd || state == BoldEnd2 || state == MonoSpaceEnd || state == StrikethroughEnd {
signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength)+":"+textFormat) signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength)+":"+textFormat)
state = None state = None
textFormatBegin = 0 textFormatBegin = 0

View File

@@ -38,3 +38,15 @@ func TestTwoBoldFormattedStrings(t *testing.T) {
expectMessageEqual(t, message, "This is a bold and another bold message") expectMessageEqual(t, message, "This is a bold and another bold message")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:4:BOLD", "27:4:BOLD"}) expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:4:BOLD", "27:4:BOLD"})
} }
func TestStrikethrough(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("This is a ~strikethrough~ and a **bold** message")
expectMessageEqual(t, message, "This is a strikethrough and a bold message")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:13:STRIKETHROUGH", "30:4:BOLD"})
}
func TestMonospace(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("This is a `monospace` and a **bold** message")
expectMessageEqual(t, message, "This is a monospace and a bold message")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:9:MONOSPACE", "26:4:BOLD"})
}