SearchResultList.js 1.3 KB

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