-
Notifications
You must be signed in to change notification settings - Fork 249
Auto-detect and linkify URLs in plain-string TokenizedText items #7384
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -152,9 +152,40 @@ interface TokenizedTextProps { | |||||
| * `TokenizedText` renders a text string with tokens that can be either strings, | ||||||
| * links, and commands. | ||||||
| */ | ||||||
| const URL_REGEX = /https?:\/\/\S+/g | ||||||
| const URL_TRAILING_PUNCTUATION = /[.,;:!?)\]}>'"]+$/ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Bug: Suggestion: Narrow the character class to unambiguous sentence punctuation, or balance-count parens before stripping. Minimal fix:
Suggested change
And add a test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on this |
||||||
|
|
||||||
| function renderStringWithLinks(str: string): JSX.Element { | ||||||
| const matches = Array.from(str.matchAll(URL_REGEX)) | ||||||
| if (matches.length === 0) { | ||||||
| return <Text>{str}</Text> | ||||||
| } | ||||||
|
|
||||||
| const parts: JSX.Element[] = [] | ||||||
| let cursor = 0 | ||||||
| matches.forEach((match, index) => { | ||||||
| let url = match[0] | ||||||
| const trailing = url.match(URL_TRAILING_PUNCTUATION) | ||||||
| if (trailing) { | ||||||
| url = url.slice(0, url.length - trailing[0].length) | ||||||
| } | ||||||
| const start = match.index | ||||||
| const end = start + url.length | ||||||
| if (start > cursor) { | ||||||
| parts.push(<Text key={`t${index}`}>{str.slice(cursor, start)}</Text>) | ||||||
| } | ||||||
| parts.push(<Link key={`l${index}`} url={url} />) | ||||||
| cursor = end | ||||||
| }) | ||||||
| if (cursor < str.length) { | ||||||
| parts.push(<Text key="tail">{str.slice(cursor)}</Text>) | ||||||
| } | ||||||
| return <Text>{parts}</Text> | ||||||
| } | ||||||
|
|
||||||
| const TokenizedText: FunctionComponent<TokenizedTextProps> = ({item}) => { | ||||||
| if (typeof item === 'string') { | ||||||
| return <Text>{item}</Text> | ||||||
| return renderStringWithLinks(item) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Design: The PR is framed as a Banner/alert fix, but Suggestion: Either (a) scope the auto-detection to contexts that own a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can trigger this one by running: |
||||||
| } else if ('command' in item) { | ||||||
| return <Command command={item.command} /> | ||||||
| } else if ('link' in item) { | ||||||
|
|
||||||

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.
Might be worth adding edge test cases where there's back to back URLs