@@ -14,6 +14,8 @@ import { RootStore } from '@/store';
1414import { StorageState } from '@/store/standard/StorageState' ;
1515import { Icon } from '@iconify/react' ;
1616import { PhotoProvider , PhotoView } from 'react-photo-view' ;
17+ import rehypeRaw from 'rehype-raw' ;
18+ import rehypeSanitize from 'rehype-sanitize' ;
1719
1820const highlightTags = ( text ) => {
1921 if ( ! text ) return text
@@ -58,43 +60,6 @@ const LinkPreview = ({ href }) => {
5860 const store = RootStore . Local ( ( ) => ( {
5961 previewData : new StorageState < LinkInfo | null > ( { key : href , default : null } )
6062 } ) )
61- try {
62- if ( typeof href == 'object' ) {
63- return < div className = 'react-image' >
64- < PhotoProvider >
65- < PhotoView src = { href ?. props ?. src } >
66- < Image src = { href ?. props ?. src } />
67- </ PhotoView >
68- </ PhotoProvider >
69- </ div >
70- }
71- if ( href ?. startsWith ( '<img' ) ) {
72- const src = href . match ( / s r c = " ( [ ^ " ] + ) " / ) ?. [ 1 ]
73- const regex = / h e i g h t = " ( \d + ) " \s + w i d t h = " ( \d + ) " / ;
74- const matches = href . match ( regex ) ;
75- console . log ( matches )
76- let style = { }
77- if ( matches ) {
78- let height = matches [ 1 ] ;
79- let width = matches [ 2 ] ;
80- style = { height : height + 'px' , width : width + 'px' }
81- console . log ( `Height: ${ height } , Width: ${ width } ` ) ;
82- } else {
83- console . log ( "No match found." ) ;
84- }
85- return < div className = 'text-image' >
86- < PhotoProvider >
87- < PhotoView src = { src } >
88- < Image src = { src } { ...style } />
89- </ PhotoView >
90- </ PhotoProvider >
91- </ div >
92- }
93- } catch ( error ) {
94- console . log ( error )
95- return href
96- }
97-
9863 useEffect ( ( ) => {
9964 const fetchData = async ( ) => {
10065 try {
@@ -124,6 +89,21 @@ const LinkPreview = ({ href }) => {
12489 ) ;
12590} ;
12691
92+ const ImageWrapper = ( { src, alt, width, height } ) => {
93+ return (
94+ < div className = "custom-image-wrapper" >
95+ < div className = 'react-image' >
96+ < PhotoProvider >
97+ < PhotoView src = { src } >
98+ < Image src = { src } width = { width } height = { height } />
99+ </ PhotoView >
100+ </ PhotoProvider >
101+ </ div >
102+ </ div >
103+ ) ;
104+ } ;
105+
106+
127107
128108export const MarkdownRender = observer ( ( { content = '' , onChange } : { content ?: string , onChange ?: ( newContent : string ) => void } ) => {
129109 const { theme } = useTheme ( )
@@ -150,6 +130,7 @@ export const MarkdownRender = observer(({ content = '', onChange }: { content?:
150130 < div ref = { contentRef } data-markdown-theme = { theme } className = { `markdown-body content ${ isExpanded ? "expanded" : "collapsed" } ` } >
151131 < ReactMarkdown
152132 remarkPlugins = { [ remarkGfm ] }
133+ rehypePlugins = { [ rehypeRaw , rehypeSanitize ] } //xss
153134 components = { {
154135 p : ( { node, children } ) => < p > { highlightTags ( children ) } </ p > ,
155136 code : Code ,
@@ -180,6 +161,7 @@ export const MarkdownRender = observer(({ content = '', onChange }: { content?:
180161 }
181162 return < li > { children } </ li >
182163 } ,
164+ img : ImageWrapper
183165 } }
184166 >
185167 { content }
0 commit comments