From 00e5bfe3328b6dfeb3cecd6e95a4e1fe592269b4 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 18 Aug 2022 15:41:07 +0100 Subject: [PATCH] Fix markdown paragraphs not rendering properly (#443) * Fix markdown paragraphs not rendering properly * changelog --- changelog.d/443.bugfix | 1 + src/Connections/GenericHook.ts | 3 +- tests/connections/GenericHookTest.ts | 44 +++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 changelog.d/443.bugfix diff --git a/changelog.d/443.bugfix b/changelog.d/443.bugfix new file mode 100644 index 00000000..c11337ea --- /dev/null +++ b/changelog.d/443.bugfix @@ -0,0 +1 @@ +Headers and paragraphs now rendered properly when outputted from a Generic webhook transformation function. \ No newline at end of file diff --git a/src/Connections/GenericHook.ts b/src/Connections/GenericHook.ts index 15b2e0ef..a8d9a052 100644 --- a/src/Connections/GenericHook.ts +++ b/src/Connections/GenericHook.ts @@ -377,7 +377,8 @@ export class GenericHookConnection extends BaseConnection implements IConnection await this.messageClient.sendMatrixMessage(this.roomId, { msgtype: content.msgtype || "m.notice", body: content.plain, - formatted_body: content.html || md.renderInline(content.plain), + // render can output redundant trailing newlines, so trim it. + formatted_body: content.html || md.render(content.plain).trim(), format: "org.matrix.custom.html", "uk.half-shot.hookshot.webhook_data": safeData, }, 'm.room.message', sender); diff --git a/tests/connections/GenericHookTest.ts b/tests/connections/GenericHookTest.ts index 107676cc..43f35722 100644 --- a/tests/connections/GenericHookTest.ts +++ b/tests/connections/GenericHookTest.ts @@ -63,7 +63,43 @@ describe("GenericHookConnection", () => { content: { body: "simple-message", format: "org.matrix.custom.html", - formatted_body: "simple-message", + formatted_body: "

simple-message

", + msgtype: "m.notice", + "uk.half-shot.hookshot.webhook_data": webhookData, + }, + type: 'm.room.message', + }); + }); + it("will handle a hook event containing markdown", async () => { + const webhookData = {text: "**bold-message** _italic-message_"}; + const [connection, mq] = createGenericHook(); + const messagePromise = handleMessage(mq); + await connection.onGenericHook(webhookData); + expect(await messagePromise).to.deep.equal({ + roomId: ROOM_ID, + sender: connection.getUserId(), + content: { + body: "**bold-message** _italic-message_", + format: "org.matrix.custom.html", + formatted_body: "

bold-message italic-message

", + msgtype: "m.notice", + "uk.half-shot.hookshot.webhook_data": webhookData, + }, + type: 'm.room.message', + }); + }); + it("will handle a hook event containing markdown with newlines", async () => { + const webhookData = {text: "# Oh wow\n\n`some-code`"}; + const [connection, mq] = createGenericHook(); + const messagePromise = handleMessage(mq); + await connection.onGenericHook(webhookData); + expect(await messagePromise).to.deep.equal({ + roomId: ROOM_ID, + sender: connection.getUserId(), + content: { + body: "# Oh wow\n\n`some-code`", + format: "org.matrix.custom.html", + formatted_body: "

Oh wow

\n

some-code

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, }, @@ -122,7 +158,7 @@ describe("GenericHookConnection", () => { content: { body: "Received webhook: The answer to 'What is the meaning of life?' is 42", format: "org.matrix.custom.html", - formatted_body: "Received webhook: The answer to 'What is the meaning of life?' is 42", + formatted_body: "

Received webhook: The answer to 'What is the meaning of life?' is 42

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, }, @@ -145,7 +181,7 @@ describe("GenericHookConnection", () => { content: { body: "The answer to 'What is the meaning of life?' is 42", format: "org.matrix.custom.html", - formatted_body: "The answer to 'What is the meaning of life?' is 42", + formatted_body: "

The answer to 'What is the meaning of life?' is 42

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, }, @@ -168,7 +204,7 @@ describe("GenericHookConnection", () => { content: { body: "Webhook received but failed to process via transformation function", format: "org.matrix.custom.html", - formatted_body: "Webhook received but failed to process via transformation function", + formatted_body: "

Webhook received but failed to process via transformation function

", msgtype: "m.notice", "uk.half-shot.hookshot.webhook_data": webhookData, },