RevisionRenderer.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withUnstatedContainers } from '../UnstatedUtils';
  4. import AppContainer from '../../services/AppContainer';
  5. import PageContainer from '../../services/PageContainer';
  6. import NavigationContainer from '../../services/NavigationContainer';
  7. import GrowiRenderer from '../../util/GrowiRenderer';
  8. import RevisionBody from './RevisionBody';
  9. const WIKI_HEADER_LINK = 120;
  10. class RevisionRenderer extends React.PureComponent {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. html: '',
  15. };
  16. this.renderHtml = this.renderHtml.bind(this);
  17. this.getHighlightedBody = this.getHighlightedBody.bind(this);
  18. }
  19. initCurrentRenderingContext() {
  20. this.currentRenderingContext = {
  21. markdown: this.props.markdown,
  22. currentPagePath: this.props.pageContainer.state.path,
  23. };
  24. }
  25. componentDidMount() {
  26. this.initCurrentRenderingContext();
  27. this.renderHtml();
  28. }
  29. componentDidUpdate(prevProps) {
  30. const { markdown: prevMarkdown, highlightKeywords: prevHighlightKeywords } = prevProps;
  31. const { markdown, highlightKeywords } = this.props;
  32. // render only when props.markdown is updated
  33. if (markdown !== prevMarkdown || highlightKeywords !== prevHighlightKeywords) {
  34. this.initCurrentRenderingContext();
  35. this.renderHtml();
  36. return;
  37. }
  38. const HeaderLink = document.getElementsByClassName('revision-head-link');
  39. const HeaderLinkArray = Array.from(HeaderLink);
  40. HeaderLinkArray.forEach(link => link.addEventListener('click', (e) => {
  41. e.preventDefault();
  42. this.props.navigationContainer.smoothScrollIntoView(link, WIKI_HEADER_LINK);
  43. }));
  44. const { interceptorManager } = this.props.appContainer;
  45. interceptorManager.process('postRenderHtml', this.currentRenderingContext);
  46. }
  47. /**
  48. * transplanted from legacy code -- Yuki Takei
  49. * @param {string} body html strings
  50. * @param {string} keywords
  51. */
  52. getHighlightedBody(body, keywords) {
  53. let returnBody = body;
  54. keywords.replace(/"/g, '').split(' ').forEach((keyword) => {
  55. if (keyword === '') {
  56. return;
  57. }
  58. const k = keyword
  59. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  60. .replace(/(^"|"$)/g, ''); // for phrase (quoted) keyword
  61. const keywordExp = new RegExp(`(${k}(?!(.*?")))`, 'ig');
  62. returnBody = returnBody.replace(keywordExp, '<em class="highlighted">$&</em>');
  63. });
  64. return returnBody;
  65. }
  66. async renderHtml() {
  67. const {
  68. appContainer, growiRenderer,
  69. highlightKeywords,
  70. } = this.props;
  71. const { interceptorManager } = appContainer;
  72. const context = this.currentRenderingContext;
  73. await interceptorManager.process('preRender', context);
  74. await interceptorManager.process('prePreProcess', context);
  75. context.markdown = growiRenderer.preProcess(context.markdown);
  76. await interceptorManager.process('postPreProcess', context);
  77. context.parsedHTML = growiRenderer.process(context.markdown);
  78. await interceptorManager.process('prePostProcess', context);
  79. context.parsedHTML = growiRenderer.postProcess(context.parsedHTML);
  80. if (this.props.highlightKeywords != null) {
  81. context.parsedHTML = this.getHighlightedBody(context.parsedHTML, highlightKeywords);
  82. }
  83. await interceptorManager.process('postPostProcess', context);
  84. await interceptorManager.process('preRenderHtml', context);
  85. this.setState({ html: context.parsedHTML });
  86. }
  87. render() {
  88. const config = this.props.appContainer.getConfig();
  89. const isMathJaxEnabled = !!config.env.MATHJAX;
  90. return (
  91. <RevisionBody
  92. html={this.state.html}
  93. isMathJaxEnabled={isMathJaxEnabled}
  94. renderMathJaxOnInit
  95. />
  96. );
  97. }
  98. }
  99. /**
  100. * Wrapper component for using unstated
  101. */
  102. const RevisionRendererWrapper = withUnstatedContainers(RevisionRenderer, [AppContainer, PageContainer, NavigationContainer]);
  103. RevisionRenderer.propTypes = {
  104. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  105. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  106. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  107. growiRenderer: PropTypes.instanceOf(GrowiRenderer).isRequired,
  108. markdown: PropTypes.string.isRequired,
  109. highlightKeywords: PropTypes.string,
  110. };
  111. export default RevisionRendererWrapper;