Skip to content

Commit

Permalink
Changed regex for stripDomainPrefixes to handle all protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed May 30, 2024
1 parent 9194b2b commit 2802679
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useCopyToClipboard } from '@wordpress/compose';
import {
filterURLForDisplay,
safeDecodeURI,
removeProtocol,
stripDomainPrefixes,
} from '@wordpress/url';
import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
Expand Down Expand Up @@ -64,7 +64,7 @@ export default function LinkPreview( {
stripHTML( richData?.title || value?.title || displayURL );

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

let icon;

Expand Down
24 changes: 12 additions & 12 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,6 @@ _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 Expand Up @@ -555,6 +543,18 @@ _Returns_

- `string`: Decoded URI component if possible.

### stripDomainPrefixes

Removes domain prefixes from a URL.

_Parameters_

- _url_ `string`: The URL to strip domain prefixes from.

_Returns_

- `string`: The URL without domain prefixes.

<!-- END TOKEN(Autogenerated API docs) -->

## Contributing to this package
Expand Down
17 changes: 11 additions & 6 deletions 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 = removeProtocol( url );
let filteredURL = stripDomainPrefixes( url );

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

/**
* Removes the protocol from a URL.
* Removes domain prefixes from a URL.
*
* @param {string} url - The URL to remove the protocol from.
* @param {string} url The URL to strip domain prefixes from.
*
* @return {string} The URL without the protocol.
* @return {string} The URL without domain prefixes.
*/
export function removeProtocol( url ) {
return url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
export function stripDomainPrefixes( url ) {
if ( ! url ) {
return '';
}
return url
.replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' )
.replace( /^www\./i, '' );
}
2 changes: 1 addition & 1 deletion packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +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';
export { stripDomainPrefixes } from './filter-url-for-display';

0 comments on commit 2802679

Please sign in to comment.