diff --git a/src/utils/pillify.tsx b/src/utils/pillify.tsx index 396ddd5dbf9..2032d86b690 100644 --- a/src/utils/pillify.tsx +++ b/src/utils/pillify.tsx @@ -44,7 +44,11 @@ export function pillifyLinks(nodes: ArrayLike, mxEvent: MatrixEvent, pi while (node) { let pillified = false; - if (node.tagName === "A" && node.getAttribute("href")) { + if (node.tagName === "PRE" || node.tagName === "CODE") { + // Skip code blocks + node = node.nextSibling as Element; + continue; + } else if (node.tagName === "A" && node.getAttribute("href")) { const href = node.getAttribute("href"); const parts = parsePermalink(href); // If the link is a (localised) matrix.to link, replace it with a pill diff --git a/test/components/views/messages/TextualBody-test.js b/test/components/views/messages/TextualBody-test.js index 48ab8ed2445..5bf1a7de523 100644 --- a/test/components/views/messages/TextualBody-test.js +++ b/test/components/views/messages/TextualBody-test.js @@ -222,6 +222,26 @@ describe("", () => { ''); }); + it("pills do not appear in code blocks", () => { + const ev = mkEvent({ + type: "m.room.message", + room: "room_id", + user: "sender", + content: { + body: "`@room`\n```\n@room\n```", + msgtype: "m.text", + format: "org.matrix.custom.html", + formatted_body: "

@room

\n
@room\n
\n", + }, + event: true, + }); + + const wrapper = mount(); + expect(wrapper.text()).toBe("@room\n1@room\n\n"); + const content = wrapper.find(".mx_EventTile_body"); + expect(content.html()).toMatchSnapshot(); + }); + it("pills do not appear for event permalinks", () => { const ev = mkEvent({ type: "m.room.message", diff --git a/test/components/views/messages/__snapshots__/TextualBody-test.js.snap b/test/components/views/messages/__snapshots__/TextualBody-test.js.snap new file mode 100644 index 00000000000..9d481765e1d --- /dev/null +++ b/test/components/views/messages/__snapshots__/TextualBody-test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders formatted m.text correctly pills do not appear in code blocks 1`] = ` +"

@room

+
1@room
+
+
" +`;