RevisionRenderer.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import React from 'react';
  2. import ReactMarkdown from 'react-markdown';
  3. import { blinkElem } from '~/client/util/blink-section-header';
  4. import { addSmoothScrollEvent } from '~/client/util/smooth-scroll';
  5. import { CustomWindow } from '~/interfaces/global';
  6. import { RendererOptions } from '~/services/renderer/renderer';
  7. import { useCurrentPathname, useInterceptorManager } from '~/stores/context';
  8. import { useEditorSettings } from '~/stores/editor';
  9. import { useViewOptions } from '~/stores/renderer';
  10. import loggerFactory from '~/utils/logger';
  11. // import RevisionBody from './RevisionBody';
  12. import katexStyles from '../CommonStyles/katex.module.scss';
  13. const logger = loggerFactory('components:Page:RevisionRenderer');
  14. // function getHighlightedBody(body: string, _keywords: string | string[]): string {
  15. // const normalizedKeywordsArray: string[] = [];
  16. // const keywords = (typeof _keywords === 'string') ? [_keywords] : _keywords;
  17. // if (keywords.length === 0) {
  18. // return body;
  19. // }
  20. // // !!TODO!!: add test code refs: https://redmine.weseek.co.jp/issues/86841
  21. // // Separate keywords
  22. // // - Surrounded by double quotation
  23. // // - Split by both full-width and half-width spaces
  24. // // [...keywords.match(/"[^"]+"|[^\u{20}\u{3000}]+/ug)].forEach((keyword, i) => {
  25. // keywords.forEach((keyword, i) => {
  26. // if (keyword === '') {
  27. // return;
  28. // }
  29. // const k = keyword
  30. // .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // escape regex operators
  31. // .replace(/(^"|"$)/g, ''); // for phrase (quoted) keyword
  32. // normalizedKeywordsArray.push(k);
  33. // });
  34. // const normalizedKeywords = `(${normalizedKeywordsArray.join('|')})`;
  35. // const keywordRegxp = new RegExp(`${normalizedKeywords}(?!(.*?"))`, 'ig'); // prior https://regex101.com/r/oX7dq5/1
  36. // let keywordRegexp2 = keywordRegxp;
  37. // // for non-chrome browsers compatibility
  38. // try {
  39. // eslint-disable-next-line regex/invalid, max-len
  40. // keywordRegexp2 = new RegExp(`(?<!<)${normalizedKeywords}(?!(.*?("|>)))`, 'ig'); // inferior (this doesn't work well when html tags exist a lot) https://regex101.com/r/Dfi61F/1
  41. // }
  42. // catch (err) {
  43. // logger.debug('Failed to initialize regex:', err);
  44. // }
  45. // const highlighter = (str) => { return str.replace(keywordRegxp, '<em class="highlighted-keyword">$&</em>') }; // prior
  46. // const highlighter2 = (str) => { return str.replace(keywordRegexp2, '<em class="highlighted-keyword">$&</em>') }; // inferior
  47. // const insideTagRegex = /<[^<>]*>/g;
  48. // const betweenTagRegex = />([^<>]*)</g; // use (group) to ignore >< around
  49. // const insideTagStrs = body.match(insideTagRegex);
  50. // const betweenTagMatches = Array.from(body.matchAll(betweenTagRegex));
  51. // let returnBody = body;
  52. // const isSafeHtml = insideTagStrs?.length === betweenTagMatches.length + 1; // to check whether is safe to join
  53. // if (isSafeHtml) {
  54. // // highlight
  55. // const betweenTagStrs: string[] = betweenTagMatches.map(match => highlighter(match[1])); // get only grouped part (exclude >< around)
  56. // const arr: string[] = [];
  57. // insideTagStrs.forEach((str, i) => {
  58. // arr.push(str);
  59. // arr.push(betweenTagStrs[i]);
  60. // });
  61. // returnBody = arr.join('');
  62. // }
  63. // else {
  64. // // inferior highlighter
  65. // returnBody = highlighter2(body);
  66. // }
  67. // return returnBody;
  68. // }
  69. type Props = {
  70. rendererOptions: RendererOptions,
  71. markdown: string,
  72. pagePath: string,
  73. highlightKeywords?: string | string[],
  74. additionalClassName?: string,
  75. }
  76. const RevisionRenderer = (props: Props): JSX.Element => {
  77. const {
  78. rendererOptions, markdown, pagePath, highlightKeywords, additionalClassName,
  79. } = props;
  80. return (
  81. <ReactMarkdown
  82. {...rendererOptions}
  83. className={`wiki katex-container ${katexStyles['katex-container']} ${additionalClassName ?? ''}`}
  84. >
  85. {markdown}
  86. </ReactMarkdown>
  87. );
  88. // const [html, setHtml] = useState('');
  89. // const { data: interceptorManager } = useInterceptorManager();
  90. // const { data: editorSettings } = useEditorSettings();
  91. // const { data: currentPathname } = useCurrentPathname();
  92. // const currentRenderingContext = useMemo(() => {
  93. // return {
  94. // markdown,
  95. // parsedHTML: '',
  96. // pagePath,
  97. // renderDrawioInRealtime: editorSettings?.renderDrawioInRealtime,
  98. // currentPathname: decodeURIComponent(currentPathname ?? '/'),
  99. // };
  100. // }, [editorSettings?.renderDrawioInRealtime, markdown, pagePath]);
  101. // const renderHtml = useCallback(async() => {
  102. // if (interceptorManager == null) {
  103. // return;
  104. // }
  105. // const context = currentRenderingContext;
  106. // await interceptorManager.process('preRender', context);
  107. // await interceptorManager.process('prePreProcess', context);
  108. // context.markdown = growiRenderer.preProcess(context.markdown, context);
  109. // await interceptorManager.process('postPreProcess', context);
  110. // context.parsedHTML = growiRenderer.process(context.markdown, context);
  111. // await interceptorManager.process('prePostProcess', context);
  112. // context.parsedHTML = growiRenderer.postProcess(context.parsedHTML, context);
  113. // const isMarkdownEmpty = context.markdown.trim().length === 0;
  114. // if (highlightKeywords != null && !isMarkdownEmpty) {
  115. // context.parsedHTML = getHighlightedBody(context.parsedHTML, highlightKeywords);
  116. // }
  117. // await interceptorManager.process('postPostProcess', context);
  118. // await interceptorManager.process('preRenderHtml', context);
  119. // setHtml(context.parsedHTML);
  120. // }, [currentRenderingContext, growiRenderer, highlightKeywords, interceptorManager]);
  121. // useEffect(() => {
  122. // if (interceptorManager == null) {
  123. // return;
  124. // }
  125. // renderHtml()
  126. // .then(() => {
  127. // // const HeaderLink = document.getElementsByClassName('revision-head-link');
  128. // // const HeaderLinkArray = Array.from(HeaderLink);
  129. // // addSmoothScrollEvent(HeaderLinkArray as HTMLAnchorElement[], blinkElem);
  130. // // interceptorManager.process('postRenderHtml', currentRenderingContext);
  131. // });
  132. // }, [currentRenderingContext, interceptorManager, renderHtml]);
  133. // const config = props.appContainer.getConfig();
  134. // const isMathJaxEnabled = !!config.env.MATHJAX;
  135. // return (
  136. // <RevisionBody
  137. // html={html}
  138. // isMathJaxEnabled={isMathJaxEnabled}
  139. // additionalClassName={props.additionalClassName}
  140. // renderMathJaxOnInit
  141. // />
  142. // );
  143. // const [html, setHtml] = useState('');
  144. // const { data: interceptorManager } = useInterceptorManager();
  145. // const { data: editorSettings } = useEditorSettings();
  146. // const { data: currentPathname } = useCurrentPathname();
  147. // const currentRenderingContext = useMemo(() => {
  148. // return {
  149. // markdown,
  150. // parsedHTML: '',
  151. // pagePath,
  152. // renderDrawioInRealtime: editorSettings?.renderDrawioInRealtime,
  153. // currentPathname: decodeURIComponent(currentPathname ?? '/'),
  154. // };
  155. // }, [editorSettings?.renderDrawioInRealtime, markdown, pagePath]);
  156. // const renderHtml = useCallback(async() => {
  157. // if (interceptorManager == null) {
  158. // return;
  159. // }
  160. // const context = currentRenderingContext;
  161. // await interceptorManager.process('preRender', context);
  162. // await interceptorManager.process('prePreProcess', context);
  163. // context.markdown = growiRenderer.preProcess(context.markdown, context);
  164. // await interceptorManager.process('postPreProcess', context);
  165. // context.parsedHTML = growiRenderer.process(context.markdown, context);
  166. // await interceptorManager.process('prePostProcess', context);
  167. // context.parsedHTML = growiRenderer.postProcess(context.parsedHTML, context);
  168. // const isMarkdownEmpty = context.markdown.trim().length === 0;
  169. // if (highlightKeywords != null && !isMarkdownEmpty) {
  170. // context.parsedHTML = getHighlightedBody(context.parsedHTML, highlightKeywords);
  171. // }
  172. // await interceptorManager.process('postPostProcess', context);
  173. // await interceptorManager.process('preRenderHtml', context);
  174. // setHtml(context.parsedHTML);
  175. // }, [currentRenderingContext, growiRenderer, highlightKeywords, interceptorManager]);
  176. // useEffect(() => {
  177. // if (interceptorManager == null) {
  178. // return;
  179. // }
  180. // renderHtml()
  181. // .then(() => {
  182. // // const HeaderLink = document.getElementsByClassName('revision-head-link');
  183. // // const HeaderLinkArray = Array.from(HeaderLink);
  184. // // addSmoothScrollEvent(HeaderLinkArray as HTMLAnchorElement[], blinkElem);
  185. // // interceptorManager.process('postRenderHtml', currentRenderingContext);
  186. // });
  187. // }, [currentRenderingContext, interceptorManager, renderHtml]);
  188. // return (
  189. // <RevisionBody
  190. // html={html}
  191. // additionalClassName={props.additionalClassName}
  192. // renderMathJaxOnInit
  193. // />
  194. // );
  195. };
  196. export default RevisionRenderer;