SearchResultList.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import PageBody from '../Page/PageBody.js';
  4. export default class SearchResultList extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.getHighlightBody = this.getHighlightBody.bind(this);
  8. }
  9. getHighlightBody(body) {
  10. let returnBody = body;
  11. this.props.searchingKeyword.split(' ').forEach((keyword) => {
  12. if (keyword === '') {
  13. return;
  14. }
  15. const k = keyword
  16. .replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
  17. .replace(/(^"|"$)/g, ''); // for phrase (quoted) keyword
  18. const keywordExp = new RegExp(`(${k}(?!(.*?\]|.*?\\)|.*?"|.*?>)))`, 'ig');
  19. returnBody = returnBody.replace(keywordExp, '<em class="highlighted">$&</em>');
  20. });
  21. //console.log(this.props.searchingKeyword, body);
  22. return returnBody;
  23. }
  24. render() {
  25. const resultList = this.props.pages.map((page) => {
  26. const pageBody = this.getHighlightBody(page.revision.body);
  27. return (
  28. <div id={page._id} key={page._id} className="search-result-page">
  29. <h2><a href={page.path}>{page.path}</a></h2>
  30. <div className="wiki">
  31. <PageBody className="hige" page={page} pageBody={pageBody} />
  32. </div>
  33. </div>
  34. );
  35. });
  36. return (
  37. <div>
  38. {resultList}
  39. </div>
  40. );
  41. }
  42. }
  43. SearchResultList.propTypes = {
  44. pages: PropTypes.array.isRequired,
  45. searchingKeyword: PropTypes.string.isRequired,
  46. };
  47. SearchResultList.defaultProps = {
  48. pages: [],
  49. searchingKeyword: '',
  50. };