From 82c8b3f83cee6f8ed0190acf575b0d900c76535a Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 6 Dec 2023 09:09:37 +0100 Subject: [PATCH] fix: use gomatrix to render markdown messages (#56) --- go.mod | 4 ++-- go.sum | 4 ++-- plugin/impl.go | 15 +++------------ 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index e124b50..0177320 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,8 @@ module github.com/thegeeklab/wp-matrix go 1.21 require ( - github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 github.com/microcosm-cc/bluemonday v1.0.26 github.com/rs/zerolog v1.31.0 - github.com/russross/blackfriday/v2 v2.1.0 github.com/thegeeklab/wp-plugin-go v1.1.0 github.com/urfave/cli/v2 v2.26.0 maunium.net/go/mautrix v0.16.2 @@ -27,6 +25,7 @@ require ( github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/tidwall/gjson v1.17.0 // indirect @@ -34,6 +33,7 @@ require ( github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/sjson v1.2.5 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + github.com/yuin/goldmark v1.6.0 // indirect go.mau.fi/util v0.2.1 // indirect golang.org/x/crypto v0.16.0 // indirect golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect diff --git a/go.sum b/go.sum index 7b75867..dc27d5e 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,6 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U= -github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -76,6 +74,8 @@ github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6S github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68= +github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mau.fi/util v0.2.1 h1:eazulhFE/UmjOFtPrGg6zkF5YfAyiDzQb8ihLMbsPWw= go.mau.fi/util v0.2.1/go.mod h1:MjlzCQEMzJ+G8RsPawHzpLB8rwTo3aPIjG5FzBvQT/c= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/plugin/impl.go b/plugin/impl.go index 6d4b9eb..31a0454 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -12,13 +12,12 @@ import ( "fmt" "strings" - "github.com/matrix-org/gomatrix" "github.com/microcosm-cc/bluemonday" "github.com/rs/zerolog/log" - "github.com/russross/blackfriday/v2" "github.com/thegeeklab/wp-plugin-go/template" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" ) @@ -81,16 +80,8 @@ func (p *Plugin) Execute() error { return fmt.Errorf("failed to render template: %w", err) } - formatted := bluemonday.UGCPolicy().SanitizeBytes( - blackfriday.Run([]byte(message)), - ) - - content := gomatrix.HTMLMessage{ - Body: message, - MsgType: "m.notice", - Format: "org.matrix.custom.html", - FormattedBody: string(formatted), - } + formatted := bluemonday.UGCPolicy().SanitizeBytes([]byte(message)) + content := format.RenderMarkdown(string(formatted), true, false) if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil { return fmt.Errorf("failed to submit message: %w", err)