File tree Expand file tree Collapse file tree 8 files changed +157
-0
lines changed Expand file tree Collapse file tree 8 files changed +157
-0
lines changed Original file line number Diff line number Diff line change 7676 "@emotion/react" : " 11.7.1" ,
7777 "@rollup/plugin-json" : " ^6.0.0" ,
7878 "emoji-toolkit" : " ^7.0.1" ,
79+ "katex" : " ^0.16.21" ,
7980 "prop-types" : " ^15.8.1" ,
81+ "react-error-boundary" : " ^5.0.0" ,
8082 "react-syntax-highlighter" : " ^15.6.1"
8183 }
8284}
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import ChannelMention from '../mentions/ChannelMention';
1010import ColorElement from './ColorElement' ;
1111import LinkSpan from './LinkSpan' ;
1212import UserMention from '../mentions/UserMention' ;
13+ import KatexErrorBoundary from '../katex/KatexErrorBoundary' ;
14+ import KatexElement from '../katex/KatexElement' ;
1315
1416const InlineElements = ( { contents } ) =>
1517 contents . map ( ( content , index ) => {
@@ -53,6 +55,14 @@ const InlineElements = ({ contents }) =>
5355 }
5456 />
5557 ) ;
58+
59+ case 'INLINE_KATEX' :
60+ return (
61+ < KatexErrorBoundary key = { index } code = { content . value } >
62+ < KatexElement code = { content . value } />
63+ </ KatexErrorBoundary >
64+ ) ;
65+
5666 default :
5767 return null ;
5868 }
Original file line number Diff line number Diff line change 1+ import PropTypes from 'prop-types' ;
2+ import katex from 'katex' ;
3+ import React , { useMemo } from 'react' ;
4+
5+ import 'katex/dist/katex.css' ;
6+
7+ const KatexBlock = ( { code } ) => {
8+ const html = useMemo (
9+ ( ) =>
10+ katex . renderToString ( code , {
11+ displayMode : true ,
12+ macros : {
13+ '\\href' : '\\@secondoftwo' ,
14+ } ,
15+ maxSize : 100 ,
16+ } ) ,
17+ [ code ]
18+ ) ;
19+
20+ return (
21+ < div
22+ role = "math"
23+ style = { { overflowX : 'auto' } }
24+ aria-label = { code }
25+ dangerouslySetInnerHTML = { { __html : html } }
26+ />
27+ ) ;
28+ } ;
29+
30+ export default KatexBlock ;
31+ KatexBlock . prototype = {
32+ code : PropTypes . string . isRequired ,
33+ } ;
Original file line number Diff line number Diff line change 1+ import katex from 'katex' ;
2+ import React , { useMemo } from 'react' ;
3+ import PropTypes from 'prop-types' ;
4+
5+ import 'katex/dist/katex.css' ;
6+
7+ const KatexElement = ( { code } ) => {
8+ const html = useMemo (
9+ ( ) =>
10+ katex . renderToString ( code , {
11+ displayMode : false ,
12+ macros : {
13+ '\\href' : '\\@secondoftwo' ,
14+ } ,
15+ maxSize : 100 ,
16+ } ) ,
17+ [ code ]
18+ ) ;
19+
20+ return < span dangerouslySetInnerHTML = { { __html : html } } /> ;
21+ } ;
22+
23+ export default KatexElement ;
24+ KatexElement . prototype = {
25+ code : PropTypes . string . isRequired ,
26+ } ;
Original file line number Diff line number Diff line change 1+ // import colors from '@rocket.chat/fuselage-tokens/colors.json';
2+ import React , { useState } from 'react' ;
3+ import { ErrorBoundary } from 'react-error-boundary' ;
4+ import PropTypes from 'prop-types' ;
5+ import { Box } from '@embeddedchat/ui-elements' ;
6+ import { css } from '@emotion/react' ;
7+
8+ // const Fallback = styled('span')`
9+ // text-decoration: underline;
10+ // text-decoration-color: ${colors.r400};
11+ // `;
12+
13+ const KatexErrorBoundary = ( { children, code } ) => {
14+ const [ error , setError ] = useState ( null ) ;
15+ return (
16+ < ErrorBoundary
17+ // children={children}
18+ onError = { setError }
19+ fallback = {
20+ < Box
21+ is = "span"
22+ title = { error ?. message }
23+ css = { css `
24+ text-decoration : underline;
25+ text-decoration-color : red;
26+ ` }
27+ >
28+ < span > { code } </ span >
29+ </ Box >
30+ }
31+ >
32+ { children }
33+ </ ErrorBoundary >
34+ ) ;
35+ } ;
36+
37+ export default KatexErrorBoundary ;
38+ KatexErrorBoundary . propTypes = {
39+ code : PropTypes . string . isRequired ,
40+ children : PropTypes . node . isRequired ,
41+ } ;
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import 'katex/dist/katex.css' ;
3+ import PropTypes from 'prop-types' ;
4+
5+ const PreviewKatexBlock = ( { code } ) => < > { code } </ > ;
6+
7+ export default PreviewKatexBlock ;
8+ PreviewKatexBlock . propTypes = {
9+ code : PropTypes . string . isRequired ,
10+ } ;
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import PropTypes from 'prop-types' ;
3+
4+ import 'katex/dist/katex.css' ;
5+
6+ const PreviewKatexElement = ( { code } ) => < > { code } </ > ;
7+
8+ export default PreviewKatexElement ;
9+ PreviewKatexElement . propTypes = {
10+ code : PropTypes . string . isRequired ,
11+ } ;
Original file line number Diff line number Diff line change @@ -2360,12 +2360,14 @@ __metadata:
23602360 eslint-plugin-react-hooks: ^4.6.0
23612361 identity-obj-proxy: ^3.0.0
23622362 jest: ^27.5.1
2363+ katex: ^0.16.21
23632364 lint-staged: ^12.4.2
23642365 npm-run-all: ^4.1.5
23652366 prettier: ^2.8.1
23662367 prop-types: ^15.8.1
23672368 react: ^17.0.2
23682369 react-dom: ^17.0.2
2370+ react-error-boundary: ^5.0.0
23692371 react-syntax-highlighter: ^15.6.1
23702372 rimraf: ^5.0.1
23712373 rollup: ^2.70.1
@@ -21418,6 +21420,17 @@ __metadata:
2141821420 languageName: node
2141921421 linkType: hard
2142021422
21423+ "katex@npm:^0.16.21":
21424+ version: 0.16.21
21425+ resolution: "katex@npm:0.16.21"
21426+ dependencies:
21427+ commander: ^8.3.0
21428+ bin:
21429+ katex: cli.js
21430+ checksum: 14180322a4e8fe9e4227a08b7d86fde9ee445859ff534e6a540b85eb5022b39ea2be70082776cce8c59b891c247fce3d1c1a090ea7821e005fd8b7bfee714936
21431+ languageName: node
21432+ linkType: hard
21433+
2142121434"keyv@npm:^4.5.3":
2142221435 version: 4.5.4
2142321436 resolution: "keyv@npm:4.5.4"
@@ -26869,6 +26882,17 @@ __metadata:
2686926882 languageName: node
2687026883 linkType: hard
2687126884
26885+ "react-error-boundary@npm:^5.0.0":
26886+ version: 5.0.0
26887+ resolution: "react-error-boundary@npm:5.0.0"
26888+ dependencies:
26889+ "@babel/runtime": ^7.12.5
26890+ peerDependencies:
26891+ react: ">=16.13.1"
26892+ checksum: 4fa78890bb254fe1f0ee1eed893ac161a27482c4567f7667ef83a8339432eb99e323ee69757f01f4864e0037b01a9b6822735ea122f02e749d0bf7a781d9ea53
26893+ languageName: node
26894+ linkType: hard
26895+
2687226896"react-error-overlay@npm:6.0.9":
2687326897 version: 6.0.9
2687426898 resolution: "react-error-overlay@npm:6.0.9"
You can’t perform that action at this time.
0 commit comments