feat(props): add props data gathering and table rendering support#40
feat(props): add props data gathering and table rendering support#40
Conversation
|
|
||
| adapter: node({ | ||
| mode: 'standalone' | ||
| }) |
There was a problem hiding this comment.
These changes enable astro to run a node server for the custom endpoint below in props.json.ts
| @@ -0,0 +1,94 @@ | |||
| /* eslint-disable no-console */ | |||
There was a problem hiding this comment.
This file ingests the files which should have their props parsed, parses the props out using tsDocGen, compiles that data into a single object, then writes that object to a json file.
| try { | ||
| astroRoot = import.meta | ||
| .resolve('@patternfly/patternfly-doc-core') | ||
| .replace('dist/cli/cli.js', '') | ||
| .replace('file://', '') | ||
| } catch (e) { | ||
| if (e.code === 'ERR_MODULE_NOT_FOUND') { | ||
| astroRoot = process.cwd() | ||
| } else { | ||
| console.error('Error resolving astroRoot', e) | ||
| } | ||
| } |
There was a problem hiding this comment.
This change just makes it so that we can run the docs-core locally more easily
| @@ -0,0 +1,207 @@ | |||
| import { readFile } from 'fs/promises' | |||
There was a problem hiding this comment.
This file is 99% just tsDocGen from the current docs framework. I'd like us to clean this up at some point, but that that is best left for further down the road.
| @@ -0,0 +1,31 @@ | |||
| export const config = { | |||
There was a problem hiding this comment.
This top level config files is also something that I just added to make running this repo locally easier.
| } | ||
|
|
||
| type PropsTableProps = { | ||
| interface PropsTableProps extends React.HTMLProps<HTMLDivElement> { |
There was a problem hiding this comment.
I needed to make this change so that we could pass a key prop to the tables without TS throwing an error.
src/pages/[section]/[...id].astro
Outdated
| async function getPropsData(propComponents: string[]) { | ||
| if (!propComponents.length) { | ||
| return [] | ||
| } | ||
|
|
||
| const url = new URL(`/props?components=${propComponents}`, Astro.url) | ||
| const response = await fetch(url) | ||
| const propsData = await response.json() | ||
|
|
||
| return propsData | ||
| } |
There was a problem hiding this comment.
At its root the new process works by the frontend hitting the backend /props route with its propComponents value as a query param. The backend then returns only the requested components prop data in json.
| import { readFileSync } from 'fs' | ||
| import { join } from 'path' | ||
|
|
||
| const propsFilePath = join(process.cwd(), 'dist', 'props.json') | ||
| const propsData = readFileSync(propsFilePath) | ||
| const props = JSON.parse(propsData.toString()) | ||
|
|
||
| export const prerender = false | ||
|
|
||
| export async function GET({ request }: { request: Request }) { | ||
| const queryParams = new URL(request.url).searchParams | ||
| const components = queryParams.get('components') | ||
| const componentsArray = components?.split(',') | ||
| const propsData = componentsArray?.map((component) => props[component]) | ||
|
|
||
| return new Response(JSON.stringify(propsData)) | ||
| } |
There was a problem hiding this comment.
This is what creates the custom endpoint to get the props data from the frontend.
| id: Contribute | ||
| title: Contribute to PatternFly | ||
| section: get-started | ||
| propComponents: ['Button', 'BadgeCountObject'] |
There was a problem hiding this comment.
This is just for testing/demo purposes.
| .resolve('@patternfly/patternfly-doc-core') | ||
| .replace('dist/cli/cli.js', '') | ||
| .replace('file://', '') | ||
| async function generateProps(program: Command, forceProps: boolean = false) { |
There was a problem hiding this comment.
Just curious, what would the expected use case of forceProps be?
There was a problem hiding this comment.
Whenever we want to enforce the props being generated on the CLI internals side regardless of if the user passes the --props flag, such as for when doing a production build.
| // { | ||
| // include: ['*/@patternfly/react-core/src/**/*.tsx'], | ||
| // exclude: [ | ||
| // '/**/examples/**', | ||
| // '/**/__mocks__/**', | ||
| // '/**/__tests__/**', | ||
| // '/**/*.test.tsx', | ||
| // ], | ||
| // }, |
There was a problem hiding this comment.
Is this expected to be removed or uncommented before merging?
There was a problem hiding this comment.
Nope I was planning to leave it commented in the template file like the content entry, but I'm not dead set on that.
| (type && type.name) || | ||
| (type && (type.raw || type.name)) || | ||
| (tsType && (tsType.raw || tsType.name)) || | ||
| 'No type info', |
There was a problem hiding this comment.
Could either remove this or update the PropsTable since I had included a fallback there with a similar string.
There was a problem hiding this comment.
Yeah we can if you want, I just wasn't trying to mess with tsDocGen any more than I needed to at the moment.
src/pages/[section]/[...id].astro
Outdated
| propsData | ||
| .filter((comp: any) => !!comp) | ||
| .map((component: any) => ( |
There was a problem hiding this comment.
Would be something in a future update, but wondering if we want to tweak the order the prop tables get rendered. Believe currently it's just based on whatever order the propComponents array in frontmatter is written, but would sorting alphabetically make more sense? Or some other way to dictate what the order should be rather than manually having to know "Component X should be rendered before Component C because Reasons"
There was a problem hiding this comment.
I think I lean towards just letting the order be dictated by the propsData order? Just because it's the simplest solution and lets the order be dictated consumer side. But I'm not firmly against us sorting it some way theoretically.
|
🎉 This PR is included in version 1.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Closes #41
To test this PR:
build:props(this will happen automatically when the server is started via the CLI)