From 5211dd8d918cfa63cdd47f559cbba549460a16bb Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Fri, 26 Jan 2024 15:44:30 +0100 Subject: [PATCH 1/5] fix: fix decoding issues in StackOverflowSearchResultListItem Signed-off-by: Tommy Le --- plugins/stack-overflow/package.json | 2 ++ .../StackOverflowSearchResultListItem.tsx | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/stack-overflow/package.json b/plugins/stack-overflow/package.json index de0aec1e61..5364ee59bb 100644 --- a/plugins/stack-overflow/package.json +++ b/plugins/stack-overflow/package.json @@ -54,6 +54,7 @@ "@testing-library/jest-dom": "^6.0.0", "@types/react": "^16.13.1 || ^17.0.0", "cross-fetch": "^4.0.0", + "he": "^1.2.0", "lodash": "^4.17.21", "qs": "^6.9.4", "react-use": "^17.2.4" @@ -69,6 +70,7 @@ "@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": [ diff --git a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx index 245666deeb..9e98d6612f 100644 --- a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx +++ b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx @@ -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 { decode } from 'he'; /** * Props for {@link StackOverflowSearchResultListItem} @@ -48,6 +48,10 @@ export const StackOverflowSearchResultListItem = ( const analytics = useAnalytics(); const handleClick = () => { + if (!result) { + return; + } + analytics.captureEvent('discover', result.title, { attributes: { to: result.location }, value: props.rank, @@ -69,12 +73,12 @@ export const StackOverflowSearchResultListItem = ( {highlight?.fields?.title ? ( ) : ( - _unescape(result.title) + decode(result.title) )} } @@ -83,13 +87,13 @@ export const StackOverflowSearchResultListItem = ( <> Author:{' '} ) : ( - `Author: ${result.text}` + `Author: ${decode(result.text)}` ) } /> From c6779aca2da8103128a257afc18e1853f3797e90 Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Fri, 26 Jan 2024 15:49:18 +0100 Subject: [PATCH 2/5] chore: add changeset Signed-off-by: Tommy Le --- .changeset/dry-impalas-serve.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dry-impalas-serve.md diff --git a/.changeset/dry-impalas-serve.md b/.changeset/dry-impalas-serve.md new file mode 100644 index 0000000000..709c0fb498 --- /dev/null +++ b/.changeset/dry-impalas-serve.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-stack-overflow': patch +--- + +fix: fix decode issues in title and author fields in StackOverflowSearchResultListItem From 30fadd61daf864217304fe0a203f15af4d78a3fd Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Tue, 13 Feb 2024 13:25:12 +0100 Subject: [PATCH 3/5] refactor: removed he and added a util to decode html Signed-off-by: Tommy Le --- plugins/stack-overflow/package.json | 2 -- .../StackOverflowSearchResultListItem.tsx | 15 ++++++------- plugins/stack-overflow/src/util.ts | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 plugins/stack-overflow/src/util.ts diff --git a/plugins/stack-overflow/package.json b/plugins/stack-overflow/package.json index 73498e2a53..c1a4f8f40b 100644 --- a/plugins/stack-overflow/package.json +++ b/plugins/stack-overflow/package.json @@ -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": [ diff --git a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx index 9e98d6612f..70e182b86d 100644 --- a/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx +++ b/plugins/stack-overflow/src/search/StackOverflowSearchResultListItem/StackOverflowSearchResultListItem.tsx @@ -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 = ( {highlight?.fields?.title ? ( ) : ( - decode(result.title) + decodeHtml(result.title) )} } @@ -87,13 +84,13 @@ export const StackOverflowSearchResultListItem = ( <> Author:{' '} ) : ( - `Author: ${decode(result.text)}` + `Author: ${decodeHtml(result.text)}` ) } /> diff --git a/plugins/stack-overflow/src/util.ts b/plugins/stack-overflow/src/util.ts new file mode 100644 index 0000000000..a40ae905ea --- /dev/null +++ b/plugins/stack-overflow/src/util.ts @@ -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; +} From 6faf825b662a6e9a9a64d22d9fe55fec81bbe1f8 Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Tue, 13 Feb 2024 13:50:35 +0100 Subject: [PATCH 4/5] chore: update api-report Signed-off-by: Tommy Le --- plugins/stack-overflow/api-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stack-overflow/api-report.md b/plugins/stack-overflow/api-report.md index 293c2ac226..7593db530f 100644 --- a/plugins/stack-overflow/api-report.md +++ b/plugins/stack-overflow/api-report.md @@ -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 From 72d705a069a5a0d1e351f4a58b812c2fd3c39f01 Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Tue, 20 Feb 2024 17:30:36 +0100 Subject: [PATCH 5/5] chore: update changeset Signed-off-by: Tommy Le --- .changeset/dry-impalas-serve.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/dry-impalas-serve.md b/.changeset/dry-impalas-serve.md index 709c0fb498..3b1f502b9c 100644 --- a/.changeset/dry-impalas-serve.md +++ b/.changeset/dry-impalas-serve.md @@ -2,4 +2,4 @@ '@backstage/plugin-stack-overflow': patch --- -fix: fix decode issues in title and author fields in StackOverflowSearchResultListItem +fix: fix decode issues in title and author fields in `StackOverflowSearchResultListItem`