Fix markdown paragraphs not rendering properly (#443)

* Fix markdown paragraphs not rendering properly

* changelog
This commit is contained in:
Will Hunt 2022-08-18 15:41:07 +01:00 committed by GitHub
parent 52b6033908
commit 00e5bfe332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 5 deletions

1
changelog.d/443.bugfix Normal file
View File

@ -0,0 +1 @@
Headers and paragraphs now rendered properly when outputted from a Generic webhook transformation function.

View File

@ -377,7 +377,8 @@ export class GenericHookConnection extends BaseConnection implements IConnection
await this.messageClient.sendMatrixMessage(this.roomId, { await this.messageClient.sendMatrixMessage(this.roomId, {
msgtype: content.msgtype || "m.notice", msgtype: content.msgtype || "m.notice",
body: content.plain, 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", format: "org.matrix.custom.html",
"uk.half-shot.hookshot.webhook_data": safeData, "uk.half-shot.hookshot.webhook_data": safeData,
}, 'm.room.message', sender); }, 'm.room.message', sender);

View File

@ -63,7 +63,43 @@ describe("GenericHookConnection", () => {
content: { content: {
body: "simple-message", body: "simple-message",
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: "simple-message", formatted_body: "<p>simple-message</p>",
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: "<p><strong>bold-message</strong> <em>italic-message</em></p>",
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: "<h1>Oh wow</h1>\n<p><code>some-code</code></p>",
msgtype: "m.notice", msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData, "uk.half-shot.hookshot.webhook_data": webhookData,
}, },
@ -122,7 +158,7 @@ describe("GenericHookConnection", () => {
content: { content: {
body: "Received webhook: The answer to 'What is the meaning of life?' is 42", body: "Received webhook: The answer to 'What is the meaning of life?' is 42",
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: "Received webhook: The answer to 'What is the meaning of life?' is 42", formatted_body: "<p>Received webhook: The answer to 'What is the meaning of life?' is 42</p>",
msgtype: "m.notice", msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData, "uk.half-shot.hookshot.webhook_data": webhookData,
}, },
@ -145,7 +181,7 @@ describe("GenericHookConnection", () => {
content: { content: {
body: "The answer to 'What is the meaning of life?' is 42", body: "The answer to 'What is the meaning of life?' is 42",
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: "The answer to 'What is the meaning of life?' is 42", formatted_body: "<p>The answer to 'What is the meaning of life?' is 42</p>",
msgtype: "m.notice", msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData, "uk.half-shot.hookshot.webhook_data": webhookData,
}, },
@ -168,7 +204,7 @@ describe("GenericHookConnection", () => {
content: { content: {
body: "Webhook received but failed to process via transformation function", body: "Webhook received but failed to process via transformation function",
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: "Webhook received but failed to process via transformation function", formatted_body: "<p>Webhook received but failed to process via transformation function</p>",
msgtype: "m.notice", msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData, "uk.half-shot.hookshot.webhook_data": webhookData,
}, },