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 11 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 @@ -27,6 +27,20 @@ import { ViewerSlot } from './viewer-slot';

import useRichUrlData from './use-rich-url-data';

/**
* 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 ) {
// Dervied from `filterURLForDisplay` in `@wordpress/url`.
return title
.replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' )
.replace( /^www\./i, '' );
}

export default function LinkPreview( {
value,
onEditClick,
Expand Down Expand Up @@ -59,6 +73,9 @@ export default function LinkPreview( {
! isEmptyURL &&
stripHTML( richData?.title || value?.title || displayURL );

const isUrlRedundant =
value?.url && filterTitleForDisplay( displayTitle ) === displayURL;

let icon;

if ( richData?.icon ) {
Expand Down Expand Up @@ -112,7 +129,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 '';
}

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

// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
Expand Down
27 changes: 24 additions & 3 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,12 @@ describe( 'filterURLForDisplay', () => {
expect( url ).toBe( 'wordpress.org' );
url = filterURLForDisplay( 'https://wordpress.org' );
expect( url ).toBe( 'wordpress.org' );
url = filterURLForDisplay( 'file:///folder/file.txt' );
expect( url ).toBe( '/folder/file.txt' );
url = filterURLForDisplay( 'tel:0123456789' );
expect( url ).toBe( '0123456789' );
url = filterURLForDisplay( 'blob:data' );
expect( url ).toBe( 'data' );
} );
it( 'should remove www subdomain', () => {
const url = filterURLForDisplay( 'http://www.wordpress.org' );
Expand Down Expand Up @@ -1030,18 +1036,28 @@ describe( 'filterURLForDisplay', () => {
expect( url ).toBe( 'wordpress.org/ig.jpg' );
} );
it( 'should return ellipsis, upper level pieces url, and filename when the url is long enough but filename is short enough', () => {
const url = filterURLForDisplay(
let url = filterURLForDisplay(
'http://www.wordpress.org/wp-content/uploads/myimage.jpg',
20
);
expect( url ).toBe( '…/uploads/myimage.jpg' );
url = filterURLForDisplay(
'file:///home/user/documents/project/reports/2024/myfile.pdf',
20
);
expect( url ).toBe( '…orts/2024/myfile.pdf' );
} );
it( 'should return filename split by ellipsis plus three characters when filename is long enough', () => {
const url = filterURLForDisplay(
let url = filterURLForDisplay(
'http://www.wordpress.org/wp-content/uploads/superlongtitlewithextension.jpeg',
20
);
expect( url ).toBe( 'superlongti…ion.jpeg' );
url = filterURLForDisplay(
'file:///home/user/documents/project/reports/2024/superlongtitlewithextension.pdf',
20
);
expect( url ).toBe( 'superlongtit…ion.pdf' );
} );
it( 'should remove query arguments', () => {
const url = filterURLForDisplay(
Expand All @@ -1058,11 +1074,16 @@ describe( 'filterURLForDisplay', () => {
expect( url ).toBe( 'wordpress.org/wp-content/url/' );
} );
it( 'should return file split by ellipsis when the file name has multiple periods', () => {
const url = filterURLForDisplay(
let url = filterURLForDisplay(
'http://www.wordpress.org/wp-content/uploads/filename.2020.12.20.png',
20
);
expect( url ).toBe( 'filename.202….20.png' );
url = filterURLForDisplay(
'file:///home/user/documents/project/reports/2024/filename.2020.12.20.png',
20
);
expect( url ).toBe( 'filename.202….20.png' );
} );
} );

Expand Down
Loading