Skip to content

Commit d551eb2

Browse files
authored
Merge pull request #401 from 10up/feature/raw-titles-content-picker
Fix: Raw Titles for Content Picker
2 parents fa1cca6 + 9282ed4 commit d551eb2

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

components/content-picker/SortableList.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { Post, User, store as coreStore } from '@wordpress/core-data';
3030
*/
3131
import PickedItem, { PickedItemType } from './PickedItem';
3232
import { DraggableChip } from './DraggableChip';
33+
import { toPlainTextTitle } from '../content-search/utils';
3334
import { ContentSearchMode, QueryFieldsFilter } from '../content-search/types';
3435
import type { PickedItemFilter } from './index';
3536
import { Term } from './types';
@@ -135,7 +136,7 @@ const SortableList: React.FC<SortableListProps> = ({
135136
case 'post': {
136137
const post = result as Post;
137138
newItem = {
138-
title: post.title.rendered,
139+
title: toPlainTextTitle(post.title.rendered),
139140
url: post.link,
140141
id: post.id,
141142
type: post.type,
@@ -146,7 +147,7 @@ const SortableList: React.FC<SortableListProps> = ({
146147
case 'user': {
147148
const user = result as User;
148149
newItem = {
149-
title: user.name,
150+
title: toPlainTextTitle(user.name),
150151
url: user.link,
151152
id: user.id,
152153
type: 'user',
@@ -156,7 +157,7 @@ const SortableList: React.FC<SortableListProps> = ({
156157
default: {
157158
const taxonomy = result as Term;
158159
newItem = {
159-
title: taxonomy.name,
160+
title: toPlainTextTitle(taxonomy.name),
160161
url: taxonomy.link,
161162
id: taxonomy.id,
162163
type: taxonomy.taxonomy,

components/content-search/utils.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { WP_REST_API_User, WP_REST_API_Search_Result } from 'wp-types';
99
*/
1010
import apiFetch from '@wordpress/api-fetch';
1111
import { addQueryArgs } from '@wordpress/url';
12+
import { decodeEntities } from '@wordpress/html-entities';
1213

1314
/**
1415
* Types
@@ -114,6 +115,23 @@ interface NormalizeResultsArgs {
114115
searchResultFilter?: SearchResultFilter;
115116
}
116117

118+
/**
119+
* Convert a WP "rendered" title (which can contain HTML + entities) into plain text.
120+
* - Strips any HTML tags.
121+
* - Decodes HTML entities.
122+
* - Normalizes NBSP and trims.
123+
*/
124+
export const toPlainTextTitle = (input: string | undefined | null): string => {
125+
if (!input) {
126+
return '';
127+
}
128+
129+
const doc = new DOMParser().parseFromString(String(input), 'text/html');
130+
const text = doc.body.textContent ?? '';
131+
132+
return decodeEntities(text).replace(/\u00A0/g, ' ').trim();
133+
};
134+
117135
/*
118136
* Depending on the mode value, this method normalizes the format
119137
* of the result array.
@@ -148,7 +166,7 @@ export const normalizeResults = ({
148166
newItem = {
149167
id: userItem.id,
150168
subtype: mode,
151-
title: userItem.name,
169+
title: toPlainTextTitle(userItem.name),
152170
type: mode,
153171
url: userItem.link,
154172
};
@@ -158,7 +176,7 @@ export const normalizeResults = ({
158176
newItem = {
159177
id: searchItem.id as number,
160178
subtype: searchItem.subtype,
161-
title: searchItem.title,
179+
title: toPlainTextTitle(searchItem.title),
162180
type: searchItem.type,
163181
url: searchItem.url,
164182
};

0 commit comments

Comments
 (0)