Skip to content

Commit f1599dd

Browse files
committed
No longer throws an error
1 parent 0b0c2b0 commit f1599dd

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

storyhelpers/storybook-readme/register.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable @typescript-eslint/no-var-requires */
33
/* eslint-disable @typescript-eslint/no-unused-vars */
44

5-
import React, { useState, useEffect } from 'react';
5+
import React, { useState, useEffect, useRef } from 'react';
66
import { addons, types } from '@storybook/addons';
77
import { AddonPanel } from '@storybook/components';
88
import ReactMarkdown from 'react-markdown';
@@ -17,47 +17,63 @@ const PANEL_ID = `${ADDON_ID}/panel`;
1717

1818
const Readme = props => {
1919
const [markdown, setMarkdown] = useState();
20-
2120
useEffect(() => {
2221
const api = props.api;
2322
api.on(STORY_RENDERED, eventData => {
2423
const component = api.getCurrentStoryData().component;
25-
2624
const readme =
2725
require(`!raw-loader!../../packages/${component}/README.md`).default;
2826

2927
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+
}
3042
});
3143
return () => {
3244
//TODO: find out how to remove the api event listener
3345
};
3446
}, []);
3547

48+
const renderReadme = () => (
49+
<ReactMarkdown
50+
children={markdown}
51+
remarkPlugins={[remarkGfm]}
52+
components={{
53+
code({ node, inline, className, children, ...props }) {
54+
const match = /language-(\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+
3674
return (
3775
<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 = /language-(\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()}
6177
</div>
6278
);
6379
};

0 commit comments

Comments
 (0)