Skip to content

Commit 03c1c03

Browse files
committed
docs(storybook): upgrade readme plugin to v7 api
1 parent bec14f9 commit 03c1c03

File tree

1 file changed

+4
-41
lines changed

1 file changed

+4
-41
lines changed

storyhelpers/storybook-readme/register.js

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
/* eslint-disable @typescript-eslint/no-unused-vars */
44
import 'github-markdown-css/github-markdown.css';
55

6-
import {
7-
addons,
8-
types,
9-
useStorybookApi,
10-
useChannel,
11-
} from '@storybook/manager-api';
6+
import { addons, types, useParameter } from '@storybook/manager-api';
127
import { AddonPanel } from '@storybook/components';
13-
import { STORY_RENDERED } from '@storybook/core-events';
148
import React, { useEffect, useState } from 'react';
159
import ReactMarkdown from 'react-markdown';
1610
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
@@ -22,44 +16,13 @@ import remarkGfm from 'remark-gfm';
2216

2317
const ADDON_ID = 'readme';
2418
const PANEL_ID = `${ADDON_ID}/panel`;
19+
const PARAM_KEY = 'readme';
2520

2621
const Readme = () => {
27-
const api = useStorybookApi();
28-
const [markdown, setMarkdown] = useState();
2922
const [useDarkMode, setUseDarkMode] = useState();
3023

31-
useChannel({
32-
[STORY_RENDERED]: async () => {
33-
setMarkdown('');
34-
const componentPath = api.getCurrentStoryData().importPath.split('/');
35-
const component = componentPath[componentPath.length - 1].split('.')[0];
36-
if (component) {
37-
try {
38-
const readmeUrl = `../../packages/${component}/README.md`;
39-
const readme = await fetch(readmeUrl, {
40-
headers: { 'Content-Type': 'text/plain' },
41-
}).then(res => res.text());
42-
43-
setMarkdown(readme);
44-
45-
const syntaxHighlighters = document.querySelectorAll(
46-
'.storybook-readme-syntax-highlighter'
47-
);
48-
49-
if (syntaxHighlighters.length > 0) {
50-
for (const item of syntaxHighlighters) {
51-
const children = item.children;
52-
const parent = item.parentElement;
53-
54-
parent.append(...children);
55-
}
56-
}
57-
} catch (e) {
58-
console.warn('No README file found for', component);
59-
}
60-
}
61-
},
62-
});
24+
const markdownParameter = useParameter(PARAM_KEY, null);
25+
const markdown = markdownParameter ? markdownParameter.data : null;
6326

6427
const updateUseDarkMode = event => {
6528
setUseDarkMode(event.matches);

0 commit comments

Comments
 (0)