schlechtenburg/packages/docgen/support/generate-pkg-md.mjs

76 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-21 23:58:05 +00:00
const getTypeParamString = (params) => `<${params.map(p => p.name).join(', ')}>`;
const generateComponentDoc = (docs) => `
## ${docs.exportName}
${docs.description || ''}
2022-03-22 20:16:26 +00:00
- **Type**: \`Component\`
2022-03-21 23:58:05 +00:00
### Props
${(docs.props || []).map(prop => `
#### ${prop.name}
${prop.description || ''}
${prop.type ? `
2022-03-22 20:16:26 +00:00
- **Type** \`${prop.type.name}\`
2022-03-21 23:58:05 +00:00
` : ''}
${prop.defaultValue ? `
2022-03-22 20:16:26 +00:00
- **Default value** \`${prop.defaultValue.value}\`
2022-03-21 23:58:05 +00:00
` : ''}
`).join('\n')}
`;
2022-03-22 20:16:26 +00:00
const generateMembersDocs = (children) => children ? `
- **Members**
${(children)
.map((child) => ` - **${child.name}**: \`${child.type?.name}\``)
.join('\n')}
` : '';
2022-03-21 23:58:05 +00:00
const generateTSDocs = (docs) => `
## ${docs.name}${docs.typeParameters ? getTypeParamString(docs.typeParameters) : ''}
${docs.comment?.shortText || ''}
2022-03-22 20:16:26 +00:00
- **Type** \`${docs.kindString}\`
2022-03-21 23:58:05 +00:00
2022-03-22 20:16:26 +00:00
${generateMembersDocs(docs.children)}
2022-03-21 23:58:05 +00:00
`;
const generateChildren = (
children = [],
components,
) => children.map((child) => {
const componentDocs = components.find((c) => c.exportName === child.name);
if (componentDocs) {
return generateComponentDoc(componentDocs);
}
return generateTSDocs(child);
}).join('');
/**
* Generate the full markdown for a package
*
* Takes the package name (e.g. @schlechtenburg/core) and outputs a markdown string ready to be
* consumed by vitepress
*/
export default ({ lib, components }) => {
const markdown = `
# ${lib.name}
${lib.comment ? lib.comment : ''}
${generateChildren(lib.children, components)}
`;
return markdown
.trim()
.replace(/\n\n+/, '\n\n');
}