From 1c70696b1020c9f54f78855b78664cdccaa81a2e Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 2 May 2022 20:26:37 -0400 Subject: [PATCH] Don't linkify code blocks (#7859) * Don't linkify code blocks Signed-off-by: Robin Townsend * Put the linkify ignoreTags option in the right place Signed-off-by: Robin Townsend * Add code to list of ignored linkification tags as well Signed-off-by: Robin Townsend * Test that code blocks skip linkification Signed-off-by: Robin Townsend * Move test to the right spot Signed-off-by: Robin Townsend * Use a snapshot instead for test Signed-off-by: Robin Townsend --- src/linkify-matrix.ts | 2 ++ .../views/messages/TextualBody-test.tsx | 20 +++++++++++++++++++ .../__snapshots__/TextualBody-test.tsx.snap | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/src/linkify-matrix.ts b/src/linkify-matrix.ts index c42d98d326c..7935f5d0377 100644 --- a/src/linkify-matrix.ts +++ b/src/linkify-matrix.ts @@ -225,6 +225,8 @@ export const options = { rel: 'noreferrer noopener', }, + ignoreTags: ['pre', 'code'], + className: 'linkified', target: function(href: string, type: Type | string): string { diff --git a/test/components/views/messages/TextualBody-test.tsx b/test/components/views/messages/TextualBody-test.tsx index 371c372196f..fe4dbf99467 100644 --- a/test/components/views/messages/TextualBody-test.tsx +++ b/test/components/views/messages/TextualBody-test.tsx @@ -216,6 +216,26 @@ describe("", () => { ''); }); + it("linkification is not applied to code blocks", () => { + const ev = mkEvent({ + type: "m.room.message", + room: "room_id", + user: "sender", + content: { + body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```", + msgtype: "m.text", + format: "org.matrix.custom.html", + formatted_body: "

Visit https://matrix.org/

\n
https://matrix.org/\n
\n", + }, + event: true, + }); + + const wrapper = getComponent({ mxEvent: ev }, matrixClient); + expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); + const content = wrapper.find(".mx_EventTile_body"); + expect(content.html()).toMatchSnapshot(); + }); + // If pills were rendered within a Portal/same shadow DOM then it'd be easier to test it("pills get injected correctly into the DOM", () => { const ev = mkEvent({ diff --git a/test/components/views/messages/__snapshots__/TextualBody-test.tsx.snap b/test/components/views/messages/__snapshots__/TextualBody-test.tsx.snap index 9d481765e1d..15d3b7e208f 100644 --- a/test/components/views/messages/__snapshots__/TextualBody-test.tsx.snap +++ b/test/components/views/messages/__snapshots__/TextualBody-test.tsx.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[` renders formatted m.text correctly linkification is not applied to code blocks 1`] = ` +"

Visit https://matrix.org/

+
1https://matrix.org/
+
+
" +`; + exports[` renders formatted m.text correctly pills do not appear in code blocks 1`] = ` "

@room

1@room