generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add docs tab in storybook (#61)
- Loading branch information
Showing
6 changed files
with
356 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
Description, | ||
DocsContext, | ||
Subtitle, | ||
Title, | ||
} from '@storybook/addon-docs/'; | ||
|
||
const readmeCache: Record<string, {default: string}> = {}; | ||
|
||
function importAllReadme(ctx: __WebpackModuleApi.RequireContext) { | ||
const path = ctx.id.split(' ')[0].replace('./', '') + '/'; | ||
ctx.keys().forEach((key) => { | ||
const dirPath = key.replace(/^\.\//, path).replace(/\/readme\.md$/i, ''); | ||
readmeCache[dirPath] = ctx(key); | ||
}); | ||
} | ||
|
||
importAllReadme(require.context('../src/components', true, /readme\.md$/i)); | ||
|
||
export const Docs = () => { | ||
const context = React.useContext(DocsContext); | ||
const fileName = context?.parameters?.fileName; | ||
const kind = context.kind; | ||
let isComponent = false; | ||
if (kind && /Components\//.test(kind)) { | ||
isComponent = true; | ||
} | ||
|
||
let dirPath; | ||
if (isComponent && fileName) { | ||
const pathArr = fileName.split('/'); | ||
dirPath = pathArr.slice(1, pathArr.length - 2).join('/'); | ||
} | ||
|
||
let sourceBadgeContent; | ||
if (dirPath) { | ||
sourceBadgeContent = ( | ||
<a | ||
href={`https://github.com/gravity-ui/components/tree/main/${dirPath}`} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
<img src="https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/icons/github-badge.svg" /> | ||
</a> | ||
); | ||
} | ||
|
||
let readmeContent; | ||
if (dirPath && readmeCache[dirPath]) { | ||
readmeContent = ( | ||
<div | ||
className="yfm yfm_only-light" | ||
dangerouslySetInnerHTML={{__html: readmeCache[dirPath].default}} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{/* <Title /> */} | ||
{sourceBadgeContent} | ||
{/* <Subtitle /> */} | ||
{/* <Description /> */} | ||
{readmeContent} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import type {StorybookConfig} from '@storybook/core-common'; | ||
|
||
const {join} = require('path'); | ||
|
||
|
||
const config: StorybookConfig = { | ||
stories: ['../src/**/*.stories.@(ts|tsx)'], | ||
addons: [ | ||
'@storybook/preset-scss', | ||
{name: '@storybook/addon-essentials', options: {backgrounds: false}}, | ||
'./theme-addon/register.tsx', | ||
], | ||
|
||
webpackFinal: (storybookBaseConfig) => { | ||
storybookBaseConfig?.module?.rules.push({ | ||
test: /\.md$/, | ||
include: [join(__dirname, '../src')], | ||
use: [{loader: 'markdown-loader'}], | ||
}); | ||
|
||
// to turn fileName in context.parameters into path form number in production bundle | ||
if (storybookBaseConfig?.optimization) { | ||
storybookBaseConfig.optimization.moduleIds = 'named'; | ||
} | ||
|
||
return storybookBaseConfig; | ||
}, | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import '~@doc-tools/transform/dist/css/yfm.css'; |
Oops, something went wrong.