formatter.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "github.com/yuin/goldmark"
  7. "maunium.net/go/mautrix/id"
  8. "maunium.net/go/mautrix/event"
  9. "maunium.net/go/mautrix/format"
  10. "maunium.net/go/mautrix/format/mdext"
  11. )
  12. var mdRenderer = goldmark.New(format.Extensions, format.HTMLOptions,
  13. goldmark.WithExtensions(mdext.EscapeHTML, mdext.SimpleSpoiler, mdext.DiscordUnderline))
  14. var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`)
  15. func renderDiscordMarkdown(text string) event.MessageEventContent {
  16. text = escapeFixer.ReplaceAllStringFunc(text, func(s string) string {
  17. return s[:2] + `\` + s[2:]
  18. })
  19. return format.RenderMarkdownCustom(text, mdRenderer)
  20. }
  21. const formatterContextUserKey = "fi.mau.discord.user"
  22. const formatterContextPortalKey = "fi.mau.discord.portal"
  23. func pillConverter(displayname, mxid, eventID string, ctx format.Context) string {
  24. if len(mxid) == 0 {
  25. return displayname
  26. }
  27. user := ctx[formatterContextUserKey].(*User)
  28. if mxid[0] == '#' {
  29. alias, err := user.bridge.Bot.ResolveAlias(id.RoomAlias(mxid))
  30. if err != nil {
  31. return displayname
  32. }
  33. mxid = alias.RoomID.String()
  34. }
  35. if mxid[0] == '!' {
  36. portal := user.bridge.GetPortalByMXID(id.RoomID(mxid))
  37. if portal != nil {
  38. if eventID == "" {
  39. //currentPortal := ctx[formatterContextPortalKey].(*Portal)
  40. return fmt.Sprintf("<#%s>", portal.Key.ChannelID)
  41. //if currentPortal.GuildID == portal.GuildID {
  42. //} else if portal.GuildID != "" {
  43. // return fmt.Sprintf("<#%s:%s:%s>", portal.Key.ChannelID, portal.GuildID, portal.Name)
  44. //} else {
  45. // // TODO is mentioning private channels possible at all?
  46. //}
  47. } else if msg := user.bridge.DB.Message.GetByMXID(portal.Key, id.EventID(eventID)); msg != nil {
  48. guildID := portal.GuildID
  49. if guildID == "" {
  50. guildID = "@me"
  51. }
  52. return fmt.Sprintf("https://discord.com/channels/%s/%s/%s", guildID, msg.DiscordProtoChannelID(), msg.DiscordID)
  53. }
  54. }
  55. } else if mxid[0] == '@' {
  56. parsedID, ok := user.bridge.ParsePuppetMXID(id.UserID(mxid))
  57. if ok {
  58. return fmt.Sprintf("<@%s>", parsedID)
  59. }
  60. mentionedUser := user.bridge.GetUserByMXID(id.UserID(mxid))
  61. if mentionedUser != nil && mentionedUser.DiscordID != "" {
  62. return fmt.Sprintf("<@%s>", mentionedUser.DiscordID)
  63. }
  64. }
  65. return displayname
  66. }
  67. var matrixHTMLParser = &format.HTMLParser{
  68. TabsToSpaces: 4,
  69. Newline: "\n",
  70. HorizontalLine: "\n---\n",
  71. ItalicConverter: func(s string, context format.Context) string {
  72. return fmt.Sprintf("*%s*", s)
  73. },
  74. UnderlineConverter: func(s string, context format.Context) string {
  75. return fmt.Sprintf("__%s__", s)
  76. },
  77. TextConverter: func(s string, context format.Context) string {
  78. return discordMarkdownEscaper.Replace(s)
  79. },
  80. SpoilerConverter: func(text, reason string, ctx format.Context) string {
  81. if reason != "" {
  82. return fmt.Sprintf("(%s) ||%s||", reason, text)
  83. }
  84. return fmt.Sprintf("||%s||", text)
  85. },
  86. }
  87. func init() {
  88. matrixHTMLParser.PillConverter = pillConverter
  89. }
  90. var discordMarkdownEscaper = strings.NewReplacer(
  91. `\`, `\\`,
  92. `_`, `\_`,
  93. `*`, `\*`,
  94. `~`, `\~`,
  95. "`", "\\`",
  96. `|`, `\|`,
  97. `<`, `\<`,
  98. )
  99. func (portal *Portal) parseMatrixHTML(user *User, content *event.MessageEventContent) string {
  100. if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
  101. return matrixHTMLParser.Parse(content.FormattedBody, format.Context{
  102. formatterContextUserKey: user,
  103. formatterContextPortalKey: portal,
  104. })
  105. } else {
  106. return discordMarkdownEscaper.Replace(content.Body)
  107. }
  108. }