/** * WordPress dependencies */ import { Button, Dashicon, Tooltip } from '@wordpress/components'; import { useDispatch } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { Icon, copySmall, link, seen } from '@wordpress/icons'; /** * Internal dependencies */ import { VerticalDivider } from '../../common/components/vertical-divider/component'; import { LeafIcon } from '../../common/icons/leaf-icon'; import { escapeRegExp } from '../../common/utils/functions'; import { PostListItemMetric, PostListItemProps } from '../../common/utils/post'; /** * Returns a single related post item. * * @since 3.14.0 * * @param {PostListItemProps} props The component's props. */ export const RelatedPostItem = ( { metric, post, postContent }: Readonly ): React.JSX.Element => { const { createNotice } = useDispatch( 'core/notices' ); /** * Checks if a hyperlink is present in the content by using a regular expression. * * @since 3.14.1 * * @param {string} content * @param {string} rawUrl * * @return {boolean} Whether the link is present in the content. */ const isLinkPresentInContent = ( content: string, rawUrl: string ): boolean => { const escapedUrl = escapeRegExp( rawUrl ); const regexPattern = new RegExp( `]*href=["'](http:\/\/|https:\/\/)?.*${ escapedUrl }.*["'][^>]*>`, 'i' ); return regexPattern.test( content ); }; const isLinked = postContent && isLinkPresentInContent( postContent, post.rawUrl ); return (
{ __( 'View on website (opens new tab)', 'wp-parsely' ) } { post.title }
} avgEngagedIcon={ } />
{ isLinked && (
) }
); };