You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchResultEntry.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import * as classNames from 'classnames';
  22. import { Link } from 'react-router';
  23. import { highlightMarks, cutWords, DocumentationEntry } from '../utils';
  24. export interface SearchResult {
  25. exactMatch?: boolean;
  26. highlights: { [field: string]: [number, number][] };
  27. longestTerm: string;
  28. page: DocumentationEntry;
  29. query: string;
  30. }
  31. interface Props {
  32. active: boolean;
  33. result: SearchResult;
  34. }
  35. export default function SearchResultEntry({ active, result }: Props) {
  36. return (
  37. <Link
  38. className={classNames('list-group-item', { active })}
  39. to={'/documentation' + result.page.url}>
  40. <SearchResultTitle result={result} />
  41. <SearchResultText result={result} />
  42. </Link>
  43. );
  44. }
  45. export function SearchResultTitle({ result }: { result: SearchResult }) {
  46. let titleWithMarks: React.ReactNode;
  47. const titleHighlights = result.highlights.title;
  48. if (titleHighlights && titleHighlights.length > 0) {
  49. const { title } = result.page;
  50. const tokens = highlightMarks(
  51. title,
  52. titleHighlights.map(h => ({ from: h[0], to: h[0] + h[1] }))
  53. );
  54. titleWithMarks = <SearchResultTokens tokens={tokens} />;
  55. } else {
  56. titleWithMarks = result.page.title;
  57. }
  58. return (
  59. <h3 className="list-group-item-heading" style={{ fontWeight: 'normal' }}>
  60. {titleWithMarks}
  61. </h3>
  62. );
  63. }
  64. export function SearchResultText({ result }: { result: SearchResult }) {
  65. const textHighlights = result.highlights.text;
  66. const { text } = result.page;
  67. let tokens: {
  68. text: string;
  69. marked: boolean;
  70. }[] = [];
  71. if (result.exactMatch) {
  72. const pageText = result.page.text.toLowerCase();
  73. const highlights: { from: number; to: number }[] = [];
  74. let start = 0;
  75. let index = pageText.indexOf(result.query, start);
  76. let loopCount = 0;
  77. while (index > -1 && loopCount < 10) {
  78. loopCount++;
  79. highlights.push({ from: index, to: index + result.query.length });
  80. start = index + 1;
  81. index = pageText.indexOf(result.query, start);
  82. }
  83. if (highlights.length) {
  84. tokens = highlightMarks(text, highlights);
  85. }
  86. }
  87. if (tokens.length === 0 && textHighlights && textHighlights.length > 0) {
  88. tokens = highlightMarks(text, textHighlights.map(h => ({ from: h[0], to: h[0] + h[1] })));
  89. }
  90. if (tokens.length) {
  91. return (
  92. <div className="note">
  93. <SearchResultTokens tokens={cutWords(tokens)} />
  94. </div>
  95. );
  96. } else {
  97. return null;
  98. }
  99. }
  100. export function SearchResultTokens({
  101. tokens
  102. }: {
  103. tokens: Array<{ text: string; marked: boolean }>;
  104. }) {
  105. return (
  106. <>
  107. {tokens.map((token, index) => (
  108. <React.Fragment key={index}>
  109. {token.marked ? <mark key={index}>{token.text}</mark> : token.text}
  110. </React.Fragment>
  111. ))}
  112. </>
  113. );
  114. }