From 08da5526d82bce40c85b1d3e93b5e04063bfd201 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Sun, 17 Sep 2023 19:57:11 +1000 Subject: [PATCH] Fixed to use best practices for middle of word emphasis Per https://www.markdownguide.org/basic-syntax#emphasis --- mm-go-irckit/userbridge.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/mm-go-irckit/userbridge.go b/mm-go-irckit/userbridge.go index 5f354a02..922ef355 100644 --- a/mm-go-irckit/userbridge.go +++ b/mm-go-irckit/userbridge.go @@ -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 }