-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use only Linkify for replace the links
- Loading branch information
1 parent
f3fc362
commit 11696d8
Showing
6 changed files
with
73 additions
and
71 deletions.
There are no files selected for viewing
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
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,34 @@ | ||
import { render } from '@testing-library/react' | ||
import Links from './Links' | ||
|
||
describe('Links', () => { | ||
it('should render links', () => { | ||
const message = 'https://example.com/' | ||
|
||
render(<Links message={message} />) | ||
|
||
expect(document.querySelector('a')).toHaveAttribute('href', 'https://example.com/') | ||
expect(document.querySelector('a')).toHaveTextContent('example.com') | ||
}) | ||
|
||
it('should render links without protocol', () => { | ||
const message = 'example.com' | ||
|
||
render(<Links message={message} />) | ||
|
||
expect(document.querySelector('a')).toHaveAttribute('href', 'https://example.com') | ||
expect(document.querySelector('a')).toHaveTextContent('example.com') | ||
}) | ||
|
||
it('should shorten long links', () => { | ||
const message = 'https://www.systemli.org/2024/01/11/neue-sicherheitsma%C3%9Fnahme-gegen-mitm-angriffe-eingef%C3%BChrt/' | ||
|
||
render(<Links message={message} />) | ||
|
||
expect(document.querySelector('a')).toHaveAttribute( | ||
'href', | ||
'https://www.systemli.org/2024/01/11/neue-sicherheitsma%C3%9Fnahme-gegen-mitm-angriffe-eingef%C3%BChrt/' | ||
) | ||
expect(document.querySelector('a')).toHaveTextContent('www.systemli.org/2024/01/11/neue-sic...') | ||
}) | ||
}) |
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 Linkify from 'linkify-react' | ||
import { FC } from 'react' | ||
|
||
interface Props { | ||
message: string | ||
} | ||
const Links: FC<Props> = ({ message }) => { | ||
const render = ({ attributes }: { attributes: unknown }) => { | ||
const { href } = attributes as { href: string } | ||
const url = new URL(href) | ||
const content = url.hostname + url.pathname.slice(0, 20) + '...' | ||
|
||
return ( | ||
<a href={href} target="_blank" title={href}> | ||
{content} | ||
</a> | ||
) | ||
} | ||
|
||
return <Linkify options={{ defaultProtocol: 'https', render: render }}>{message}</Linkify> | ||
} | ||
|
||
export default Links |
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
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
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