-
Notifications
You must be signed in to change notification settings - Fork 263
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
Closed
+362
−11
Closed
Changes from all commits
Commits
Show all changes
3 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,31 @@ | ||
| import {tokenItemToString, TokenizedText} from './TokenizedText.js' | ||
| import {LinksContext, Link} from '../contexts/LinksContext.js' | ||
| import {unstyled} from '../../../../public/node/output.js' | ||
| import {render} from '../../testing/ui.js' | ||
| import {describe, expect, test} from 'vitest' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
| import supportsHyperlinks from 'supports-hyperlinks' | ||
|
|
||
| import React from 'react' | ||
| import React, {FunctionComponent, useRef} from 'react' | ||
|
|
||
| vi.mock('supports-hyperlinks') | ||
|
|
||
| const WithLinksContext: FunctionComponent<{children: React.ReactNode}> = ({children}) => { | ||
| const links = useRef<Record<string, Link>>({}) | ||
| return ( | ||
| <LinksContext.Provider | ||
| value={{ | ||
| links, | ||
| addLink: (label, url) => { | ||
| const newId = (Object.keys(links.current).length + 1).toString() | ||
| links.current = {...links.current, [newId]: {label, url}} | ||
| return newId | ||
| }, | ||
| }} | ||
| > | ||
| {children} | ||
| </LinksContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| describe('TokenizedText', async () => { | ||
| test('renders arrays of items separated by spaces', async () => { | ||
|
|
@@ -57,6 +79,142 @@ describe('TokenizedText', async () => { | |
| `) | ||
| }) | ||
|
|
||
| describe('URL auto-detection in plain strings', async () => { | ||
| test('renders strings without URLs unchanged', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = false | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item="no link here, just text" /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toBe('no link here, just text') | ||
| }) | ||
|
|
||
| test('replaces a URL with a `[N]` footnote anchor in the body when the terminal does not support hyperlinks', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = false | ||
| const url = 'https://shopify.dev/docs/apps/build/sales-channels/channel-config-extension#specification-properties' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`See specification requirements: ${url}`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toBe('See specification requirements: [1]') | ||
| expect(lastFrame()).not.toContain(url) | ||
| }) | ||
|
|
||
| test('wraps detected URLs in OSC 8 escapes when the terminal supports hyperlinks', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const url = 'https://example.com/docs' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`visit ${url} now`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toContain(`]8;;${url}${url}]8;;`) | ||
| }) | ||
|
|
||
| test('detects multiple URLs in the same string', async () => { | ||
|
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. Might be worth adding edge test cases where there's back to back URLs |
||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const first = 'https://example.com/a' | ||
| const second = 'https://example.com/b' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`see ${first} and ${second}`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toContain(`]8;;${first}${first}]8;;`) | ||
| expect(lastFrame()).toContain(`]8;;${second}${second}]8;;`) | ||
| }) | ||
|
|
||
| test('detects back-to-back URLs separated only by whitespace', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const first = 'https://example.com/a' | ||
| const second = 'https://example.com/b' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`${first} ${second}`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toContain(`]8;;${first}${first}]8;;`) | ||
| expect(lastFrame()).toContain(`]8;;${second}${second}]8;;`) | ||
| }) | ||
|
|
||
| test('preserves balanced parentheses inside URLs (e.g. Wikipedia disambiguation links)', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const url = 'https://en.wikipedia.org/wiki/Ruby_(programming_language)' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`see ${url} for details`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toContain(`]8;;${url}${url}]8;;`) | ||
| }) | ||
|
|
||
| test('does not auto-linkify URLs outside a LinksContext', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const url = 'https://example.com/docs' | ||
|
|
||
| const {lastFrame} = render(<TokenizedText item={`visit ${url} now`} />) | ||
|
|
||
| expect(lastFrame()).toBe(`visit ${url} now`) | ||
| expect(lastFrame()).not.toContain(']8;;') | ||
| }) | ||
|
|
||
| test('does not linkify https://-prefixed strings whose hostname has no dot (e.g. user typos like https://asda)', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const candidate = 'https://asda' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`Invalid tunnel URL: ${candidate}`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toBe(`Invalid tunnel URL: ${candidate}`) | ||
| expect(lastFrame()).not.toContain(']8;;') | ||
| }) | ||
|
|
||
| test('does not linkify placeholder URLs with non-numeric ports (e.g. https://my-tunnel-url:port)', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const candidate = 'https://my-tunnel-url:port' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`Valid format: "${candidate}"`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toBe(`Valid format: "${candidate}"`) | ||
| expect(lastFrame()).not.toContain(']8;;') | ||
| }) | ||
|
|
||
| test('strips trailing sentence punctuation from detected URLs', async () => { | ||
| vi.mocked(supportsHyperlinks).stdout = true | ||
| const url = 'https://example.com/docs' | ||
|
|
||
| const {lastFrame} = render( | ||
| <WithLinksContext> | ||
| <TokenizedText item={`see ${url}. Thanks`} /> | ||
| </WithLinksContext>, | ||
| ) | ||
|
|
||
| expect(lastFrame()).toContain(`]8;;${url}${url}]8;;`) | ||
| expect(lastFrame()).toContain('. Thanks') | ||
| }) | ||
| }) | ||
|
|
||
| describe('tokenItemToString', async () => { | ||
| test("doesn't add a space before char", async () => { | ||
| expect(tokenItemToString(['Run', {char: '!'}])).toBe('Run!') | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.