refactor: removed he and added a util to decode html

Signed-off-by: Tommy Le <tommy_8786@hotmail.com>
This commit is contained in:
Tommy Le
2024-02-13 13:25:12 +01:00
parent e3414a5d9d
commit 30fadd61da
3 changed files with 27 additions and 11 deletions
-2
View File
@@ -54,7 +54,6 @@
"@testing-library/jest-dom": "^6.0.0",
"@types/react": "^16.13.1 || ^17.0.0 || ^18.0.0",
"cross-fetch": "^4.0.0",
"he": "^1.2.0",
"lodash": "^4.17.21",
"qs": "^6.9.4",
"react-use": "^17.2.4"
@@ -70,7 +69,6 @@
"@testing-library/dom": "^9.0.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.0.0",
"@types/he": "^1.2.3",
"msw": "^1.0.0"
},
"files": [
@@ -27,7 +27,7 @@ import {
import { useAnalytics } from '@backstage/core-plugin-api';
import type { ResultHighlight } from '@backstage/plugin-search-common';
import { HighlightedSearchResultText } from '@backstage/plugin-search-react';
import { decode } from 'he';
import { decodeHtml } from '../../util';
/**
* Props for {@link StackOverflowSearchResultListItem}
@@ -45,13 +45,10 @@ export const StackOverflowSearchResultListItem = (
props: StackOverflowSearchResultListItemProps,
) => {
const { result, highlight } = props;
const analytics = useAnalytics();
const handleClick = () => {
if (!result) {
return;
}
analytics.captureEvent('discover', result.title, {
attributes: { to: result.location },
value: props.rank,
@@ -73,12 +70,12 @@ export const StackOverflowSearchResultListItem = (
<Link to={result.location} noTrack onClick={handleClick}>
{highlight?.fields?.title ? (
<HighlightedSearchResultText
text={decode(highlight.fields.title)}
text={decodeHtml(highlight.fields.title)}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
) : (
decode(result.title)
decodeHtml(result.title)
)}
</Link>
}
@@ -87,13 +84,13 @@ export const StackOverflowSearchResultListItem = (
<>
Author:{' '}
<HighlightedSearchResultText
text={decode(highlight.fields.text)}
text={decodeHtml(highlight.fields.text)}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
</>
) : (
`Author: ${decode(result.text)}`
`Author: ${decodeHtml(result.text)}`
)
}
/>
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function decodeHtml(input: string) {
const textContainer = document.createElement('textarea');
textContainer.innerHTML = input;
return textContainer.value;
}