diff options
author | Marcin Aman <maman@virtuslab.com> | 2020-07-29 16:51:40 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-08-03 13:47:51 +0200 |
commit | 6e6876e2e9576f1ccd2816bab5c1a5ea7e23409f (patch) | |
tree | 462289b6afd9c536c4f93b391113dbbeb6d0dc49 | |
parent | d0f707af0199a99085dc40693628acbf7e543abe (diff) | |
download | dokka-6e6876e2e9576f1ccd2816bab5c1a5ea7e23409f.tar.gz dokka-6e6876e2e9576f1ccd2816bab5c1a5ea7e23409f.tar.bz2 dokka-6e6876e2e9576f1ccd2816bab5c1a5ea7e23409f.zip |
Improve sorting algorithm
-rw-r--r-- | plugins/base/frontend/src/main/components/search/search.tsx | 49 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt | 3 |
2 files changed, 40 insertions, 12 deletions
diff --git a/plugins/base/frontend/src/main/components/search/search.tsx b/plugins/base/frontend/src/main/components/search/search.tsx index 14bf3c24..b4c5b1cd 100644 --- a/plugins/base/frontend/src/main/components/search/search.tsx +++ b/plugins/base/frontend/src/main/components/search/search.tsx @@ -5,9 +5,14 @@ import '@jetbrains/ring-ui/components/input-size/input-size.scss'; import './search.scss'; import {IWindow, Option, Props, Page} from "./types"; +enum SearchRank { + SearchKeyMatch = 1, + NameMatch = 0 +} type OptionWithSearchResult = Option & { matched: boolean, - highlight: string + highlight: string, + rank: SearchRank } type OptionWithHighlightComponent = Option & { @@ -15,17 +20,21 @@ type OptionWithHighlightComponent = Option & { } type SearchProps = { - page: Option, - label: string + searchResult: OptionWithSearchResult, } const orderRecords = (records: OptionWithSearchResult[], searchPhrase: string): OptionWithSearchResult[] => { return records.sort((a: OptionWithSearchResult, b: OptionWithSearchResult) => { + //Prefer higher rank + const byRank = b.rank - a.rank + if(byRank !== 0){ + return byRank + } //Prefer exact matches const aIncludes = a.name.toLowerCase().includes(searchPhrase.toLowerCase()) ? 1 : 0 const bIncludes = b.name.toLowerCase().includes(searchPhrase.toLowerCase()) ? 1 : 0 const byIncludes = bIncludes - aIncludes - if(byIncludes != 0 && aIncludes == 1){ + if(byIncludes != 0){ return byIncludes } @@ -38,15 +47,24 @@ const orderRecords = (records: OptionWithSearchResult[], searchPhrase: string): }) } -const SearchResultRow: React.FC<SearchProps> = ({label, page}: SearchProps) => { - const withSignature = page.name.replace(page.searchKey, label) +const SearchResultRow: React.FC<SearchProps> = ({searchResult}: SearchProps) => { + const signatureFromSearchResult = (searchResult: OptionWithSearchResult): string => { + if(searchResult.rank == SearchRank.SearchKeyMatch){ + return searchResult.name.replace(searchResult.searchKey, searchResult.highlight) + } + return searchResult.highlight + } + + const renderHighlightMarkersAsHtml = (record: string): string => { + return record.replace(/\*\*(.*?)\*\*/g, '<span class="phraseHighlight">$1</span>') + } return ( <div className="template-wrapper"> <span dangerouslySetInnerHTML={ - {__html: withSignature.replace(/\*\*(.*?)\*\*/g, '<span class="phraseHighlight">$1</span>') } + {__html: renderHighlightMarkersAsHtml(signatureFromSearchResult(searchResult)) } }/> - <span className="template-description">{page.description}</span> + <span className="template-description">{searchResult.description}</span> </div> ) } @@ -56,7 +74,7 @@ const highlightMatchedPhrases = (records: OptionWithSearchResult[]): OptionWithH return records.map(record => { return { ...record, - template: <SearchResultRow label={record.highlight} page={record}/> + template: <SearchResultRow searchResult={record}/> } }) } @@ -65,9 +83,18 @@ class DokkaFuzzyFilterComponent extends Select { getListItems(rawFilterString: string, _: Option[]) { const matchedRecords = this.props.data .map((record: Option) => { + const bySearchKey = fuzzyHighlight(rawFilterString.trim(), record.searchKey, true) + if(bySearchKey.matched){ + return { + ...bySearchKey, + ...record, + rank: SearchRank.SearchKeyMatch + } + } return { - ...fuzzyHighlight(rawFilterString.trim(), record.searchKey), - ...record + ...fuzzyHighlight(rawFilterString.trim(), record.name), + ...record, + rank: SearchRank.NameMatch } }) .filter((record: OptionWithSearchResult) => record.matched) diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 548da21c..88d2539d 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -20,6 +20,7 @@ import org.jetbrains.dokka.plugability.query import org.jetbrains.dokka.plugability.querySingle import java.io.File import java.net.URI +import java.util.concurrent.ConcurrentHashMap open class HtmlRenderer( context: DokkaContext @@ -33,7 +34,7 @@ open class HtmlRenderer( sourceSetDependencyMap.size > 1 } - private val pageList = mutableMapOf<String, Pair<String, String>>() + private val pageList = ConcurrentHashMap<String, Pair<String, String>>() override val preprocessors = context.plugin<DokkaBase>().query { htmlPreprocessors } |