-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d86f60
commit c5b5f8a
Showing
5 changed files
with
73 additions
and
6 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,25 @@ | ||
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 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-sicherh...') | ||
}) | ||
}) |
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,30 @@ | ||
import Linkify from 'linkify-react' | ||
import { FC } from 'react' | ||
|
||
interface Props { | ||
message: string | ||
} | ||
const Links: FC<Props> = ({ message }) => { | ||
const render = ({ attributes, content }: { attributes: unknown; content: string }) => { | ||
const { href } = attributes as { href: string } | ||
content = content.replace(/^(https?:\/\/)/, '') | ||
|
||
if (content.endsWith('/')) { | ||
content = content.slice(0, -1) | ||
} | ||
|
||
if (content.length > 40) { | ||
content = content.slice(0, 40) + '...' | ||
} | ||
|
||
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