Skip to content

Commit

Permalink
Fixed to use best practices for middle of word emphasis
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Sep 18, 2023
1 parent 16cc1ce commit 08da552
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,39 +1187,52 @@ func (u *User) formatCodeBlockText(text string, prefix string, codeBlockBackTick
// Use static initialisation to optimize.
// Bold & Italic - https://www.markdownguide.org/basic-syntax#bold-and-italic
var boldItalicRegExp = []*regexp.Regexp{
regexp.MustCompile(`(?:\*\*\*)+?(.+?)(?:\*\*\*)+?`),
regexp.MustCompile(`(?:\_\_\_)+?(.+?)(?:\_\_\_)+?`),
regexp.MustCompile(`(?:\_\_\*)+?(.+?)(?:\*\_\_)+?`),
regexp.MustCompile(`(?:\*\*\_)+?(.+?)(?:\_\*\*)+?`),
regexp.MustCompile(`(^|\W)(?:\*\*\*)+?(.+?)(?:\*\*\*)+?(\W|$)`),
regexp.MustCompile(`(^|\W)(?:\_\_\_)+?(.+?)(?:\_\_\_)+?(\W|$)`),
regexp.MustCompile(`(^|\W)(?:\_\_\*)+?(.+?)(?:\*\_\_)+?(\W|$)`),
regexp.MustCompile(`(^|\W)(?:\*\*\_)+?(.+?)(?:\_\*\*)+?(\W|$)`),
}

// Bold - https://www.markdownguide.org/basic-syntax#bold
var boldRegExp = []*regexp.Regexp{
regexp.MustCompile(`(?:\*\*)+?(.+?)(?:\*\*)+?`),
regexp.MustCompile(`(?:\_\_)+?(.+?)(?:\_\_)+?`),
regexp.MustCompile(`(^|\W)(?:\*\*)+?(.+?)(?:\*\*)+?(\W|$)`),
regexp.MustCompile(`(^|\W)(?:\_\_)+?(.+?)(?:\_\_)+?(\W|$)`),
}

// Italic - https://www.markdownguide.org/basic-syntax#italic
var italicRegExp = []*regexp.Regexp{
regexp.MustCompile(`(?:\*)+?(.+?)(?:\*)+?`),
regexp.MustCompile(`(?:\_)+?(.+?)(?:\_)+?`),
regexp.MustCompile(`(^|\W)(?:\*)+?(.+?)(?:\*)+?(\W|$)`),
regexp.MustCompile(`(^|\W)(?:\_)+?(.+?)(?:\_)+?(\W|$)`),
}

// Middle of a word support
var (
subWordBoldItalicRegExp = regexp.MustCompile(`(\w)(?:\*\*\*)+?(.+?)(?:\*\*\*)+?(\w)`)
subWordBoldRegExp = regexp.MustCompile(`(\w)(?:\*\*)+?(.+?)(?:\*\*)+?(\w)`)
subWordItalicRegExp = regexp.MustCompile(`(\w)(?:\*)+?(.+?)(?:\*)+?(\w)`)
)

func markdown2irc(msg string) string {
// Bold & Italic 0x02+0x1d
for _, re := range boldItalicRegExp {
msg = re.ReplaceAllString(msg, "\x02\x1d$1\x1d\x02")
msg = re.ReplaceAllString(msg, "$1\x02\x1d$2\x1d\x02$3")
}
// Middle of a word
msg = subWordBoldItalicRegExp.ReplaceAllString(msg, "$1\x02\x1d$2\x1d\x02$3")

// Bold 0x02
for _, re := range boldRegExp {
msg = re.ReplaceAllString(msg, "\x02$1\x02")
msg = re.ReplaceAllString(msg, "$1\x02$2\x02$3")
}
// Middle of a word
msg = subWordBoldRegExp.ReplaceAllString(msg, "$1\x02$2\x02$3")

// Italic 0x1d
for _, re := range italicRegExp {
msg = re.ReplaceAllString(msg, "\x1d$1\x1d")
msg = re.ReplaceAllString(msg, "$1\x1d$2\x1d$3")
}
// Middle of a word
msg = subWordItalicRegExp.ReplaceAllString(msg, "$1\x1d$2\x1d$3")

return msg
}

0 comments on commit 08da552

Please sign in to comment.