mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 23:54:22 +01:00
@@ -8,22 +8,28 @@ const (
|
|||||||
Normal string = "NORMAL"
|
Normal string = "NORMAL"
|
||||||
Bold = "BOLD"
|
Bold = "BOLD"
|
||||||
Italic = "ITALIC"
|
Italic = "ITALIC"
|
||||||
|
Monospace = "MONOSPACE"
|
||||||
|
Strikethrough = "STRIKETHROUGH"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
None int = 0
|
None int = 0
|
||||||
ItalicBegin = 1
|
ItalicBegin = 1
|
||||||
ItalicEnd = 2
|
ItalicEnd = 2
|
||||||
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
|
||||||
|
|||||||
@@ -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"})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user