Skip to content

Commit

Permalink
add regex parsing meta formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JJTech0130 committed Jul 30, 2024
1 parent 56b475a commit d4cea85
Showing 1 changed file with 58 additions and 41 deletions.
99 changes: 58 additions & 41 deletions pkg/connector/msgconv/mentions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package msgconv

import (
"context"
"regexp"
"slices"

//"log"
Expand All @@ -42,6 +43,14 @@ func (u UTF16String) String() string {
return string(utf16.Decode(u))
}

var (
META_BOLD_REGEX = regexp.MustCompile(`\*([^*]+)\*`)
META_ITALIC_REGEX = regexp.MustCompile(`_([^_]+)_`)
META_STRIKE_REGEX = regexp.MustCompile(`~([^~]+)~`)
META_MONOSPACE_REGEX = regexp.MustCompile("`([^`]+)`")
META_MONOSPACE_BLOCK_REGEX = regexp.MustCompile("```([^`]+)```")
)

func (mc *MessageConverter) metaToMatrixText(ctx context.Context, text string, rawMentions *socket.MentionData, portal *bridgev2.Portal) (content *event.MessageEventContent) {
content = &event.MessageEventContent{
MsgType: event.MsgText,
Expand All @@ -52,53 +61,61 @@ func (mc *MessageConverter) metaToMatrixText(ctx context.Context, text string, r
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to parse mentions")
}
if mentions == nil {
return
}
utf16Text := NewUTF16String(text)
prevEnd := 0
var output strings.Builder
for _, mention := range mentions {
if mention.Offset < prevEnd {
zerolog.Ctx(ctx).Warn().Msg("Ignoring overlapping mentions in message")
continue
} else if mention.Offset >= len(utf16Text) {
zerolog.Ctx(ctx).Warn().Msg("Ignoring mention outside of message")
continue
}
end := mention.Offset + mention.Length
if end > len(utf16Text) {
end = len(utf16Text)
}
var mentionLink string
switch mention.Type {
case socket.MentionTypePerson:
info, err := mc.getBasicUserInfo(ctx, portal, ids.MakeUserID(mention.ID))
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to get user info for mention")

outputString := text

if mentions != nil {
utf16Text := NewUTF16String(text)
prevEnd := 0
var output strings.Builder
for _, mention := range mentions {
if mention.Offset < prevEnd {
zerolog.Ctx(ctx).Warn().Msg("Ignoring overlapping mentions in message")
continue
} else if mention.Offset >= len(utf16Text) {
zerolog.Ctx(ctx).Warn().Msg("Ignoring mention outside of message")
continue
}
if !slices.Contains(content.Mentions.UserIDs, info.MXID) {
content.Mentions.UserIDs = append(content.Mentions.UserIDs, info.MXID)
end := mention.Offset + mention.Length
if end > len(utf16Text) {
end = len(utf16Text)
}
mentionLink = info.MXID.URI().MatrixToURL()
case socket.MentionTypeThread:
// TODO: how does one send thread mentions?
}
if mentionLink == "" {
continue
var mentionLink string
switch mention.Type {
case socket.MentionTypePerson:
info, err := mc.getBasicUserInfo(ctx, portal, ids.MakeUserID(mention.ID))
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to get user info for mention")
continue
}
if !slices.Contains(content.Mentions.UserIDs, info.MXID) {
content.Mentions.UserIDs = append(content.Mentions.UserIDs, info.MXID)
}
mentionLink = info.MXID.URI().MatrixToURL()
case socket.MentionTypeThread:
// TODO: how does one send thread mentions?
}
if mentionLink == "" {
continue
}

output.WriteString(utf16Text[prevEnd:mention.Offset].String() + `<a href="` + mentionLink + `">` + utf16Text[mention.Offset:end].String() + `</a>`)
prevEnd = end
}
output.WriteString(utf16Text[prevEnd:mention.Offset].String())
output.WriteString(`<a href="`)
output.WriteString(mentionLink)
output.WriteString(`">`)
output.WriteString(utf16Text[mention.Offset:end].String())
output.WriteString(`</a>`)
prevEnd = end
output.WriteString(utf16Text[prevEnd:].String())

outputString = output.String()
}
output.WriteString(utf16Text[prevEnd:].String())

// Second parsing pass, replacing other formatting:
outputString = META_BOLD_REGEX.ReplaceAllString(outputString, "<strong>$1</strong>")
outputString = META_ITALIC_REGEX.ReplaceAllString(outputString, "<em>$1</em>")
outputString = META_STRIKE_REGEX.ReplaceAllString(outputString, "<del>$1</del>")
outputString = META_MONOSPACE_REGEX.ReplaceAllString(outputString, "<code>$1</code>")
outputString = META_MONOSPACE_BLOCK_REGEX.ReplaceAllString(outputString, "<pre>$1</pre>")

content.Format = event.FormatHTML
content.FormattedBody = output.String()
content.FormattedBody = outputString

log := zerolog.Ctx(ctx)
log.Debug().Str("text", text).Str("formatted_body", content.FormattedBody).Msg("Converted message to Matrix text")
Expand Down

0 comments on commit d4cea85

Please sign in to comment.