formatter.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // mautrix-discord - A Matrix-Discord puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "regexp"
  20. "strings"
  21. "github.com/yuin/goldmark"
  22. "github.com/yuin/goldmark/parser"
  23. "maunium.net/go/mautrix/event"
  24. "maunium.net/go/mautrix/format"
  25. "maunium.net/go/mautrix/format/mdext"
  26. "maunium.net/go/mautrix/id"
  27. )
  28. var discordExtensions = goldmark.WithExtensions(mdext.SimpleSpoiler, mdext.DiscordUnderline)
  29. var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`)
  30. func (portal *Portal) renderDiscordMarkdown(text string) event.MessageEventContent {
  31. return format.HTMLToContent(portal.renderDiscordMarkdownOnlyHTML(text))
  32. }
  33. func (portal *Portal) renderDiscordMarkdownOnlyHTML(text string) string {
  34. text = escapeFixer.ReplaceAllStringFunc(text, func(s string) string {
  35. return s[:2] + `\` + s[2:]
  36. })
  37. mdRenderer := goldmark.New(
  38. goldmark.WithParser(mdext.ParserWithoutFeatures(
  39. parser.NewListParser(), parser.NewListItemParser(), parser.NewHTMLBlockParser(), parser.NewRawHTMLParser(),
  40. )),
  41. format.Extensions, format.HTMLOptions, discordExtensions,
  42. goldmark.WithExtensions(&DiscordTag{portal}),
  43. )
  44. var buf strings.Builder
  45. err := mdRenderer.Convert([]byte(text), &buf)
  46. if err != nil {
  47. panic(fmt.Errorf("markdown parser errored: %w", err))
  48. }
  49. return format.UnwrapSingleParagraph(buf.String())
  50. }
  51. const formatterContextUserKey = "fi.mau.discord.user"
  52. const formatterContextPortalKey = "fi.mau.discord.portal"
  53. func pillConverter(displayname, mxid, eventID string, ctx format.Context) string {
  54. if len(mxid) == 0 {
  55. return displayname
  56. }
  57. user := ctx.ReturnData[formatterContextUserKey].(*User)
  58. if mxid[0] == '#' {
  59. alias, err := user.bridge.Bot.ResolveAlias(id.RoomAlias(mxid))
  60. if err != nil {
  61. return displayname
  62. }
  63. mxid = alias.RoomID.String()
  64. }
  65. if mxid[0] == '!' {
  66. portal := user.bridge.GetPortalByMXID(id.RoomID(mxid))
  67. if portal != nil {
  68. if eventID == "" {
  69. //currentPortal := ctx[formatterContextPortalKey].(*Portal)
  70. return fmt.Sprintf("<#%s>", portal.Key.ChannelID)
  71. //if currentPortal.GuildID == portal.GuildID {
  72. //} else if portal.GuildID != "" {
  73. // return fmt.Sprintf("<#%s:%s:%s>", portal.Key.ChannelID, portal.GuildID, portal.Name)
  74. //} else {
  75. // // TODO is mentioning private channels possible at all?
  76. //}
  77. } else if msg := user.bridge.DB.Message.GetByMXID(portal.Key, id.EventID(eventID)); msg != nil {
  78. guildID := portal.GuildID
  79. if guildID == "" {
  80. guildID = "@me"
  81. }
  82. return fmt.Sprintf("https://discord.com/channels/%s/%s/%s", guildID, msg.DiscordProtoChannelID(), msg.DiscordID)
  83. }
  84. }
  85. } else if mxid[0] == '@' {
  86. parsedID, ok := user.bridge.ParsePuppetMXID(id.UserID(mxid))
  87. if ok {
  88. return fmt.Sprintf("<@%s>", parsedID)
  89. }
  90. mentionedUser := user.bridge.GetUserByMXID(id.UserID(mxid))
  91. if mentionedUser != nil && mentionedUser.DiscordID != "" {
  92. return fmt.Sprintf("<@%s>", mentionedUser.DiscordID)
  93. }
  94. }
  95. return displayname
  96. }
  97. // Discord links start with http:// or https://, contain at least two characters afterwards,
  98. // don't contain < or whitespace anywhere, and don't end with "'),.:;]
  99. //
  100. // Zero-width whitespace is mostly in the Format category and is allowed, except \uFEFF isn't for some reason
  101. var discordLinkRegex = regexp.MustCompile(`https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]`)
  102. var discordMarkdownEscaper = strings.NewReplacer(
  103. `\`, `\\`,
  104. `_`, `\_`,
  105. `*`, `\*`,
  106. `~`, `\~`,
  107. "`", "\\`",
  108. `|`, `\|`,
  109. `<`, `\<`,
  110. )
  111. func escapeDiscordMarkdown(s string) string {
  112. submatches := discordLinkRegex.FindAllStringIndex(s, -1)
  113. if submatches == nil {
  114. return discordMarkdownEscaper.Replace(s)
  115. }
  116. var builder strings.Builder
  117. offset := 0
  118. for _, match := range submatches {
  119. start := match[0]
  120. end := match[1]
  121. builder.WriteString(discordMarkdownEscaper.Replace(s[offset:start]))
  122. builder.WriteString(s[start:end])
  123. offset = end
  124. }
  125. builder.WriteString(discordMarkdownEscaper.Replace(s[offset:]))
  126. return builder.String()
  127. }
  128. var matrixHTMLParser = &format.HTMLParser{
  129. TabsToSpaces: 4,
  130. Newline: "\n",
  131. HorizontalLine: "\n---\n",
  132. ItalicConverter: func(s string, ctx format.Context) string {
  133. return fmt.Sprintf("*%s*", s)
  134. },
  135. UnderlineConverter: func(s string, ctx format.Context) string {
  136. return fmt.Sprintf("__%s__", s)
  137. },
  138. TextConverter: func(s string, ctx format.Context) string {
  139. if ctx.TagStack.Has("pre") || ctx.TagStack.Has("code") {
  140. // If we're in a code block, don't escape markdown
  141. return s
  142. }
  143. return escapeDiscordMarkdown(s)
  144. },
  145. SpoilerConverter: func(text, reason string, ctx format.Context) string {
  146. if reason != "" {
  147. return fmt.Sprintf("(%s) ||%s||", reason, text)
  148. }
  149. return fmt.Sprintf("||%s||", text)
  150. },
  151. }
  152. func init() {
  153. matrixHTMLParser.PillConverter = pillConverter
  154. }
  155. func (portal *Portal) parseMatrixHTML(user *User, content *event.MessageEventContent) string {
  156. if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
  157. ctx := format.NewContext()
  158. ctx.ReturnData[formatterContextUserKey] = user
  159. ctx.ReturnData[formatterContextPortalKey] = portal
  160. return matrixHTMLParser.Parse(content.FormattedBody, ctx)
  161. } else {
  162. return escapeDiscordMarkdown(content.Body)
  163. }
  164. }