From 029ae18a07c8c8692e7ae8836fcdb6ccfbe94e84 Mon Sep 17 00:00:00 2001
From: Loren Burkholder <computersemiexpert@outlook.com>
Date: Sat, 13 Feb 2021 13:48:37 -0500
Subject: [PATCH] Format markdown as HTML in notifications

---
 src/notifications/ManagerLinux.cpp | 6 ++++--
 src/notifications/ManagerMac.mm    | 6 ++++--
 src/notifications/ManagerWin.cpp   | 6 ++++--
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/notifications/ManagerLinux.cpp b/src/notifications/ManagerLinux.cpp
index a222bd365..70f131c29 100644
--- a/src/notifications/ManagerLinux.cpp
+++ b/src/notifications/ManagerLinux.cpp
@@ -13,6 +13,7 @@
 #include "MatrixClient.h"
 #include "Utils.h"
 #include <mtx/responses/notifications.hpp>
+#include <cmark.h>
 
 NotificationsManager::NotificationsManager(QObject *parent)
   : QObject(parent)
@@ -58,6 +59,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
         const auto sender   = cache::displayName(
           room_id, QString::fromStdString(mtx::accessors::sender(notification.event)));
         const auto text = utils::event_body(notification.event);
+        const auto formattedText = cmark_markdown_to_html(text.toStdString().c_str(), text.length(), CMARK_OPT_UNSAFE);
 
         QVariantMap hints;
         hints["image-data"] = icon;
@@ -71,9 +73,9 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
 
         // body
         if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
-                argumentList << "* " + sender + " " + text;
+                argumentList << "* " + sender + " " + formattedText;
         else
-                argumentList << sender + ": " + text;
+                argumentList << sender + ": " + formattedText;
 
         // The list of actions has always the action name and then a localized version of that
         // action. Currently we just use an empty string for that.
diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm
index 5609d3dea..af0b5a029 100644
--- a/src/notifications/ManagerMac.mm
+++ b/src/notifications/ManagerMac.mm
@@ -8,6 +8,7 @@
 #include "MatrixClient.h"
 #include "Utils.h"
 #include <mtx/responses/notifications.hpp>
+#include <cmark.h>
 
 @interface NSUserNotification (CFIPrivate)
 - (void)set_identityImage:(NSImage *)image;
@@ -26,15 +27,16 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
 
     const auto sender   = cache::displayName(QString::fromStdString(notification.room_id), QString::fromStdString(mtx::accessors::sender(notification.event)));
     const auto text     = utils::event_body(notification.event);
+    const auto formattedText = cmark_markdown_to_html(text.toStdString().c_str(), text.length(), CMARK_OPT_UNSAFE);
 
     NSUserNotification * notif = [[NSUserNotification alloc] init];
 
     notif.title           = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name).toNSString();
     notif.subtitle        = QString("%1 sent a message").arg(sender).toNSString();
     if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
-            notif.informativeText = QString("* ").append(sender).append(" ").append(text).toNSString();
+            notif.informativeText = QString("* ").append(sender).append(" ").append(formattedText).toNSString();
     else
-            notif.informativeText = text.toNSString();
+            notif.informativeText = formattedText.toNSString();
     notif.soundName       = NSUserNotificationDefaultSoundName;
 
     [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notif];
diff --git a/src/notifications/ManagerWin.cpp b/src/notifications/ManagerWin.cpp
index 3152d84f8..5a1a867c9 100644
--- a/src/notifications/ManagerWin.cpp
+++ b/src/notifications/ManagerWin.cpp
@@ -10,6 +10,7 @@
 #include "MatrixClient.h"
 #include "Utils.h"
 #include <mtx/responses/notifications.hpp>
+#include <cmark.h>
 
 using namespace WinToastLib;
 
@@ -53,6 +54,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
           cache::displayName(QString::fromStdString(notification.room_id),
                              QString::fromStdString(mtx::accessors::sender(notification.event)));
         const auto text = utils::event_body(notification.event);
+        const auto formattedText = cmark_markdown_to_html(text.toStdString().c_str(), text.length(), CMARK_OPT_UNSAFE);
 
         if (!isInitialized)
                 init();
@@ -66,10 +68,10 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
                                    WinToastTemplate::FirstLine);
         if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
                 templ.setTextField(
-                  QString("* ").append(sender).append(" ").append(text).toStdWString(),
+                  QString("* ").append(sender).append(" ").append(formattedText).toStdWString(),
                   WinToastTemplate::SecondLine);
         else
-                templ.setTextField(QString("%1").arg(text).toStdWString(),
+                templ.setTextField(QString("%1").arg(formattedText).toStdWString(),
                                    WinToastTemplate::SecondLine);
         // TODO: implement room or user avatar
         // templ.setImagePath(L"C:/example.png");
-- 
GitLab