-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathresult.js
72 lines (66 loc) · 1.64 KB
/
result.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* WordPress dependencies.
*/
import { WPElement } from '@wordpress/element';
/**
* Internal dependencies.
*/
import { postTypeLabels } from '../../config';
import { formatDate } from '../../utilities';
import { escapeRegExp } from '../../../utils/helpers';
import Result from '../common/result';
/**
* Search result.
*
* @param {object} props Component props.
* @param {object} props.hit Elasticsearch hit.
* @param {object} props.searchTerm Search term from input search.
* @param {object} props.highlightTag Selected highlight tag.
* @returns {WPElement} Component element.
*/
export default ({ hit, searchTerm, highlightTag }) => {
const {
highlight: { post_content_plain: postContent = [] },
_source: {
meta: { _wc_average_rating: [{ value: averageRating } = {}] = [] },
permalink: url,
post_date: postDate,
post_id: id,
post_type: postType,
price_html: priceHtml,
thumbnail,
},
} = hit;
/**
* Note: highlighting is redone here because the unified highlight type is not supported in ES5
*/
const regex = new RegExp(`\\b(${escapeRegExp(searchTerm)})`, 'gi');
let title;
if (highlightTag === '' || highlightTag === undefined) {
title = hit._source.post_title;
} else {
title = hit._source.post_title.replace(
regex,
(word) => `<${highlightTag}>${word}</${highlightTag}>`,
);
}
const date = postType === 'post' ? formatDate(postDate) : null;
const excerpt = postContent.join('…');
const type = postTypeLabels[postType]?.singular;
return (
<Result
{...{
averageRating,
date,
hit,
excerpt,
id,
priceHtml,
thumbnail,
title,
type,
url,
}}
/>
);
};