Skip to content

Commit 155aecd

Browse files
authored
Shorten URL for link definitions (#4860)
* Shorten URL * Add PR number * Rename shortenURL to extractHostnameWithSubdomain * Added a test
1 parent bb5898e commit 155aecd

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2424

2525
### Added
2626

27+
- Resolves [#4853](https://github.com/microsoft/BotFramework-WebChat/issues/4853). Shorten URLs in link definitions UI, by [@compulim](https://github.com/compulim), in PR [#4860](https://github.com/microsoft/BotFramework-WebChat/pull/4860)
2728
- Resolves [#4840](https://github.com/microsoft/BotFramework-WebChat/issues/4840). Added feedback buttons in activity status, by [@compulim](https://github.com/compulim), in PR [#4846](https://github.com/microsoft/BotFramework-WebChat/pull/4846)
2829
- Resolves [#4841](https://github.com/microsoft/BotFramework-WebChat/issues/4841). Added link definitions UI in Markdown, by [@compulim](https://github.com/compulim), in PR [#4846](https://github.com/microsoft/BotFramework-WebChat/pull/4846)
2930
- Resolves [#4842](https://github.com/microsoft/BotFramework-WebChat/issues/4842). Added provenance in activity status, by [@compulim](https://github.com/compulim), in PR [#4846](https://github.com/microsoft/BotFramework-WebChat/pull/4846)

packages/component/src/Attachment/Text/private/URLItem.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { onErrorResumeNext } from 'botframework-webchat-core';
21
import React, { memo } from 'react';
32

3+
import extractHostnameWithSubdomain from './extractHostnameWithSubdomain';
44
import ItemBody from './ItemBody';
55

66
type Props = {
@@ -18,11 +18,7 @@ const URLItem = memo(({ identifier, title, url }: Props) => (
1818
rel="noopener noreferrer"
1919
target="_blank"
2020
>
21-
<ItemBody
22-
identifier={identifier}
23-
isExternal={true}
24-
title={title || onErrorResumeNext(() => new URL(url).host) || url}
25-
/>
21+
<ItemBody identifier={identifier} isExternal={true} title={title || extractHostnameWithSubdomain(url)} />
2622
</a>
2723
));
2824

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import extractHostnameWithSubdomain from './extractHostnameWithSubdomain';
2+
3+
test.each([
4+
['https://www.example.com/', 'example.com'],
5+
['https://www.example.com/index.html', 'example.com'],
6+
['HTTPS://WWW.EXAMPLE.COM/index.html', 'example.com'],
7+
['https://www.example.co.jp/index.html', 'example.co.jp'],
8+
['https://www.example.com:443/index.html', 'example.com'],
9+
['http://www.example.com:80/index.html', 'example.com'],
10+
['https://www.support.example.com/index.html', 'support.example.com'],
11+
['https://www.microsoft/index.html', 'microsoft'],
12+
['https://www.例子.com/index.html', 'xn--fsqu00a.com'],
13+
['https://www.example.com:8443/index.html', 'www.example.com:8443'],
14+
['https://www.example.com:8080', 'www.example.com:8080'],
15+
['https://support.example.com/index.html', 'support.example.com'],
16+
['https://www2.example.com/index.html', 'www2.example.com'],
17+
['ftp://www.example.com/', 'ftp://www.example.com/'],
18+
['www/index.html', 'www/index.html']
19+
])('should extract %s into %s', (url, expected) => {
20+
expect(extractHostnameWithSubdomain(url)).toBe(expected);
21+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function extractHostnameWithSubdomain(urlString: string): string {
2+
try {
3+
const { host, port, protocol } = new URL(urlString);
4+
5+
if (protocol === 'http:' || protocol === 'https:') {
6+
if (!port) {
7+
return host.replace(/^www\./iu, '');
8+
}
9+
10+
return host;
11+
}
12+
} catch (error) {
13+
// Intentionally left blank, will return `urlString`.
14+
}
15+
16+
return urlString;
17+
}

0 commit comments

Comments
 (0)