|
1 |
| -// /my-addon/src/register.js |
| 1 | +/* eslint-disable import/default */ |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
2 | 4 |
|
3 |
| -import React from 'react'; |
| 5 | +import React, { useState, useEffect } from 'react'; |
4 | 6 | import { addons, types } from '@storybook/addons';
|
5 | 7 | import { AddonPanel } from '@storybook/components';
|
6 | 8 | import ReactMarkdown from 'react-markdown';
|
7 |
| -import { STORY_CHANGED, STORY_UNCHANGED } from '@storybook/core-events'; |
| 9 | +import { STORY_RENDERED } from '@storybook/core-events'; |
| 10 | +import 'github-markdown-css/github-markdown.css'; |
| 11 | +import remarkGfm from 'remark-gfm'; |
| 12 | +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; |
| 13 | +import { vs } from 'react-syntax-highlighter/dist/esm/styles/prism'; |
8 | 14 |
|
9 | 15 | const ADDON_ID = 'readme';
|
10 | 16 | const PANEL_ID = `${ADDON_ID}/panel`;
|
11 | 17 |
|
12 | 18 | const Readme = props => {
|
| 19 | + const [markdown, setMarkdown] = useState(); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + const api = props.api; |
| 23 | + api.on(STORY_RENDERED, eventData => { |
| 24 | + const component = api.getCurrentStoryData().component; |
| 25 | + |
| 26 | + const readme = |
| 27 | + require(`!raw-loader!../../packages/${component}/README.md`).default; |
| 28 | + |
| 29 | + setMarkdown(readme); |
| 30 | + }); |
| 31 | + return () => { |
| 32 | + //TODO: find out how to remove the api event listener |
| 33 | + }; |
| 34 | + }, []); |
| 35 | + |
13 | 36 | return (
|
14 |
| - <div> |
15 |
| - <ReactMarkdown children={props.markdown} /> |
| 37 | + <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 | + /> |
16 | 61 | </div>
|
17 | 62 | );
|
18 | 63 | };
|
19 | 64 |
|
20 | 65 | addons.register(ADDON_ID, api => {
|
21 |
| - let markdown = ''; |
22 |
| - |
23 |
| - api.on(STORY_CHANGED, eventData => { |
24 |
| - console.log('EHM HALLO', eventData); |
25 |
| - const component = api.getCurrentStoryData().component; |
26 |
| - console.log('TESTING', component); |
27 |
| - markdown = |
28 |
| - require(`!raw-loader!../../packages/${component}/README.md`).default; |
29 |
| - }); |
30 |
| - |
31 | 66 | addons.add(PANEL_ID, {
|
32 | 67 | type: types.PANEL,
|
33 | 68 | title: 'Readme',
|
34 |
| - match: ({ viewMode }) => viewMode === 'story', |
35 | 69 | render: ({ active, key }) => (
|
36 | 70 | <AddonPanel active={active} key={key}>
|
37 |
| - <Readme markdown={markdown} /> |
| 71 | + <Readme api={api} /> |
38 | 72 | </AddonPanel>
|
39 | 73 | ),
|
40 | 74 | });
|
|
0 commit comments