@@ -5,7 +5,20 @@ import markdownit from 'markdown-it';
55
66import { parseTemplate } from './utils' ;
77
8- const toHTML = ( content , data = { } , highlight = true ) => {
8+ const ContentBlock = ( { content, data, highlight } ) => {
9+ const html = ContentBlock . toHTML ( content , data , highlight ) ;
10+ return < div dangerouslySetInnerHTML = { { __html : html } } /> ;
11+ } ;
12+
13+ ContentBlock . parseLanguages = content => {
14+ const md = new markdownit ( ) ;
15+ return md
16+ . parse ( content )
17+ . filter ( token => token . type === 'fence' )
18+ . map ( token => token . info ) ;
19+ } ;
20+
21+ ContentBlock . toHTML = ( content , data = { } , highlight = false ) => {
922 const md = new markdownit ( {
1023 highlight : ( str , lang ) => {
1124 if ( highlight && lang ) {
@@ -23,35 +36,18 @@ const toHTML = (content, data = {}, highlight = true) => {
2336 } ) ;
2437
2538 const markdown = parseTemplate ( content , data ) ;
26-
27- if ( highlight ) {
28- const languageImports = md
29- . parse ( markdown )
30- . filter ( token => token . type === 'fence' )
31- . map ( token => import ( `prismjs/components/prism-${ token . info } ` ) ) ;
32- return Promise . all ( languageImports ) . then ( ( ) => md . render ( markdown ) ) ;
33- }
34-
3539 return md . render ( markdown ) ;
3640} ;
3741
38- const ContentBlock = ( { content, data } ) => {
39- const [ html , setHtml ] = useState ( toHTML ( content , data , false ) ) ;
40-
41- useEffect ( ( ) => {
42- toHTML ( content , data ) . then ( result => setHtml ( result ) ) ;
43- } , [ content , data ] ) ;
44-
45- return < div dangerouslySetInnerHTML = { { __html : html } } /> ;
46- } ;
47-
4842ContentBlock . propTypes = {
4943 content : PropTypes . string . isRequired ,
5044 data : PropTypes . object ,
45+ highlight : PropTypes . bool ,
5146} ;
5247
5348ContentBlock . defaultProps = {
5449 data : { } ,
50+ highlight : false ,
5551} ;
5652
5753export default ContentBlock ;
0 commit comments