Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app crash when the message links have special characters in it #2318

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { generateMarkdownText } from './generateMarkdownText';

describe('generateMarkdownText', () => {
it.each([
['', null],
[' test message ', 'test message'],
['https://www.getstream.io', '[https://www.getstream.io](https://www.getstream.io)'],
[
'https://getstream-production.s3-accelerate.amazonaws.com/N336903591601695/33e78ef89e64642862a75c5cca2541eaf6b1c924/trimmedVideos/alert/2_270_881/outputVideo.mp4?AWSAccessKeyId=AKIAVJAW2AD2SQVQCBXV&Expires=1699998768&Signature=zdEMCGzf4Pq++16YkPprvN5NAds=',
'[https://getstream-production.s3-accelerate.amazonaws.com/N336903591601695/33e78ef89e64642862a75c5cca2541eaf6b1c924/trimmedVideos/alert/2_270_881/outputVideo.mp4?AWSAccessKeyId=AKIAVJAW2AD2SQVQCBXV&...](https://getstream-production.s3-accelerate.amazonaws.com/N336903591601695/33e78ef89e64642862a75c5cca2541eaf6b1c924/trimmedVideos/alert/2_270_881/outputVideo.mp4?AWSAccessKeyId=AKIAVJAW2AD2SQVQCBXV&Expires=1699998768&Signature=zdEMCGzf4Pq++16YkPprvN5NAds=)',
],
['Hi @getstream.io', 'Hi @getstream.io'],
[
'Hi [email protected] @[email protected]',
'Hi [[email protected]](mailto:[email protected]) @[email protected]',
],
['Hi @getstream.io getstream.io', 'Hi @getstream.io [getstream.io](http://getstream.io)'],
['Hi <Stream>', 'Hi \\<Stream\\>'],
])('Returns the generated markdown text for %p and %p', (text, expected) => {
const result = generateMarkdownText(text);
expect(result).toBe(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import truncate from 'lodash/truncate';

import { parseLinksFromText } from './parseLinks';

import type { DefaultStreamChatGenerics } from '../../../../types/types';
import type { MessageType } from '../../../MessageList/hooks/useMessageList';

export const generateMarkdownText = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
message: MessageType<StreamChatGenerics>,
) => {
const { text } = message;
// If you need to use any of the special characters literally (actually searching for a "*", for instance), you must escape it by putting a backslash in front of it.
function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
Comment on lines +6 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add comments please about what this function does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it replace with \\$&

its not understandable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment


export const generateMarkdownText = (text?: string) => {
if (!text) return null;

// Trim the extra spaces from the text.
Expand All @@ -26,18 +22,21 @@ export const generateMarkdownText = <
omission: '...',
});
// Convert raw links/emails in the text to respective markdown syntax.
// Eg: Hi getstream.io -> Hi [getstream.io](getstream.io).
const normalRegEx = new RegExp(linkInfo.raw, 'g');
// Eg: Hi @getstream.io -> Hi @[getstream.io](getstream.io).
const normalRegEx = new RegExp(escapeRegExp(linkInfo.raw), 'g');
const markdown = `[${displayLink}](${linkInfo.encodedUrl})`;
resultText = text.replace(normalRegEx, markdown);

// After previous step, in some cases, the mentioned user after `@` might have a link/email so we convert it back to normal raw text.
// Eg: Hi, @[[email protected]](mailto:[email protected]) to @[email protected].
const mentionsRegex = new RegExp(`@\\[${displayLink}\\]\\(${linkInfo.encodedUrl}\\)`, 'g');
const mentionsRegex = new RegExp(
`@\\[${escapeRegExp(displayLink)}\\]\\(${escapeRegExp(linkInfo.encodedUrl)}\\)`,
'g',
);
resultText = resultText.replace(mentionsRegex, `@${displayLink}`);
}

resultText = resultText.replace(/[<&"'>]/g, '\\$&');
resultText = resultText.replace(/[<"'>]/g, '\\$&');

return resultText;
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ export const renderText = <
onPress: onPressParam,
preventPress,
} = params;
const { text } = message;

const markdownText = generateMarkdownText<StreamChatGenerics>(message);
const markdownText = generateMarkdownText(text);

const styles: MarkdownStyle = {
...defaultMarkdownStyles,
Expand Down
Loading