formatter.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // mautrix-discord - A Matrix-Discord puppeting bridge.
  2. // Copyright (C) 2023 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/extension"
  23. "github.com/yuin/goldmark/parser"
  24. "github.com/yuin/goldmark/util"
  25. "maunium.net/go/mautrix/event"
  26. "maunium.net/go/mautrix/format"
  27. "maunium.net/go/mautrix/format/mdext"
  28. "maunium.net/go/mautrix/id"
  29. "maunium.net/go/mautrix/util/variationselector"
  30. )
  31. var discordExtensions = goldmark.WithExtensions(extension.Strikethrough, mdext.SimpleSpoiler, mdext.DiscordUnderline, &DiscordEveryone{})
  32. var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`)
  33. // indentableParagraphParser is the default paragraph parser with CanAcceptIndentedLine.
  34. // Used when disabling CodeBlockParser (as disabling it without a replacement will make indented blocks disappear).
  35. type indentableParagraphParser struct {
  36. parser.BlockParser
  37. }
  38. var defaultIndentableParagraphParser = &indentableParagraphParser{BlockParser: parser.NewParagraphParser()}
  39. func (b *indentableParagraphParser) CanAcceptIndentedLine() bool {
  40. return true
  41. }
  42. func (portal *Portal) renderDiscordMarkdownOnlyHTML(text string) string {
  43. text = escapeFixer.ReplaceAllStringFunc(text, func(s string) string {
  44. return s[:2] + `\` + s[2:]
  45. })
  46. mdRenderer := goldmark.New(
  47. goldmark.WithParser(mdext.ParserWithoutFeatures(
  48. parser.NewListParser(), parser.NewListItemParser(), parser.NewHTMLBlockParser(), parser.NewRawHTMLParser(),
  49. parser.NewSetextHeadingParser(), parser.NewATXHeadingParser(), parser.NewThematicBreakParser(),
  50. parser.NewLinkParser(), parser.NewCodeBlockParser(),
  51. )),
  52. goldmark.WithParserOptions(parser.WithBlockParsers(util.Prioritized(defaultIndentableParagraphParser, 500))),
  53. format.HTMLOptions, discordExtensions,
  54. goldmark.WithExtensions(&DiscordTag{portal}),
  55. )
  56. var buf strings.Builder
  57. err := mdRenderer.Convert([]byte(text), &buf)
  58. if err != nil {
  59. panic(fmt.Errorf("markdown parser errored: %w", err))
  60. }
  61. return format.UnwrapSingleParagraph(buf.String())
  62. }
  63. const formatterContextUserKey = "fi.mau.discord.user"
  64. const formatterContextPortalKey = "fi.mau.discord.portal"
  65. func pillConverter(displayname, mxid, eventID string, ctx format.Context) string {
  66. if len(mxid) == 0 {
  67. return displayname
  68. }
  69. user := ctx.ReturnData[formatterContextUserKey].(*User)
  70. if mxid[0] == '#' {
  71. alias, err := user.bridge.Bot.ResolveAlias(id.RoomAlias(mxid))
  72. if err != nil {
  73. return displayname
  74. }
  75. mxid = alias.RoomID.String()
  76. }
  77. if mxid[0] == '!' {
  78. portal := user.bridge.GetPortalByMXID(id.RoomID(mxid))
  79. if portal != nil {
  80. if eventID == "" {
  81. //currentPortal := ctx[formatterContextPortalKey].(*Portal)
  82. return fmt.Sprintf("<#%s>", portal.Key.ChannelID)
  83. //if currentPortal.GuildID == portal.GuildID {
  84. //} else if portal.GuildID != "" {
  85. // return fmt.Sprintf("<#%s:%s:%s>", portal.Key.ChannelID, portal.GuildID, portal.Name)
  86. //} else {
  87. // // TODO is mentioning private channels possible at all?
  88. //}
  89. } else if msg := user.bridge.DB.Message.GetByMXID(portal.Key, id.EventID(eventID)); msg != nil {
  90. guildID := portal.GuildID
  91. if guildID == "" {
  92. guildID = "@me"
  93. }
  94. return fmt.Sprintf("https://discord.com/channels/%s/%s/%s", guildID, msg.DiscordProtoChannelID(), msg.DiscordID)
  95. }
  96. }
  97. } else if mxid[0] == '@' {
  98. parsedID, ok := user.bridge.ParsePuppetMXID(id.UserID(mxid))
  99. if ok {
  100. return fmt.Sprintf("<@%s>", parsedID)
  101. }
  102. mentionedUser := user.bridge.GetUserByMXID(id.UserID(mxid))
  103. if mentionedUser != nil && mentionedUser.DiscordID != "" {
  104. return fmt.Sprintf("<@%s>", mentionedUser.DiscordID)
  105. }
  106. }
  107. return displayname
  108. }
  109. // Discord links start with http:// or https://, contain at least two characters afterwards,
  110. // don't contain < or whitespace anywhere, and don't end with "'),.:;]
  111. //
  112. // Zero-width whitespace is mostly in the Format category and is allowed, except \uFEFF isn't for some reason
  113. var discordLinkRegex = regexp.MustCompile(`https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]`)
  114. var discordMarkdownEscaper = strings.NewReplacer(
  115. `\`, `\\`,
  116. `_`, `\_`,
  117. `*`, `\*`,
  118. `~`, `\~`,
  119. "`", "\\`",
  120. `|`, `\|`,
  121. `<`, `\<`,
  122. )
  123. func escapeDiscordMarkdown(s string) string {
  124. submatches := discordLinkRegex.FindAllStringIndex(s, -1)
  125. if submatches == nil {
  126. return discordMarkdownEscaper.Replace(s)
  127. }
  128. var builder strings.Builder
  129. offset := 0
  130. for _, match := range submatches {
  131. start := match[0]
  132. end := match[1]
  133. builder.WriteString(discordMarkdownEscaper.Replace(s[offset:start]))
  134. builder.WriteString(s[start:end])
  135. offset = end
  136. }
  137. builder.WriteString(discordMarkdownEscaper.Replace(s[offset:]))
  138. return builder.String()
  139. }
  140. var matrixHTMLParser = &format.HTMLParser{
  141. TabsToSpaces: 4,
  142. Newline: "\n",
  143. HorizontalLine: "\n---\n",
  144. ItalicConverter: func(s string, ctx format.Context) string {
  145. return fmt.Sprintf("*%s*", s)
  146. },
  147. UnderlineConverter: func(s string, ctx format.Context) string {
  148. return fmt.Sprintf("__%s__", s)
  149. },
  150. TextConverter: func(s string, ctx format.Context) string {
  151. if ctx.TagStack.Has("pre") || ctx.TagStack.Has("code") {
  152. // If we're in a code block, don't escape markdown
  153. return s
  154. }
  155. return escapeDiscordMarkdown(s)
  156. },
  157. SpoilerConverter: func(text, reason string, ctx format.Context) string {
  158. if reason != "" {
  159. return fmt.Sprintf("(%s) ||%s||", reason, text)
  160. }
  161. return fmt.Sprintf("||%s||", text)
  162. },
  163. }
  164. func init() {
  165. matrixHTMLParser.PillConverter = pillConverter
  166. }
  167. func (portal *Portal) parseMatrixHTML(user *User, content *event.MessageEventContent) string {
  168. if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
  169. ctx := format.NewContext()
  170. ctx.ReturnData[formatterContextUserKey] = user
  171. ctx.ReturnData[formatterContextPortalKey] = portal
  172. return variationselector.Remove(matrixHTMLParser.Parse(content.FormattedBody, ctx))
  173. } else {
  174. return variationselector.Remove(escapeDiscordMarkdown(content.Body))
  175. }
  176. }