Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proof of Concept] Multiple Stories inside one MD #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions packages/example-react/stories/Button.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';
// import Button from '../components/Button';

import ButtonReadme from '../components/Button/README.md';
import ButtonUsage from '../components/Button/USAGE.md';
import Multiple from './Multiple.md';

const ButtonReadme = '# hi';

storiesOf('Button', module)
.addParameters({
readme: {
codeTheme: 'duotone-sea',
content: ButtonReadme,
sidebar: ButtonUsage,
content: Multiple,
},
})
.add('Button', () => <Button label={'Hello Im Button'} />)
.add('Alert Button', () => (
<Button variant="alert" label={'Hello Im Button'} />
))
.add('Success Button', () => (
<Button variant="success" label={'Hello Im Button'} />
));

.add('README.md', () => {
return null;
});
25 changes: 25 additions & 0 deletions packages/example-react/stories/Multiple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Title

Some description

```js
// const Button = require('../components/Button');

<button>Hello World</button>
```

```js
// const Button = require('../components/Button');
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now don't know how correctly import components.
It should work in context of md file dir.


const value = state.counter || 0;

<button
onClick={() => {
setState({
counter: value + 1,
});
}}
>
Hello World {value}
</button>;
```
47 changes: 23 additions & 24 deletions packages/example-react/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import React from 'react';
import { configureReadme, addFooter, addHeader } from 'storybook-readme';

configureReadme({
// codeTheme: 'far',
header: `
// configureReadme({
// // codeTheme: 'far',
// header: `

### \`storybook-readme\` addon example
<img src="https://storybook.js.org//images/logos/logo-storybook.svg"/>
// ### \`storybook-readme\` addon example
// <img src="https://storybook.js.org//images/logos/logo-storybook.svg"/>

---
// ---

`,
footer: `
// `,
// footer: `

---
// ---

- Sources of this storybook (with React components) [tuchk4/storybook-readme/packages/example-react](https://github.com/tuchk4/storybook-readme/tree/master/packages/example-react)
- Sources of the same Vue storybook [tuchk4/storybook-readme/packages/example-vue](https://github.com/tuchk4/storybook-readme/tree/master/packages/example-vue)
- Issues and suggestions [storybook-readme/issues](https://github.com/tuchk4/storybook-readme/issues). You are welcome to suggest to awesome feature or report an annoying bug.
- <img src="https://upload.wikimedia.org/wikipedia/ru/thumb/9/9f/Twitter_bird_logo_2012.svg/1200px-Twitter_bird_logo_2012.svg.png" alt="twitter" style="width:16px;"/> [tuchk4](https://twitter.com/tuchk)
// - Sources of this storybook (with React components) [tuchk4/storybook-readme/packages/example-react](https://github.com/tuchk4/storybook-readme/tree/master/packages/example-react)
// - Sources of the same Vue storybook [tuchk4/storybook-readme/packages/example-vue](https://github.com/tuchk4/storybook-readme/tree/master/packages/example-vue)
// - Issues and suggestions [storybook-readme/issues](https://github.com/tuchk4/storybook-readme/issues). You are welcome to suggest to awesome feature or report an annoying bug.
// - <img src="https://upload.wikimedia.org/wikipedia/ru/thumb/9/9f/Twitter_bird_logo_2012.svg/1200px-Twitter_bird_logo_2012.svg.png" alt="twitter" style="width:16px;"/> [tuchk4](https://twitter.com/tuchk)


`,
});
// `,
// });

import './Button';
import './withCustomPreview';
import './withSidebarDocs';
import './withContentDocs';
import './withEmoji';
import './withPropsTable';
import './withCustomTheme';
import './withCustomCodeTheme';
import './withOldApi';
// import './withCustomPreview';
// import './withSidebarDocs';
// import './withContentDocs';
// import './withEmoji';
// import './withPropsTable';
// import './withCustomTheme';
// import './withCustomCodeTheme';
// import './withOldApi';
3 changes: 3 additions & 0 deletions packages/storybook-readme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
"dependencies": {
"@storybook/components": "^5.0.6",
"@storybook/core-events": "^5.0.6",
"acorn": "^6.1.1",
"buble": "^0.19.7",
"html-loader": "^0.5.5",
"lodash": "^4.17.11",
"markdown-loader": "^5.0.0",
"marked": "^0.6.1",
"marksy": "^8.0.0",
"node-emoji": "1.10.0",
"prism-themes": "^1.1.0",
"prismjs": "^1.16.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ export default class ReadmeContent extends React.Component {
case LAYOUT_TYPE_MD:
return (
<DocPreview key={index}>
<div
<div className={'markdown-body'}>{content}</div>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content is rendered components tree

{/* <div
className={'markdown-body'}
dangerouslySetInnerHTML={{ __html: content }}
/>
/> */}
</DocPreview>
);

Expand Down
8 changes: 8 additions & 0 deletions packages/storybook-readme/src/services/getDocsLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export default function getDocsLayout({
}) {
const mdAsArray = Array.isArray(md) ? [...md] : [md];
// const mdWithEmojis = mdAsArray.map(md => transformEmojis(md));

return [
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove old md features for Proof of convept. Returns only compiled md content

{
type: LAYOUT_TYPE_MD,
content: mdAsArray.map(processMd),
},
];

const mdWithEmojis = mdAsArray.map(processMd);

const main = mdWithEmojis[0];
Expand Down
65 changes: 63 additions & 2 deletions packages/storybook-readme/src/services/marked.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
import marked from 'marked';
// import marked from 'marked';
import React, { createElement } from 'react';
import * as buble from 'buble';
import { Parser } from 'acorn';

export default md => marked(md);
import marksy from 'marksy/jsx';

const unsemicolon = s => s.replace(/;\s*$/, '');

function processCode(code) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const value = state.counter || 0;

<button
  onClick={() => {
    setState({
      counter: value + 1,
    });
  }}
>
  Hello World {value}
</button>;

Transforms into

const value = state.counter || 0;

return <button
  onClick={() => {
    setState({
      counter: value + 1,
    });
  }}
>
  Hello World {value}
</button>;

const ast = Parser.parse(code, {
ecmaVersion: 2019,
sourceType: 'module',
});

if (!ast) {
return code;
}

const firstExpression = ast.body
.reverse()
.find(({ type }) => type === 'ExpressionStatement');

if (!firstExpression) {
return code;
}

const { start, end } = firstExpression;

const firstExpressionCode = unsemicolon(code.substring(start, end));
return `
${unsemicolon(code.substring(0, start))};

return (${firstExpressionCode});
`;
}

const getStoryComponent = code => {
return new Function('require', 'state', 'setState', 'React', code);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval

};

export default md => {
const compile = marksy({
createElement,
elements: {
code({ language, code, ...props }) {
const transformedCode = processCode(buble.transform(code).code);
const story = getStoryComponent(transformedCode);

// find initial state variable at AST ans set in here
const initialState = {};
const [state, setState] = React.useState({
...initialState,
});

return <div>{story(require, state, setState, React)}</div>;
},
},
});

const compiled = compile(md);

return compiled.tree;
};
53 changes: 49 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,11 @@
dependencies:
regenerator-runtime "^0.13.2"

"@babel/standalone@^7.4.5":
version "7.4.5"
resolved "https://registry.yarnpkg.com/@babel/standalone/-/standalone-7.4.5.tgz#60ded549756cf749eb6db0a54c21b4c48c7e18e5"
integrity sha512-Ddjq6OS+NxpJdvMEMiKVU4BCg/2IOpbP+2JNM9ZY1HULxlZGTyNXKIqXHRqzV0N6e+51zmTwQg+c1Lfs44bBHg==

"@babel/template@^7.1.0", "@babel/template@^7.2.2", "@babel/template@^7.4.0", "@babel/template@^7.4.4":
version "7.4.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.4.4.tgz#f4b88d1225689a08f5bc3a17483545be9e4ed237"
Expand Down Expand Up @@ -3166,6 +3171,11 @@ acorn-globals@^4.1.0:
acorn "^6.0.1"
acorn-walk "^6.0.1"

acorn-jsx@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.0.1.tgz#32a064fd925429216a09b141102bfdd185fae40e"
integrity sha512-HJ7CfNHrfJLlNTzIEUTj43LNWGkqpRLxm3YjAlcD0ACydk9XynzYsCBHxut+iqt+1aBXkx9UP/w/ZqMr13XIzg==

acorn-walk@^6.0.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-6.1.1.tgz#d363b66f5fac5f018ff9c3a1e7b6f8e310cc3913"
Expand All @@ -3176,7 +3186,7 @@ acorn@^5.5.3:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.3.tgz#67aa231bf8812974b85235a96771eb6bd07ea279"
integrity sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw==

acorn@^6.0.1, acorn@^6.0.5:
acorn@^6.0.1, acorn@^6.0.5, acorn@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.1.1.tgz#7d25ae05bb8ad1f9b699108e1094ecd7884adc1f"
integrity sha512-jPTiwtOxaHNaAPg/dmrJ/beuzLRnXtB0kQPQ8JpotKJgTB6rX6c8mlf315941pyjBSaPg8NHXS9fhP4u17DpGA==
Expand Down Expand Up @@ -4283,6 +4293,20 @@ btoa-lite@^1.0.0:
resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337"
integrity sha1-M3dm2hWAEhD92VbCLpxokaudAzc=

buble@^0.19.7:
version "0.19.7"
resolved "https://registry.yarnpkg.com/buble/-/buble-0.19.7.tgz#1dfd080ab688101aad5388d3304bc82601a244fd"
integrity sha512-YLgWxX/l+NnfotydBlxqCMPR4FREE4ubuHphALz0FxQ7u2hp3BzxTKQ4nKpapOaRJfEm1gukC68KnT2OymRK0g==
dependencies:
acorn "^6.1.1"
acorn-dynamic-import "^4.0.0"
acorn-jsx "^5.0.1"
chalk "^2.4.2"
magic-string "^0.25.2"
minimist "^1.2.0"
os-homedir "^1.0.1"
regexpu-core "^4.5.4"

buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
Expand Down Expand Up @@ -6947,7 +6971,7 @@ hastscript@^5.0.0:
property-information "^5.0.1"
space-separated-tokens "^1.0.0"

he@1.2.0, he@1.2.x, he@^1.1.0, he@^1.1.1:
he@1.2.0, he@1.2.x, he@^1.1.0, he@^1.1.1, he@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==
Expand Down Expand Up @@ -8737,6 +8761,13 @@ macos-release@^2.2.0:
resolved "https://registry.yarnpkg.com/macos-release/-/macos-release-2.2.0.tgz#ab58d55dd4714f0a05ad4b0e90f4370fef5cdea8"
integrity sha512-iV2IDxZaX8dIcM7fG6cI46uNmHUxHE4yN+Z8tKHAW1TBPMZDIKHf/3L+YnOuj/FK9il14UaVdHmiQ1tsi90ltA==

magic-string@^0.25.2:
version "0.25.2"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.2.tgz#139c3a729515ec55e96e69e82a11fe890a293ad9"
integrity sha512-iLs9mPjh9IuTtRsqqhNGYcZXGei0Nh/A4xirrsqW7c+QhKVFL2vm7U09ru6cHRD22azaP/wMDgI+HCqbETMTtg==
dependencies:
sourcemap-codec "^1.4.4"

make-dir@^1.0.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c"
Expand Down Expand Up @@ -8854,7 +8885,7 @@ markdown-to-jsx@^6.9.1, markdown-to-jsx@^6.9.3:
prop-types "^15.6.2"
unquote "^1.1.0"

marked@^0.3.9:
marked@^0.3.12, marked@^0.3.9:
version "0.3.19"
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.19.tgz#5d47f709c4c9fc3c216b6d46127280f40b39d790"
integrity sha512-ea2eGWOqNxPcXv8dyERdSr/6FmzvWwzjMxpfGB/sbMccXoct+xY+YukPD+QTUZwyvK7BZwcr4m21WBOW41pAkg==
Expand All @@ -8873,6 +8904,15 @@ marksy@^7.0.0:
he "^1.1.1"
marked "^0.6.2"

marksy@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/marksy/-/marksy-8.0.0.tgz#b595f121fd47058df9dda1448f6ee156ab48810a"
integrity sha512-mmHcKZojCQAGuKTuu3153viXdCuxUmsSxomFaSOBTkOlfWFOZBmDhmJkOp0CsPMNRQ7m6oN2wflvAHLpBNZVPw==
dependencies:
"@babel/standalone" "^7.4.5"
he "^1.2.0"
marked "^0.3.12"

matcher@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/matcher/-/matcher-1.1.1.tgz#51d8301e138f840982b338b116bb0c09af62c1c2"
Expand Down Expand Up @@ -9838,7 +9878,7 @@ os-browserify@^0.3.0:
resolved "https://registry.yarnpkg.com/os-browserify/-/os-browserify-0.3.0.tgz#854373c7f5c2315914fc9bfc6bd8238fdda1ec27"
integrity sha1-hUNzx/XCMVkU/Jv8a9gjj92h7Cc=

os-homedir@^1.0.0:
os-homedir@^1.0.0, os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
integrity sha1-/7xJiDNuDoM94MFox+8VISGqf7M=
Expand Down Expand Up @@ -12042,6 +12082,11 @@ source-map@^0.7.2:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

sourcemap-codec@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.4.tgz#c63ea927c029dd6bd9a2b7fa03b3fec02ad56e9f"
integrity sha512-CYAPYdBu34781kLHkaW3m6b/uUSyMOC2R61gcYMWooeuaGtjof86ZA/8T+qVPPt7np1085CR9hmMGrySwEc8Xg==

space-separated-tokens@^1.0.0:
version "1.1.4"
resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-1.1.4.tgz#27910835ae00d0adfcdbd0ad7e611fb9544351fa"
Expand Down