From 751a158fbf720a9bb648364afe4120e2559aecb8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 15 Apr 2022 14:12:01 +0300 Subject: [PATCH] Parse HTML when editing messages. Fixes #301 --- ui/room-view.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ui/room-view.go b/ui/room-view.go index 01a4080..bbe2465 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -33,6 +33,7 @@ import ( "maunium.net/go/mautrix" "maunium.net/go/mautrix/crypto/attachment" "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" "maunium.net/go/mautrix/util/variationselector" @@ -423,6 +424,18 @@ func (view *RoomView) SetTyping(users []id.UserID) { } } +var editHTMLParser = &format.HTMLParser{ + PillConverter: func(displayname, mxid, eventID string, ctx format.Context) string { + if len(eventID) > 0 { + return fmt.Sprintf(`[%s](https://matrix.to/#/%s/%s)`, displayname, mxid, eventID) + } else { + return fmt.Sprintf(`[%s](https://matrix.to/#/%s)`, displayname, mxid) + } + }, + Newline: "\n", + HorizontalLine: "\n---\n", +} + func (view *RoomView) SetEditing(evt *muksevt.Event) { if evt == nil { view.editing = nil @@ -440,8 +453,14 @@ func (view *RoomView) SetEditing(evt *muksevt.Event) { // This feels kind of dangerous, but I think it works msgContent = view.editing.Gomuks.Edits[len(view.editing.Gomuks.Edits)-1].Content.AsMessage().NewContent } - // TODO this should parse HTML instead of just using the plaintext body text := msgContent.Body + if len(msgContent.FormattedBody) > 0 && (!view.config.Preferences.DisableMarkdown || !view.config.Preferences.DisableHTML) { + if view.config.Preferences.DisableMarkdown { + text = msgContent.FormattedBody + } else { + text = editHTMLParser.Parse(msgContent.FormattedBody, make(format.Context)) + } + } if msgContent.MsgType == event.MsgEmote { text = "/me " + text }