Merge pull request #22540 from OSfrog/stack-overflow-search-highlight-fix

fix: Stack Overflow plugin search highlight fix
This commit is contained in:
Fredrik Adelöw
2024-02-22 14:40:35 +01:00
committed by GitHub
4 changed files with 34 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-stack-overflow': patch
---
fix: fix decode issues in title and author fields in `StackOverflowSearchResultListItem`
+1 -1
View File
@@ -10,7 +10,7 @@ import { BackstagePlugin } from '@backstage/core-plugin-api';
import { CardExtensionProps } from '@backstage/plugin-home-react';
import { JSX as JSX_2 } from 'react';
import { default as React_2 } from 'react';
import { ResultHighlight } from '@backstage/plugin-search-common';
import type { ResultHighlight } from '@backstage/plugin-search-common';
import { SearchResultListItemExtensionProps } from '@backstage/plugin-search-react';
// @public
@@ -15,7 +15,6 @@
*/
import React from 'react';
import _unescape from 'lodash/unescape';
import { Link } from '@backstage/core-components';
import {
Divider,
@@ -26,8 +25,9 @@ import {
Chip,
} from '@material-ui/core';
import { useAnalytics } from '@backstage/core-plugin-api';
import { ResultHighlight } from '@backstage/plugin-search-common';
import type { ResultHighlight } from '@backstage/plugin-search-common';
import { HighlightedSearchResultText } from '@backstage/plugin-search-react';
import { decodeHtml } from '../../util';
/**
* Props for {@link StackOverflowSearchResultListItem}
@@ -45,6 +45,7 @@ export const StackOverflowSearchResultListItem = (
props: StackOverflowSearchResultListItemProps,
) => {
const { result, highlight } = props;
const analytics = useAnalytics();
const handleClick = () => {
@@ -69,12 +70,12 @@ export const StackOverflowSearchResultListItem = (
<Link to={result.location} noTrack onClick={handleClick}>
{highlight?.fields?.title ? (
<HighlightedSearchResultText
text={highlight.fields.title}
text={decodeHtml(highlight.fields.title)}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
) : (
_unescape(result.title)
decodeHtml(result.title)
)}
</Link>
}
@@ -83,13 +84,13 @@ export const StackOverflowSearchResultListItem = (
<>
Author:{' '}
<HighlightedSearchResultText
text={highlight.fields.text}
text={decodeHtml(highlight.fields.text)}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
</>
) : (
`Author: ${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;
}