Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Mattermost markdown formatting / emphasis to IRC #547

Merged
merged 6 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -306,6 +311,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 @@ -1174,3 +1183,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
}
Loading