SearchResultList.js 1.5 KB

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