Add md generation

This commit is contained in:
Benjamin Bädorf 2022-03-21 22:26:52 +01:00
parent fdb8ffcf2d
commit 59b01e3e88
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
5 changed files with 89 additions and 8 deletions

View file

@ -159,7 +159,7 @@ export interface IBlockDefinition<T> {
icon?: string; icon?: string;
getDefaultData: T; getDefaultData: T;
edit: Component<IBlockProps<T>>; edit: Component<IBlockProps<T>>;
display: Component<IBlockProps<T>>; view: Component<IBlockProps<T>>;
} }
/** /**

View file

@ -22,6 +22,7 @@ export default defineComponent({
const block = exampleData; const block = exampleData;
onMounted(async () => { onMounted(async () => {
// getBlock could be used to access the block data inside the editor
const { getBlock } = await startSchlechtenburg( const { getBlock } = await startSchlechtenburg(
'#example-editor', '#example-editor',
{ {
@ -39,8 +40,7 @@ export default defineComponent({
// This callback will be alled any time the block data gets updated // This callback will be alled any time the block data gets updated
onUpdate: (blockData) => { onUpdate: (blockData) => {
console.log('Got onUpdate', blockData); console.log('got update', blockData);
console.log('getBlock', getBlock());
} }
}, },
) )

View file

@ -18,7 +18,6 @@ export default defineComponent({
setup(props) { setup(props) {
const docs = props.docs; const docs = props.docs;
console.log(docs);
return () => <section class="docs component-docs"> return () => <section class="docs component-docs">
<header class="docs--header"> <header class="docs--header">
<h2 <h2

View file

@ -3,8 +3,6 @@ import { defineComponent } from 'vue';
import { ComponentDoc } from 'vue-docgen-api'; import { ComponentDoc } from 'vue-docgen-api';
import { DeclarationReflection } from 'typedoc'; import { DeclarationReflection } from 'typedoc';
import { getByName } from '../docs';
import { getShortPackageName } from './package';
import ComponentDocs from './ComponentDocs'; import ComponentDocs from './ComponentDocs';
import TSDocs from './TSDocs'; import TSDocs from './TSDocs';
@ -28,7 +26,7 @@ export default defineComponent({
} }
const { lib, components } = docs; const { lib, components } = docs;
return () => <main class="package"> return () => <div class="package">
<h1>{lib.name}</h1> <h1>{lib.name}</h1>
<p>{lib.comment}</p> <p>{lib.comment}</p>
<p>{lib.flags}</p> <p>{lib.flags}</p>
@ -40,6 +38,6 @@ export default defineComponent({
return <TSDocs docs={child}></TSDocs> return <TSDocs docs={child}></TSDocs>
})} })}
</main>; </div>;
}, },
}); });

View file

@ -0,0 +1,84 @@
import { DeclarationReflection } from 'typedoc';
import { ComponentDoc } from 'vue-docgen-api';
import { getByName } from '../docs';
import { getShortPackageName } from './package';
const getTypeParamString = (params: TypeParameterReflection[]) => `<${params.map(p => p.name).join(', ')}>`;
const generateComponentDoc = (docs: ComponentDoc) => `
## ${docs.exportName}
<p class="docs--type">Component <code>&lt;${docs.displayName} /&gt;</code></p>
${docs.description}
### Props
${(docs.props || []).map(prop => `
#### ${prop.name}
${prop.description}
${prop.type ? `Type: <code>${prop.type.name}</code>` : ''}
${prop.defaultValue ? `Default: <code>${prop.defaultValue.value}</code>`: null}
</div>`).join('\n\n')}
`;
const generateTSDocs = (docs: DeclarationReflection) => `
## ${docs.name}${docs.typeParameters ? getTypeParamString(docs.typeParameters) : ''}
<p class="docs--type">{docs.kindString}</p>
${docs.comment?.shortText || ''}
${(docs.children || []).map((child: DeclarationReflection) => `
\`\`\`
${child.name}: ${child.type?.name}
\`\`\`
`).join('\n\n')}
`;
const generateChildren = (
children: DeclarationReflection[] = [],
components: ComponentDoc[],
) => children.map((child) => {
const componentDocs = components.find((c: ComponentDoc) => c.exportName === child.name);
if (componentDocs) {
return generateComponentDoc(componentDocs);
}
return generateTSDocs(child);
});
/**
* 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 const generatePackageMd = (packageName: string) => {
const docs = getByName(getShortPackageName(Array.isArray(packageName) ? packageName[0] : packageName));
if (!docs) {
return `Could not find package docs for ${packageName}`;
}
const { lib, components } = docs;
const markdown = `
# ${lib.name}
${lib.comment}
${lib.flags}
${generateChildren(lib.children, components)}
`;
return markdown
.trim()
.replace(/\n\n+/, '\n\n');
}