From 805e47192f922de0c763bb422b9a26e97d418b64 Mon Sep 17 00:00:00 2001
From: "Andrew Morgan andrew@amorgan.xyz" <andrew@amorgan.xyz>
Date: Mon, 21 May 2018 22:38:36 +0200
Subject: [PATCH] Proper markdown parsing, new test for markdown edits

---
 src/messageprocessor.ts       | 10 ++--------
 test/test_messageprocessor.ts | 24 +++++++++++++++++++++---
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts
index bf3303a..5a24615 100644
--- a/src/messageprocessor.ts
+++ b/src/messageprocessor.ts
@@ -73,14 +73,8 @@ export class MessageProcessor {
 
     public async FormatEdit(oldMsg: Discord.Message, newMsg: Discord.Message): Promise<MessageProcessorMatrixResult> {
         // TODO: Produce a nice, colored diff between the old and new message content
-        const formattedOldMsg = await this.FormatDiscordMessage(oldMsg);
-        const formattedNewMsg = await this.FormatDiscordMessage(newMsg);
-
-        const msg = new MessageProcessorMatrixResult();
-
-        msg.body = "edit: " + formattedOldMsg.body + " -> " + formattedNewMsg.body;
-        msg.formattedBody = "<i>edit:</i> <del>" + formattedOldMsg.body + "</del> -> " + formattedNewMsg.body;
-        return msg;
+        oldMsg.content = "*edit:* ~~" + oldMsg.content + "~~ -> " + newMsg.content;
+        return await this.FormatDiscordMessage(oldMsg);
     }
 
     public InsertEmbeds(content: string, msg: Discord.Message): string {
diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts
index 4d9f392..d054b1e 100644
--- a/test/test_messageprocessor.ts
+++ b/test/test_messageprocessor.ts
@@ -52,7 +52,7 @@ describe("MessageProcessor", () => {
       });
     });
     describe("FormatEdit", () => {
-      it("should format edits appropriately", async () => {
+      it("should format basic edits appropriately", async () => {
         const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
         const oldMsg = new Discord.Message(null, null, null);
         const newMsg = new Discord.Message(null, null, null);
@@ -64,9 +64,27 @@ describe("MessageProcessor", () => {
         newMsg.content = "b";
 
         const result = await processor.FormatEdit(oldMsg, newMsg);
-        Chai.assert.equal(result.body, "edit: a -> b");
-        Chai.assert.equal(result.formattedBody, "<i>edit:</i> <del>a</del> -> b");
+        Chai.assert.equal(result.body, "*edit:* ~~a~~ -> b");
+        Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a</del> -&gt; b</p>\n");
       });
+
+      it("should format markdown heavy edits apropriately", async () => {
+        const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+        const oldMsg = new Discord.Message(null, null, null);
+        const newMsg = new Discord.Message(null, null, null);
+        oldMsg.embeds = [];
+        newMsg.embeds = [];
+       
+        // Content updated but not changed
+        oldMsg.content = "a slice of **cake**";
+        newMsg.content = "*a* slice of cake";
+
+        const result = await processor.FormatEdit(oldMsg, newMsg);
+        Chai.assert.equal(result.body, "*edit:* ~~a slice of **cake**~~ -> *a* slice of cake");
+        Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a slice of <strong>" +
+          "cake</strong></del> -&gt; <em>a</em> slice of cake</p>\n");
+      });
+
     });
         
     describe("ReplaceMembers", () => {
-- 
GitLab