File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed
components/content-search Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change 22import type { WP_REST_API_User , WP_REST_API_Search_Result } from 'wp-types' ;
33import apiFetch from '@wordpress/api-fetch' ;
44import { addQueryArgs } from '@wordpress/url' ;
5+ import { decodeEntities } from '@wordpress/html-entities' ;
56import type { ContentSearchMode , QueryFilter } from './types' ;
67
78interface IdentifiableObject extends Object {
@@ -83,6 +84,23 @@ interface NormalizeResultsArgs {
8384 excludeItems : Array < IdentifiableObject > ;
8485}
8586
87+ /**
88+ * Convert a WP "rendered" title (which can contain HTML + entities) into plain text.
89+ * - Strips any HTML tags.
90+ * - Decodes HTML entities.
91+ * - Normalizes NBSP and trims.
92+ */
93+ export const toPlainTextTitle = ( input : string | undefined | null ) : string => {
94+ if ( ! input ) {
95+ return '' ;
96+ }
97+
98+ const doc = new DOMParser ( ) . parseFromString ( String ( input ) , 'text/html' ) ;
99+ const text = doc . body . textContent ?? '' ;
100+
101+ return decodeEntities ( text ) . replace ( / \u00A0 / g, ' ' ) . trim ( ) ;
102+ } ;
103+
86104/*
87105 * Depending on the mode value, this method normalizes the format
88106 * of the result array.
You can’t perform that action at this time.
0 commit comments