-
Notifications
You must be signed in to change notification settings - Fork 324
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
package/src/components/Message/MessageSimple/utils/generateMarkdownText.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, '\\$&'); | ||
} | ||
|
||
export const generateMarkdownText = (text?: string) => { | ||
if (!text) return null; | ||
|
||
// Trim the extra spaces from the text. | ||
|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment