-
Notifications
You must be signed in to change notification settings - Fork 7
Auto-generate routes for components #7
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
base: main
Are you sure you want to change the base?
Changes from all commits
08fa1f0
c435313
d001553
8d1e453
46968f2
f02aeb1
113067b
be7274b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env node | ||
|
||
const esbuild = require('esbuild'); | ||
const cssModules = require('esbuild-css-modules-plugin'); | ||
|
||
const format = process.env.FORMAT; | ||
|
||
esbuild.build({ | ||
logLevel: 'info', | ||
sourcemap: true, | ||
bundle: false, | ||
outbase: 'src', | ||
format: format, | ||
outdir: format === 'esm' ? 'pkg/dist-src' : 'pkg/dist-node', | ||
platform: 'node', | ||
plugins: [cssModules()], | ||
entryPoints: [ | ||
'src/index.ts', | ||
'src/hooks/useDynamicImport.ts', | ||
'src/theme/ReactDocLayout/index.tsx', | ||
'src/theme/ReactDocLayout/styles.module.css', | ||
'src/theme/ReactComponentPage/index.tsx', | ||
'src/theme/ReactComponentItem/index.tsx', | ||
'src/theme/ReactPropTable/index.tsx', | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import path from 'path'; | ||
import globby from 'globby'; | ||
import docgen, { ParserOptions, ComponentDoc, FileParser } from 'react-docgen-typescript'; | ||
import { DEFAULT_PLUGIN_ID } from '@docusaurus/core/lib/constants'; | ||
|
||
import { Plugin, DocusaurusContext, RouteConfig } from '@docusaurus/types'; | ||
import { Plugin, LoadContext, RouteConfig } from '@docusaurus/types'; | ||
import { docuHash } from '@docusaurus/utils'; | ||
import { CompilerOptions } from 'typescript'; | ||
import { PropSidebarItem } from '@docusaurus/plugin-content-docs-types'; | ||
|
||
type Route = Pick<RouteConfig, 'exact' | 'component' | 'path' | 'priority'>; | ||
|
||
|
@@ -18,10 +21,13 @@ type Union = | |
}; | ||
|
||
type Options = Union & { | ||
id: string; | ||
src: string | string[]; | ||
tsConfig?: string; | ||
compilerOptions?: CompilerOptions; | ||
parserOptions?: ParserOptions; | ||
createRoutes?: boolean; | ||
baseRoute?: string; | ||
}; | ||
|
||
const getParser = ( | ||
|
@@ -39,23 +45,37 @@ const getParser = ( | |
}; | ||
|
||
export default function plugin( | ||
context: DocusaurusContext, | ||
{ src, global = false, route, tsConfig, compilerOptions, parserOptions }: Options | ||
context: LoadContext, | ||
{ | ||
id, | ||
src, | ||
global = false, | ||
route, | ||
tsConfig, | ||
compilerOptions, | ||
parserOptions, | ||
createRoutes, | ||
baseRoute = '/docs/react', | ||
}: Options | ||
): Plugin<ComponentDoc[]> { | ||
const { generatedFilesDir } = context; | ||
|
||
const pluginId = id ?? DEFAULT_PLUGIN_ID; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inspired by docusaurus-plugin-openapi setup. |
||
const pluginName = 'docusaurus-plugin-react-docgen-typescript'; | ||
const pluginDataDirRoot = path.join(generatedFilesDir, pluginName); | ||
|
||
const dataDir = path.join(pluginDataDirRoot, pluginId); | ||
|
||
return { | ||
name: 'docusaurus-plugin-react-docgen-typescript', | ||
name: pluginName, | ||
async loadContent() { | ||
return getParser(tsConfig, compilerOptions, parserOptions)(await globby(src)); | ||
}, | ||
configureWebpack(config) { | ||
configureWebpack() { | ||
return { | ||
resolve: { | ||
alias: { | ||
'@docgen': path.join( | ||
config.resolve.alias['@generated'], | ||
'docusaurus-plugin-react-docgen-typescript', | ||
'default' | ||
), | ||
'@docgen': dataDir, | ||
}, | ||
}, | ||
}; | ||
|
@@ -77,10 +97,63 @@ export default function plugin( | |
}, | ||
}); | ||
} else { | ||
content.map(component => | ||
createData(`${component.displayName}.json`, JSON.stringify(component.props)) | ||
const sidebarName = `react-docgen-typescript-sidebar-${pluginId}`; | ||
const sidebar: PropSidebarItem[] = content.map(component => ({ | ||
type: 'link', | ||
href: `${baseRoute}/${component.displayName}`, | ||
label: component.displayName, | ||
})); | ||
|
||
const componentMetadataPath = await createData( | ||
`${docuHash('component-metadata-prop')}.json`, | ||
JSON.stringify( | ||
{ | ||
apiSidebars: { | ||
[sidebarName]: sidebar, | ||
}, | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
|
||
const routes: RouteConfig[] = await Promise.all( | ||
content.map(async component => { | ||
const componentData = await createData( | ||
`${component.displayName}.json`, | ||
JSON.stringify(component) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the only breaking change: |
||
); | ||
|
||
if (createRoutes) { | ||
return { | ||
path: `${baseRoute}/${component.displayName}`, | ||
exact: true, | ||
component: '@theme/ReactComponentItem', | ||
sidebar: sidebarName, | ||
modules: { | ||
componentMetadata: componentMetadataPath, | ||
data: componentData, | ||
}, | ||
}; | ||
} | ||
}) | ||
); | ||
|
||
if (createRoutes) { | ||
addRoute({ | ||
path: baseRoute, | ||
component: '@theme/ReactComponentPage', | ||
sidebar: sidebarName, | ||
routes, | ||
modules: { | ||
componentMetadata: componentMetadataPath, | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
getThemePath(): string { | ||
return path.resolve(__dirname, './theme'); | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ReactComponentMetadata = { | ||
apiSidebars: import('@docusaurus/plugin-content-docs-types').PropSidebars; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
|
||
import type { Props } from '@theme/DocPage'; | ||
import { ComponentDoc } from 'react-docgen-typescript'; | ||
import ReactPropTable from '@theme/ReactPropTable'; | ||
|
||
export type ReactComponentItemProps = Omit<Props, 'versionMetadata'> & { | ||
data: ComponentDoc; | ||
}; | ||
|
||
export default function ReactComponentItem(props: ReactComponentItemProps): JSX.Element { | ||
const { data } = props; | ||
|
||
return ( | ||
<> | ||
<h2>{data.displayName}</h2> | ||
<p>{data.description}</p> | ||
<h3>Props</h3> | ||
<ReactPropTable data={data.props} /> | ||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { ComponentDoc } from 'react-docgen-typescript'; | ||
import renderRoutes from '@docusaurus/renderRoutes'; | ||
import type { Props } from '@theme/DocPage'; | ||
import ReactDocLayout from '@theme/ReactDocLayout'; | ||
import { ReactComponentMetadata } from '../../metadata'; | ||
import { matchPath, useLocation } from '@docusaurus/router'; | ||
|
||
export type ReactComponentPageProps = Omit<Props, 'versionMetadata'> & { | ||
componentMetadata: ReactComponentMetadata; | ||
data: ComponentDoc[]; | ||
}; | ||
|
||
export default function ReactComponentPage({ | ||
componentMetadata, | ||
route: { routes: componentRoutes, ...route }, | ||
}: ReactComponentPageProps): JSX.Element { | ||
const location = useLocation(); | ||
const currentRoute = | ||
componentRoutes.find(componentRoute => matchPath(location.pathname, componentRoute)) || | ||
route; | ||
|
||
return ( | ||
<ReactDocLayout componentMetadata={componentMetadata} currentRoute={currentRoute}> | ||
{renderRoutes(componentRoutes)} | ||
</ReactDocLayout> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was easier to update the component's
createData
with the whole metadata, not only the props.