Skip to content

Commit 142c275

Browse files
committed
Addon updates with story, and styling
1 parent d3953ba commit 142c275

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

storyhelpers/storybook-readme/Readme.jsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

storyhelpers/storybook-readme/register.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,74 @@
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 */
24

3-
import React from 'react';
5+
import React, { useState, useEffect } from 'react';
46
import { addons, types } from '@storybook/addons';
57
import { AddonPanel } from '@storybook/components';
68
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';
814

915
const ADDON_ID = 'readme';
1016
const PANEL_ID = `${ADDON_ID}/panel`;
1117

1218
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+
1336
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+
/>
1661
</div>
1762
);
1863
};
1964

2065
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-
3166
addons.add(PANEL_ID, {
3267
type: types.PANEL,
3368
title: 'Readme',
34-
match: ({ viewMode }) => viewMode === 'story',
3569
render: ({ active, key }) => (
3670
<AddonPanel active={active} key={key}>
37-
<Readme markdown={markdown} />
71+
<Readme api={api} />
3872
</AddonPanel>
3973
),
4074
});

0 commit comments

Comments
 (0)