Skip to content

Commit

Permalink
added spoiler to textstyleparser
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard B committed Oct 28, 2023
1 parent 54fd4db commit 0cab12c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/utils/textstyleparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
Italic = "ITALIC"
Monospace = "MONOSPACE"
Strikethrough = "STRIKETHROUGH"
Spoiler = "SPOILER"
)

const (
Expand All @@ -23,6 +24,10 @@ const (
MonoSpaceEnd = 7
StrikethroughBegin = 8
StrikethroughEnd = 9
SpoilerBegin1 = 10
SpoilerBegin = 11
SpoilerEnd1 = 12
SpoilerEnd2 = 13
)

func getUtf16CharacterCount(s string) int {
Expand Down Expand Up @@ -76,6 +81,20 @@ func ParseMarkdownMessage(message string) (string, []string) {
state = BoldEnd2
}
numOfControlChars += 1
} else if v == '|' {
if state == None {
state = SpoilerBegin1
} else if state == SpoilerBegin1 && lastChar == "|" {
state = SpoilerBegin
textFormat = Spoiler
textFormatBegin = i - numOfControlChars + additionalCharacterCount
textFormatLength = 0
} else if state == SpoilerBegin {
state = SpoilerEnd1
} else if state == SpoilerEnd1 && lastChar == "|" {
state = SpoilerEnd2
}
numOfControlChars += 1
} else if v == '`' {
if state == None {
state = MonoSpaceBegin
Expand Down Expand Up @@ -103,7 +122,7 @@ func ParseMarkdownMessage(message string) (string, []string) {
}
lastChar = string(v)

if state == ItalicEnd || state == BoldEnd2 || state == MonoSpaceEnd || state == StrikethroughEnd {
if state == ItalicEnd || state == BoldEnd2 || state == MonoSpaceEnd || state == StrikethroughEnd || state == SpoilerEnd2 {
signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength + additionalCharacterCount)+":"+textFormat)
state = None
textFormatBegin = 0
Expand Down
12 changes: 12 additions & 0 deletions src/utils/textstyleparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ func TestMultipleMulticharacterEmojiWithBoldText(t *testing.T) {
expectMessageEqual(t, message, "👋🏾abcdefg")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"4:9:BOLD"})
}

func TestSpoiler(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("||this is a spoiler||")
expectMessageEqual(t, message, "this is a spoiler")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:17:SPOILER"})
}

func TestSpoiler1(t *testing.T) {
message, signalCliFormatStrings := ParseMarkdownMessage("||this is a spoiler|| and another ||spoiler||")
expectMessageEqual(t, message, "this is a spoiler and another spoiler")
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:17:SPOILER", "30:7:SPOILER"})
}

0 comments on commit 0cab12c

Please sign in to comment.