From 37d0cb271a74ab6c5c1b6f1859353c007810378e Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Fri, 14 Jun 2019 10:07:55 +0200 Subject: [PATCH] Small performance improvement on documentation search --- server/sonar-docs/src/components/Search.tsx | 16 ++++++++-------- .../documentation/components/SearchResults.tsx | 10 ++++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/server/sonar-docs/src/components/Search.tsx b/server/sonar-docs/src/components/Search.tsx index d798a2b5ad2..a4a24c40eee 100644 --- a/server/sonar-docs/src/components/Search.tsx +++ b/server/sonar-docs/src/components/Search.tsx @@ -73,6 +73,13 @@ export default class Search extends React.PureComponent { return undefined; } + const searchResultPage: SearchResult['page'] = { + id: page.id, + text: (page.html || '').replace(/<(?:.|\n)*?>/gm, '').replace(/<(?:.|\n)*?>/gm, ''), + title: getMarkdownRemarkTitle(page) || '', + url: getMarkdownRemarkUrl(page) || '' + }; + const highlights: { [field: string]: [number, number][] } = {}; let longestTerm = ''; let exactMatch = false; @@ -103,14 +110,7 @@ export default class Search extends React.PureComponent { }); return { - page: { - id: page.id, - text: (page.html || '') - .replace(/<(?:.|\n)*?>/gm, '') - .replace(/<(?:.|\n)*?>/gm, ''), - title: getMarkdownRemarkTitle(page) || '', - url: getMarkdownRemarkUrl(page) || '' - }, + page: searchResultPage, exactMatch, highlights, query, diff --git a/server/sonar-web/src/main/js/apps/documentation/components/SearchResults.tsx b/server/sonar-web/src/main/js/apps/documentation/components/SearchResults.tsx index 909a80e9123..644a406918e 100644 --- a/server/sonar-web/src/main/js/apps/documentation/components/SearchResults.tsx +++ b/server/sonar-web/src/main/js/apps/documentation/components/SearchResults.tsx @@ -21,9 +21,10 @@ import * as React from 'react'; import lunr, { LunrBuilder, LunrIndex, LunrToken } from 'lunr'; import { sortBy } from 'lodash'; import { DocNavigationItem } from 'Docs/@types/types'; -import SearchResultEntry, { SearchResult } from './SearchResultEntry'; +import SearchResultEntry from './SearchResultEntry'; import { getUrlsList } from '../navTreeUtils'; import { DocumentationEntry } from '../utils'; +import { isDefined } from '../../../helpers/types'; interface Props { navigation: DocNavigationItem[]; @@ -62,6 +63,11 @@ export default class SearchResults extends React.PureComponent { ) .map(match => { const page = this.props.pages.find(page => page.relativeName === match.ref); + + if (!page) { + return null; + } + const highlights: T.Dict<[number, number][]> = {}; let longestTerm = ''; let exactMatch = false; @@ -93,7 +99,7 @@ export default class SearchResults extends React.PureComponent { return { exactMatch, highlights, longestTerm, page, query }; }) - .filter(result => result.page) as SearchResult[]; + .filter(isDefined); // Re-order results by the length of the longest matched term and by exact // match (if applicable). The longer the matched term is, the higher the -- 2.39.5