Skip to content

Commit

Permalink
Fix pinterest URLs not embedding when not .com
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaykuber committed Jul 2, 2024
1 parent 1eab7a2 commit 5d9d94d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/block-library/src/embed/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fallback,
getEmbedInfoByProvider,
getMergedAttributesWithPreview,
replaceDomain,
} from './util';
import EmbedControls from './embed-controls';
import { embedContentIcon } from './icons';
Expand Down Expand Up @@ -220,8 +221,20 @@ const EmbedEdit = ( props ) => {
attributes.className
);

let modifiedUrl;
// If the provider is Pinterest, replace the domain in the URL and update local state.
if ( title === 'Pinterest' ) {
modifiedUrl = replaceDomain( url );
setURL( modifiedUrl );
} else {
modifiedUrl = url;
}

setIsEditingURL( false );
setAttributes( { url, className: blockClass } );
setAttributes( {
url: modifiedUrl,
className: blockClass,
} );
} }
value={ url }
cannotEmbed={ cannotEmbed }
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/embed/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,17 @@ export const getMergedAttributesWithPreview = (
),
};
};

/**
* Replaces any domain extension with 'com' in a given URL.
*
* @param {string} url - The URL string to be modified.
* @return {string} - The modified URL string with the domain extension replaced by 'com'.
*/
export function replaceDomain( url ) {
// Create a URL object to easily manipulate the URL parts
const urlObj = new URL( url );
// Replace any domain extension with 'com'
urlObj.hostname = urlObj.hostname.replace( /\.[a-z]{2,}$/, '.com' );
return urlObj.href;
}

0 comments on commit 5d9d94d

Please sign in to comment.