2
2
/* eslint-disable @typescript-eslint/no-var-requires */
3
3
/* eslint-disable @typescript-eslint/no-unused-vars */
4
4
5
- import React , { useState , useEffect } from 'react' ;
5
+ import React , { useState , useEffect , useRef } from 'react' ;
6
6
import { addons , types } from '@storybook/addons' ;
7
7
import { AddonPanel } from '@storybook/components' ;
8
8
import ReactMarkdown from 'react-markdown' ;
@@ -17,47 +17,63 @@ const PANEL_ID = `${ADDON_ID}/panel`;
17
17
18
18
const Readme = props => {
19
19
const [ markdown , setMarkdown ] = useState ( ) ;
20
-
21
20
useEffect ( ( ) => {
22
21
const api = props . api ;
23
22
api . on ( STORY_RENDERED , eventData => {
24
23
const component = api . getCurrentStoryData ( ) . component ;
25
-
26
24
const readme =
27
25
require ( `!raw-loader!../../packages/${ component } /README.md` ) . default ;
28
26
29
27
setMarkdown ( readme ) ;
28
+
29
+ const syntaxHighlighters = document . querySelectorAll (
30
+ '.storybook-readme-syntax-highlighter'
31
+ ) ;
32
+
33
+ if ( syntaxHighlighters . length > 0 ) {
34
+ for ( const item of syntaxHighlighters ) {
35
+ item . style . display = 'none' ;
36
+ const children = item . children ;
37
+ const parent = item . parentElement ;
38
+
39
+ parent . append ( ...children ) ;
40
+ }
41
+ }
30
42
} ) ;
31
43
return ( ) => {
32
44
//TODO: find out how to remove the api event listener
33
45
} ;
34
46
} , [ ] ) ;
35
47
48
+ const renderReadme = ( ) => (
49
+ < ReactMarkdown
50
+ children = { markdown }
51
+ remarkPlugins = { [ remarkGfm ] }
52
+ components = { {
53
+ code ( { node, inline, className, children, ...props } ) {
54
+ const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
55
+ return ! inline && match ? (
56
+ < SyntaxHighlighter
57
+ className = "storybook-readme-syntax-highlighter"
58
+ children = { String ( children ) . replace ( / \n $ / , '' ) }
59
+ style = { { ...vs } }
60
+ language = { match [ 1 ] }
61
+ PreTag = { 'div' }
62
+ { ...props }
63
+ />
64
+ ) : (
65
+ < code className = { className } { ...props } >
66
+ { children }
67
+ </ code >
68
+ ) ;
69
+ } ,
70
+ } }
71
+ />
72
+ ) ;
73
+
36
74
return (
37
75
< div className = "markdown-body" style = { { padding : '32px' } } >
38
- < ReactMarkdown
39
- children = { markdown }
40
- remarkPlugins = { [ remarkGfm ] }
41
- components = { {
42
- code ( { node, inline, className, children, ...props } ) {
43
- const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
44
- console . log ( 'HERE' , props ) ;
45
- return ! inline && match ? (
46
- < SyntaxHighlighter
47
- children = { String ( children ) . replace ( / \n $ / , '' ) }
48
- style = { vs }
49
- language = { match [ 1 ] }
50
- PreTag = { React . Fragment }
51
- { ...props }
52
- />
53
- ) : (
54
- < code className = { className } { ...props } >
55
- { children }
56
- </ code >
57
- ) ;
58
- } ,
59
- } }
60
- />
76
+ { renderReadme ( ) }
61
77
</ div >
62
78
) ;
63
79
} ;
0 commit comments