Skip to content

Commit

Permalink
Convert Mattermost markdown formatting / emphasis to IRC
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Sep 17, 2023
1 parent 83f4f83 commit 7895671
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 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 !codeBlockBackTick && !codeBlockTilde {
text = md2irc(text)
}

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

if !codeBlockBackTick && !codeBlockTilde {
text = md2irc(text)
}

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

return text, codeBlockBackTick, codeBlockTilde, lexer
}

func md2irc(msg string) string {
var re *regexp.Regexp

// Bold 0x02 ** (**text**)
re = regexp.MustCompile(`([\s^]*)(?:(?:\*\*)|(?:\_\_)){1}(\S)`)
msg = re.ReplaceAllString(msg, "$1\x02$2")
re = regexp.MustCompile(`(\S)(?:(?:\*\*)|(?:\_\_)){1}([\s$]*)`)
msg = re.ReplaceAllString(msg, "$1\x02$2")

// Italics 0x1D _ (_text_)
re = regexp.MustCompile(`([\s^]*)(?:[\*\_]{1})(\S)`)
msg = re.ReplaceAllString(msg, "$1\x1d$2")
re = regexp.MustCompile(`(\S)(?:_)([\s$]*)`)
msg = re.ReplaceAllString(msg, "$1\x1d$2")

// Monospace 0x11 ` (`text`)
re = regexp.MustCompile(`([\s^]*)(?:\x60{1})(\S)`)
msg = re.ReplaceAllString(msg, "$1\x11$2")
re = regexp.MustCompile(`(\S)(?:\x60{1})([\s$]*)`)
msg = re.ReplaceAllString(msg, "$1\x11$2")

return msg
}

0 comments on commit 7895671

Please sign in to comment.