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 8 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 @@ -59,6 +59,22 @@ export default function LinkPreview( {
! isEmptyURL &&
stripHTML( richData?.title || value?.title || displayURL );

/**
* Filters the title for display. Removes the protocol and www prefix.
*
* @param {string} title - The title to be filtered.
*
* @return {string} The filtered title.
*/
function filterTitleForDisplay( title ) {
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved
return title
.replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' )
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved
.replace( /^www\./i, '' );
}

const isUrlRedundant =
value?.url && filterTitleForDisplay( displayTitle ) === displayURL;
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved

let icon;

if ( richData?.icon ) {
Expand Down Expand Up @@ -112,7 +128,7 @@ export default function LinkPreview( {
{ displayTitle }
</Truncate>
</ExternalLink>
{ value?.url && displayTitle !== displayURL && (
{ ! isUrlRedundant && (
<span className="block-editor-link-control__search-item-info">
<Truncate numberOfLines={ 1 }>
{ displayURL }
Expand Down
8 changes: 7 additions & 1 deletion packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
* @return {string} Displayed URL.
*/
export function filterURLForDisplay( url, maxLength = null ) {
if ( ! url ) {
return '';
}
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved

// Remove protocol and www prefixes.
let filteredURL = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved
let filteredURL = url
.replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' )
.replace( /^www\./i, '' );
amitraj2203 marked this conversation as resolved.
Show resolved Hide resolved

// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
Expand Down
Loading