-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table with config options to docs (#1588)
* Add table with config options to docs * Make config schema more consistent * Update cosmos-config.mdx * Sort options * Install JSON schema types in docs package * Organize code
- Loading branch information
Showing
6 changed files
with
95 additions
and
12 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,28 @@ | ||
import { flattenConfigSchema } from '../utils/flattenConfigSchema'; | ||
|
||
export function ConfigTable() { | ||
const jsonSchema = require('../../packages/react-cosmos/config.schema.json'); | ||
const configItems = flattenConfigSchema(jsonSchema.properties); | ||
return ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Option</th> | ||
<th>Description</th> | ||
<th>Default</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{configItems.map(item => ( | ||
<tr key={item.name}> | ||
<td> | ||
<code>{item.name}</code> | ||
</td> | ||
<td>{item.description}</td> | ||
<td>{item.default && <code>{item.default}</code>}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
); | ||
} |
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
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,49 @@ | ||
import { JSONSchema4 } from 'json-schema'; | ||
|
||
type ConfigItem = { | ||
name: string; | ||
description: string; | ||
default?: string; | ||
}; | ||
|
||
export function flattenConfigSchema( | ||
properties: JSONSchema4['properties'] = {}, | ||
prefixes: string[] = [] | ||
) { | ||
const items: ConfigItem[] = []; | ||
|
||
Object.keys(properties) | ||
.filter(key => !key.startsWith('$')) | ||
.filter(key => !['vite', 'webpack', 'ui'].includes(key)) | ||
.sort((a, b) => a.localeCompare(b)) | ||
.forEach(key => { | ||
const value = properties[key]; | ||
|
||
if (value.type === 'object' && value.properties) { | ||
items.push( | ||
...flattenConfigSchema(value.properties, [...prefixes, key]) | ||
); | ||
} else { | ||
const { description, defaultValue } = parseDesc(value.description); | ||
items.push({ | ||
name: [...prefixes, key].join('.'), | ||
description, | ||
default: defaultValue, | ||
}); | ||
} | ||
}); | ||
|
||
return items; | ||
} | ||
|
||
// Parse description with format "This is a description. [default: foo]" | ||
const descRegex = /^(.+?)( \[default: (.+?)\])?$/; | ||
|
||
function parseDesc(description: string = '') { | ||
const match = description.match(descRegex); | ||
|
||
return { | ||
description: match[1], | ||
defaultValue: match[3], | ||
}; | ||
} |
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