Skip to content

Commit 9851213

Browse files
committed
Remove code blocks from breadcrumb settings for post type or tax in search index
1 parent cd741f1 commit 9851213

2 files changed

Lines changed: 45 additions & 21 deletions

File tree

packages/js/src/settings/components/search.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable complexity */
22
import { Combobox } from "@headlessui/react";
33
import { SearchIcon } from "@heroicons/react/outline";
4-
import { useCallback, useRef, useState } from "@wordpress/element";
4+
import { useCallback, useRef, useState, useMemo } from "@wordpress/element";
55
import { __ } from "@wordpress/i18n";
6-
import { Modal, Title, useSvgAria, useToggleState } from "@yoast/ui-library";
6+
import { Modal, Title, useSvgAria, useToggleState, Code } from "@yoast/ui-library";
77
import classNames from "classnames";
88
import { debounce, first, groupBy, includes, isEmpty, map, max, reduce, split, trim, values } from "lodash";
99
import PropTypes from "prop-types";
@@ -13,6 +13,42 @@ import { safeToLocaleLower } from "../helpers";
1313
import { useParsedUserAgent, useSelectSettings } from "../hooks";
1414

1515
const QUERY_MIN_CHARS = 3;
16+
const POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP = new RegExp( /^input-wpseo_titles-(post_types|taxonomy)-(?<name>\S+)-(maintax|ptparent)$/is );
17+
18+
/**
19+
* @param {string} fieldId The item field ID.
20+
* @param {string} fieldLabel The item label.
21+
* @returns {JSX.Element} The SearchResultLabel element.
22+
*/
23+
const SearchResultLabel = ( { fieldId, fieldLabel } ) => {
24+
// Deduce wether field is a breadcrumb option for post type or taxonomy.
25+
const { isPostTypeOrTaxonomyBreadcrumbSetting, postTypeOrTaxonomyName } = useMemo( () => {
26+
const matches = POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP.exec( fieldId );
27+
return {
28+
isPostTypeOrTaxonomyBreadcrumbSetting: Boolean( matches ),
29+
postTypeOrTaxonomyName: matches?.groups?.name,
30+
};
31+
}, [ fieldId, POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP ] );
32+
33+
// Render additional code block with post type or taxonomy name if applicable.
34+
if ( isPostTypeOrTaxonomyBreadcrumbSetting ) {
35+
return (
36+
<>
37+
{ fieldLabel }
38+
{ postTypeOrTaxonomyName && (
39+
<Code className="yst-ml-2 group-hover:yst-bg-primary-200 group-hover:yst-text-primary-800">{ postTypeOrTaxonomyName }</Code>
40+
) }
41+
</>
42+
);
43+
}
44+
45+
return fieldLabel;
46+
};
47+
48+
SearchResultLabel.propTypes = {
49+
fieldId: PropTypes.string.isRequired,
50+
fieldLabel: PropTypes.string.isRequired,
51+
};
1652

1753
/**
1854
* @param {string} title The title.
@@ -176,13 +212,13 @@ const Search = () => {
176212
<li key={ groupedItems?.[ 0 ]?.route || `group-${ index }` }>
177213
<Title as="h4" size="3" className="yst-bg-slate-100 yst-py-3 yst-px-4">{ first( groupedItems ).routeLabel }</Title>
178214
<ul>
179-
{ map( groupedItems, ( item, name ) => (
215+
{ map( groupedItems, ( item ) => (
180216
<Combobox.Option
181-
key={ name }
217+
key={ item.fieldId }
182218
value={ item }
183219
className={ handleOptionActiveState }
184220
>
185-
{ item.fieldLabel }
221+
<SearchResultLabel { ...item } />
186222
</Combobox.Option>
187223
) ) }
188224
</ul>

packages/js/src/settings/helpers/search.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable camelcase */
22
import { __, sprintf } from "@wordpress/i18n";
3-
import { Code } from "@yoast/ui-library";
4-
import { createInterpolateElement } from "@wordpress/element";
53
import { omit, reduce, times, filter, includes, isEmpty } from "lodash";
64
import { safeToLocaleLower } from "./i18n";
75

@@ -756,13 +754,8 @@ export const createSearchIndex = ( postTypes, taxonomies, { userLocale } = {} )
756754
route: "/breadcrumbs",
757755
routeLabel: __( "Breadcrumbs", "wordpress-seo" ),
758756
fieldId: `input-wpseo_titles-post_types-${ postType.name }-maintax`,
759-
fieldLabel: createInterpolateElement(
760-
// translators: %1$s expands to the post type plural, e.g. posts.
761-
sprintf( __( "Breadcrumbs for %1$s<code />", "wordpress-seo" ), safeToLocaleLower( postType.label, userLocale ) ),
762-
{
763-
code: <Code className="yst-ml-2 group-hover:yst-bg-primary-200 group-hover:yst-text-primary-800">{ postType.name }</Code>,
764-
}
765-
),
757+
// translators: %1$s expands to the post type plural, e.g. posts.
758+
fieldLabel: sprintf( __( "Breadcrumbs for %1$s", "wordpress-seo" ), safeToLocaleLower( postType.label, userLocale ) ),
766759
keywords: [],
767760
},
768761
} );
@@ -773,13 +766,8 @@ export const createSearchIndex = ( postTypes, taxonomies, { userLocale } = {} )
773766
route: "/breadcrumbs",
774767
routeLabel: __( "Breadcrumbs", "wordpress-seo" ),
775768
fieldId: `input-wpseo_titles-taxonomy-${ taxonomy.name }-ptparent`,
776-
fieldLabel: createInterpolateElement(
777-
// translators: %1$s expands to the taxonomy plural, e.g. categories.
778-
sprintf( __( "Breadcrumbs for %1$s<code />", "wordpress-seo" ), safeToLocaleLower( taxonomy.label, userLocale ) ),
779-
{
780-
code: <Code className="yst-ml-2 group-hover:yst-bg-primary-200 group-hover:yst-text-primary-800">{ taxonomy.name }</Code>,
781-
}
782-
),
769+
// translators: %1$s expands to the taxonomy plural, e.g. categories.
770+
fieldLabel: sprintf( __( "Breadcrumbs for %1$s", "wordpress-seo" ), safeToLocaleLower( taxonomy.label, userLocale ) ),
783771
keywords: [],
784772
},
785773
} ), {} ),

0 commit comments

Comments
 (0)