Skip to content

Commit

Permalink
Add table with config options to docs (#1588)
Browse files Browse the repository at this point in the history
* 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
ovidiuch committed Dec 17, 2023
1 parent 6e8136d commit c9d42ed
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 deletions.
28 changes: 28 additions & 0 deletions docs/components/ConfigTable.mdx
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>
);
}
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@svgr/webpack": "8.1.0",
"@types/json-schema": "^7.0.15",
"autoprefixer": "10.4.16",
"next": "14.0.3",
"nextra": "2.13.2",
Expand Down
15 changes: 10 additions & 5 deletions docs/pages/docs/configuration/cosmos-config.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConfigTable } from '../../../components/ConfigTable.mdx';
import { Callout } from 'nextra/components';

# `cosmos.config.json`
Expand All @@ -9,11 +10,6 @@ config editing.

## Config Schema

<Callout type="info">
**To learn about all available options in the Cosmos config see
[config.schema.json](https://github.com/react-cosmos/react-cosmos/blob/main/packages/react-cosmos/config.schema.json).**
</Callout>

You can enhance your Cosmos config with autocomplete in code editors like VS Code.

```jsonc filename="cosmos.config.json"
Expand Down Expand Up @@ -44,3 +40,12 @@ Alternatively, you can reference the local Cosmos config schema in your workspac
}
]
```

## Config Options

<Callout type="info">
To understand the Cosmos config options in more detail see
[config.schema.json](https://github.com/react-cosmos/react-cosmos/blob/main/packages/react-cosmos/config.schema.json).
</Callout>

<ConfigTable />
49 changes: 49 additions & 0 deletions docs/utils/flattenConfigSchema.ts
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],
};
}
12 changes: 6 additions & 6 deletions packages/react-cosmos/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"minLength": 1
},
"detectLocalPlugins": {
"description": "Automatically detect and load local plugins [default: true].",
"description": "Automatically detect and load local plugins. [default: true]",
"type": "boolean"
},
"disablePlugins": {
"description": "Disable plugins",
"description": "Disable plugins. [default: false]",
"type": "boolean"
},
"staticPath": {
Expand Down Expand Up @@ -49,7 +49,7 @@
"minLength": 1
},
"ignore": {
"description": "Patterns for ignoring fixture and decorator files (eg. [\"**/dist/**\"]]).",
"description": "Patterns for ignoring fixture and decorator files (eg. [\"**/dist/**\"]).",
"type": "array",
"items": { "type": "string", "minLength": 1 },
"uniqueItems": true
Expand Down Expand Up @@ -93,17 +93,17 @@
"additionalProperties": false,
"properties": {
"certPath": {
"description": "Path of a certificate file",
"description": "Path of a certificate file.",
"type": "string"
},
"keyPath": {
"description": "Path of a certificate's key file",
"description": "Path of a certificate's key file.",
"type": "string"
}
}
},
"httpProxy": {
"description": "Proxy some URLs to a different HTTP server (eg. an API backend dev server). Similar to devServer.proxy in webpack config.",
"description": "Proxy some URLs to a different HTTP server (eg. an API backend dev server).",
"type": "object",
"additionalProperties": true,
"patternProperties": {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2761,7 +2761,7 @@
"@types/tough-cookie" "*"
parse5 "^7.0.0"

"@types/json-schema@*", "@types/json-schema@^7.0.12", "@types/json-schema@^7.0.8", "@types/json-schema@^7.0.9":
"@types/json-schema@*", "@types/json-schema@^7.0.12", "@types/json-schema@^7.0.15", "@types/json-schema@^7.0.8", "@types/json-schema@^7.0.9":
version "7.0.15"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==
Expand Down

0 comments on commit c9d42ed

Please sign in to comment.