Skip to content

Commit 509e45f

Browse files
authored
fix: render html as text in ChannelPreview (#2830)
1 parent 1bdcb8d commit 509e45f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/components/ChannelPreview/__tests__/utils.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
import ReactMarkdown from 'react-markdown';
13
import '@testing-library/jest-dom';
24
import { nanoid } from 'nanoid';
35

@@ -14,6 +16,7 @@ import {
1416

1517
import { getDisplayImage, getDisplayTitle, getLatestMessagePreview } from '../utils';
1618
import { generateStaticLocationResponse } from '../../../mock-builders';
19+
import { render } from '@testing-library/react';
1720

1821
describe('ChannelPreview utils', () => {
1922
const clientUser = generateUser();
@@ -55,16 +58,45 @@ describe('ChannelPreview utils', () => {
5558
}),
5659
],
5760
});
61+
const channelWithHTMLInMessage = generateChannel({
62+
messages: [
63+
generateMessage({
64+
attachments: [generateImageAttachment()],
65+
text:
66+
'<h1>Hello, world!</h1> \n' +
67+
'<p>This is my first web page.</p> \n' +
68+
'<p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>',
69+
}),
70+
],
71+
});
72+
73+
const expectedTextWithHTMLRendering =
74+
'<h1>Hello, world!</h1> <p>This is my first web page.</p> <p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>';
75+
76+
function isReactMarkdownElement(x) {
77+
return React.isValidElement(x) && x.type === ReactMarkdown;
78+
}
5879

5980
it.each([
6081
['Nothing yet...', 'channelWithEmptyMessage', channelWithEmptyMessage],
6182
['Message deleted', 'channelWithDeletedMessage', channelWithDeletedMessage],
6283
['🏙 Attachment...', 'channelWithAttachmentMessage', channelWithAttachmentMessage],
6384
['📍Shared location', 'channelWithLocationMessage', channelWithLocationMessage],
85+
[
86+
expectedTextWithHTMLRendering,
87+
'channelWithHTMLInMessage',
88+
channelWithHTMLInMessage,
89+
],
6490
])('should return %s for %s', async (expectedValue, testCaseName, c) => {
6591
const t = (text) => text;
6692
const channel = await getQueriedChannelInstance(c);
67-
expect(getLatestMessagePreview(channel, t)).toBe(expectedValue);
93+
const preview = getLatestMessagePreview(channel, t);
94+
if (isReactMarkdownElement(preview)) {
95+
const { container } = render(preview);
96+
expect(container).toHaveTextContent(expectedValue);
97+
} else {
98+
expect(getLatestMessagePreview(channel, t)).toBe(expectedValue);
99+
}
68100
});
69101
});
70102

src/components/ChannelPreview/utils.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ import type { Channel, PollVote, TranslationLanguages, UserResponse } from 'stre
55

66
import type { TranslationContextValue } from '../../context/TranslationContext';
77
import type { ChatContextValue } from '../../context';
8+
import type { PluggableList } from 'unified';
9+
import { htmlToTextPlugin } from '../Message';
10+
import remarkGfm from 'remark-gfm';
11+
12+
const remarkPlugins: PluggableList = [
13+
htmlToTextPlugin,
14+
[remarkGfm, { singleTilde: false }],
15+
];
816

917
export const renderPreviewText = (text: string) => (
10-
<ReactMarkdown skipHtml>{text}</ReactMarkdown>
18+
<ReactMarkdown remarkPlugins={remarkPlugins} skipHtml>
19+
{text}
20+
</ReactMarkdown>
1121
);
1222

1323
const getLatestPollVote = (latestVotesByOption: Record<string, PollVote[]>) => {

0 commit comments

Comments
 (0)