Skip to content
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

LinkControl: refined the display of the link preview title and url when both are same #61819

Merged
merged 16 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
__experimentalTruncate as Truncate,
} from '@wordpress/components';
import { useCopyToClipboard } from '@wordpress/compose';
import { filterURLForDisplay, safeDecodeURI } from '@wordpress/url';
import {
filterURLForDisplay,
safeDecodeURI,
removeProtocol,
} from '@wordpress/url';
import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { useDispatch, useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -59,6 +63,9 @@ export default function LinkPreview( {
! isEmptyURL &&
stripHTML( richData?.title || value?.title || displayURL );

const isTitleRedundant =
value?.url && removeProtocol( displayTitle ) === displayURL;

let icon;

if ( richData?.icon ) {
Expand Down Expand Up @@ -112,7 +119,7 @@ export default function LinkPreview( {
{ displayTitle }
</Truncate>
</ExternalLink>
{ value?.url && displayTitle !== displayURL && (
{ ! isTitleRedundant && (
<span className="block-editor-link-control__search-item-info">
<Truncate numberOfLines={ 1 }>
{ displayURL }
Expand Down
12 changes: 12 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ _Returns_

- `string`: The updated URL.

### removeProtocol

Removes the protocol from a URL.

_Parameters_

- _url_ `string`: - The URL to remove the protocol from.

_Returns_

- `string`: The URL without the protocol.

### removeQueryArgs

Removes arguments from the query string of the url
Expand Down
13 changes: 12 additions & 1 deletion packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
export function filterURLForDisplay( url, maxLength = null ) {
// Remove protocol and www prefixes.
let filteredURL = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
let filteredURL = removeProtocol( url );

// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
Expand Down Expand Up @@ -53,3 +53,14 @@ export function filterURLForDisplay( url, maxLength = null ) {
truncatedFile
);
}

/**
* Removes the protocol from a URL.
*
* @param {string} url - The URL to remove the protocol from.
*
* @return {string} The URL without the protocol.
*/
export function removeProtocol( url ) {
return url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
}
1 change: 1 addition & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { cleanForSlug } from './clean-for-slug';
export { getFilename } from './get-filename';
export { normalizePath } from './normalize-path';
export { prependHTTPS } from './prepend-https';
export { removeProtocol } from './filter-url-for-display';
Loading