RevisionRenderer.jsx 4.1 KB

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