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

Commit

Permalink
Don't linkify code blocks (#7859)
Browse files Browse the repository at this point in the history
* Don't linkify code blocks

Signed-off-by: Robin Townsend <robin@robin.town>

* Put the linkify ignoreTags option in the right place

Signed-off-by: Robin Townsend <robin@robin.town>

* Add code to list of ignored linkification tags as well

Signed-off-by: Robin Townsend <robin@robin.town>

* Test that code blocks skip linkification

Signed-off-by: Robin Townsend <robin@robin.town>

* Move test to the right spot

Signed-off-by: Robin Townsend <robin@robin.town>

* Use a snapshot instead for test

Signed-off-by: Robin Townsend <robin@robin.town>
  • Loading branch information
robintown authored May 3, 2022
1 parent 7477a2d commit 1c70696
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/linkify-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export const options = {
rel: 'noreferrer noopener',
},

ignoreTags: ['pre', 'code'],

className: 'linkified',

target: function(href: string, type: Type | string): string {
Expand Down
20 changes: 20 additions & 0 deletions test/components/views/messages/TextualBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ describe("<TextualBody />", () => {
'</span></span></span>');
});

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: "<p>Visit <code>https://matrix.org/</code></p>\n<pre>https://matrix.org/\n</pre>\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({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

0 comments on commit 1c70696

Please sign in to comment.