Skip to content

Commit

Permalink
Convert Mattermost markdown formatting / emphasis to IRC (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung authored Sep 29, 2023
1 parent 62f6234 commit bbe3e54
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions matterircd.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ ShowMentions = false
# This disables that making them appear as normal PRIVMSGs.
#DisableDefaultMentions = true

# Disable formatting / emphasis of text from Mattermost markdown to IRC (bold & italics)
#DisableIRCEmphasis = true

# Enable syntax highlighting for code blocks.
# Formatter and Style are passed through to the chroma v2 package.
# https://github.com/alecthomas/chroma/blob/master/formatters/tty_indexed.go#L262
Expand Down
49 changes: 49 additions & 0 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"net"
"net/http"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -160,6 +161,10 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) {
continue
}

if !u.v.GetBool(u.br.Protocol()+".disableircemphasis") && !codeBlockBackTick && !codeBlockTilde {
text = markdown2irc(text)
}

if showContext {
text = prefix + text + suffix
}
Expand Down Expand Up @@ -308,6 +313,10 @@ func (u *User) handleChannelMessageEvent(event *bridge.ChannelMessageEvent) {
continue
}

if !u.v.GetBool(u.br.Protocol()+".disableircemphasis") && !codeBlockBackTick && !codeBlockTilde {
text = markdown2irc(text)
}

if showContext {
text = prefix + text + suffix
}
Expand Down Expand Up @@ -1180,3 +1189,43 @@ func (u *User) formatCodeBlockText(text string, prefix string, codeBlockBackTick

return text, codeBlockBackTick, codeBlockTilde, lexer
}

// Use static initialisation to optimize.
// Bold & Italic - https://www.markdownguide.org/basic-syntax#bold-and-italic
var boldItalicRegExp = []*regexp.Regexp{
regexp.MustCompile(`(?:\*\*\*)+?(.+?)(?:\*\*\*)+?`),
regexp.MustCompile(`\b(?:\_\_\_)+?(.+?)(?:\_\_\_)+?\b`),
regexp.MustCompile(`\b(?:\_\_\*)+?(.+?)(?:\*\_\_)+?\b`),
regexp.MustCompile(`\b(?:\*\*\_)+?(.+?)(?:\_\*\*)+?\b`),
}

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

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

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

// Bold 0x02
for _, re := range boldRegExp {
msg = re.ReplaceAllString(msg, "\x02$1\x02")
}

// Italic 0x1d
for _, re := range italicRegExp {
msg = re.ReplaceAllString(msg, "\x1d$1\x1d")
}

return msg
}

0 comments on commit bbe3e54

Please sign in to comment.