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,22 +8,28 @@ const (
Normal string = "NORMAL"
Bold = "BOLD"
Italic = "ITALIC"
Monospace = "MONOSPACE"
Strikethrough = "STRIKETHROUGH"
)
const (
None int = 0
ItalicBegin = 1
ItalicEnd = 2
BoldBegin = 3
BoldEnd1 = 4
BoldEnd2 = 5
None int = 0
ItalicBegin = 1
ItalicEnd = 2
BoldBegin = 3
BoldEnd1 = 4
BoldEnd2 = 5
MonoSpaceBegin = 6
MonoSpaceEnd = 7
StrikethroughBegin = 8
StrikethroughEnd = 9
)
func ParseMarkdownMessage(message string) (string, []string) {
textFormat := Normal
textFormatBegin := 0
textFormatLength := 0
numOfAsterisks := 0
numOfControlChars := 0
state := None
signalCliFormatStrings := []string{}
fullString := ""
@@ -37,7 +43,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
if lastChar == "*" {
state = BoldBegin
textFormat = Bold
textFormatBegin = i - numOfAsterisks
textFormatBegin = i - numOfControlChars
textFormatLength = 0
} else {
state = ItalicEnd
@@ -45,21 +51,41 @@ func ParseMarkdownMessage(message string) (string, []string) {
} else if state == None {
state = ItalicBegin
textFormat = Italic
textFormatBegin = i - numOfAsterisks
textFormatBegin = i - numOfControlChars
textFormatLength = 0
} else if state == BoldBegin {
state = BoldEnd1
} else if state == BoldEnd1 {
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 {
textFormatLength += 1
fullString += 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)
state = None
textFormatBegin = 0

View File

@@ -38,3 +38,15 @@ func TestTwoBoldFormattedStrings(t *testing.T) {
expectMessageEqual(t, message, "This is a bold and another bold message")
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"})
}