SearchResultList.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. const k = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  12. const keywordExp = new RegExp(`(${k}(?!(.*?\]|.*?\\)|.*?"|.*?>)))`, 'ig');
  13. returnBody = returnBody.replace(keywordExp, '<em class="highlighted">$&</em>');
  14. });
  15. //console.log(this.props.searchingKeyword, body);
  16. return returnBody;
  17. }
  18. render() {
  19. const resultList = this.props.pages.map((page) => {
  20. const pageBody = this.getHighlightBody(page.revision.body);
  21. return (
  22. <div id={page._id} key={page._id} className="search-result-page">
  23. <h2><a href={page.path}>{page.path}</a></h2>
  24. <div className="wiki">
  25. <PageBody className="hige" page={page} pageBody={pageBody} />
  26. </div>
  27. </div>
  28. );
  29. });
  30. return (
  31. <div>
  32. {resultList}
  33. </div>
  34. );
  35. }
  36. }
  37. SearchResultList.propTypes = {
  38. pages: React.PropTypes.array.isRequired,
  39. searchingKeyword: React.PropTypes.string.isRequired,
  40. };
  41. SearchResultList.defaultProps = {
  42. pages: [],
  43. searchingKeyword: '',
  44. };