Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1c70696

Browse files
authored
Don't linkify code blocks (#7859)
* Don't linkify code blocks Signed-off-by: Robin Townsend <[email protected]> * Put the linkify ignoreTags option in the right place Signed-off-by: Robin Townsend <[email protected]> * Add code to list of ignored linkification tags as well Signed-off-by: Robin Townsend <[email protected]> * Test that code blocks skip linkification Signed-off-by: Robin Townsend <[email protected]> * Move test to the right spot Signed-off-by: Robin Townsend <[email protected]> * Use a snapshot instead for test Signed-off-by: Robin Townsend <[email protected]>
1 parent 7477a2d commit 1c70696

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/linkify-matrix.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ export const options = {
225225
rel: 'noreferrer noopener',
226226
},
227227

228+
ignoreTags: ['pre', 'code'],
229+
228230
className: 'linkified',
229231

230232
target: function(href: string, type: Type | string): string {

test/components/views/messages/TextualBody-test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ describe("<TextualBody />", () => {
216216
'</span></span></span>');
217217
});
218218

219+
it("linkification is not applied to code blocks", () => {
220+
const ev = mkEvent({
221+
type: "m.room.message",
222+
room: "room_id",
223+
user: "sender",
224+
content: {
225+
body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```",
226+
msgtype: "m.text",
227+
format: "org.matrix.custom.html",
228+
formatted_body: "<p>Visit <code>https://matrix.org/</code></p>\n<pre>https://matrix.org/\n</pre>\n",
229+
},
230+
event: true,
231+
});
232+
233+
const wrapper = getComponent({ mxEvent: ev }, matrixClient);
234+
expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n");
235+
const content = wrapper.find(".mx_EventTile_body");
236+
expect(content.html()).toMatchSnapshot();
237+
});
238+
219239
// If pills were rendered within a Portal/same shadow DOM then it'd be easier to test
220240
it("pills get injected correctly into the DOM", () => {
221241
const ev = mkEvent({

test/components/views/messages/__snapshots__/TextualBody-test.tsx.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`<TextualBody /> renders formatted m.text correctly linkification is not applied to code blocks 1`] = `
4+
"<span class=\\"mx_EventTile_body markdown-body\\" dir=\\"auto\\"><p>Visit <code>https://matrix.org/</code></p>
5+
<div class=\\"mx_EventTile_pre_container\\"><pre class=\\"mx_EventTile_collapsedCodeBlock\\"><span class=\\"mx_EventTile_lineNumbers\\"><span>1</span></span><code>https://matrix.org/
6+
</code><span></span></pre><span class=\\"mx_EventTile_button mx_EventTile_copyButton \\"></span></div>
7+
</span>"
8+
`;
9+
310
exports[`<TextualBody /> renders formatted m.text correctly pills do not appear in code blocks 1`] = `
411
"<span class=\\"mx_EventTile_body markdown-body\\" dir=\\"auto\\"><p><code>@room</code></p>
512
<div class=\\"mx_EventTile_pre_container\\"><pre class=\\"mx_EventTile_collapsedCodeBlock\\"><span class=\\"mx_EventTile_lineNumbers\\"><span>1</span></span><code>@room

0 commit comments

Comments
 (0)