Skip to content

Commit 7cd19f2

Browse files
committed
build: update api generator
1 parent 631fa5b commit 7cd19f2

File tree

6 files changed

+173
-126
lines changed

6 files changed

+173
-126
lines changed

build/docgen.config.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
'use strict'
23

34
const path = require('path')
4-
const templates = require('./docgen.templates')
5+
const pkg = require('./../package.json')
6+
// const templates = require('./docgen.templates')
57

68
module.exports = {
79
componentsRoot: 'src/components', // the folder where CLI will start searching for components.
810
components: '**/[A-Z]*.ts', // the glob to define what files should be documented as components (relative to componentRoot)
9-
outDir: 'docs/api', // folder to save components docs in (relative to the current working directry)
11+
outDir: `docs/${pkg.config.version_short}/api`, // folder to save components docs in (relative to the current working directry)
1012
getDocFileName: (componentPath) =>
1113
componentPath.replace(/\.ts$/, '.md'), // specify the name of the input md file
1214
getDestFile: (file, config) =>
1315
path.join(config.outDir, file).replace(/\.ts$/, '.api.md'), // specify the name of the output md file
14-
templates,
16+
// templates,
17+
templates: {
18+
// global component template wrapping all others see #templates
19+
component: require('./templates/component'),
20+
events: require('./templates/events'),
21+
props: require('./templates/props'),
22+
slots: require('./templates/slots'),
23+
},
1524
docsRepo: 'profile/repo',
1625
docsBranch: 'master',
1726
docsFolder: '',

build/docgen.templates.js

Lines changed: 0 additions & 123 deletions
This file was deleted.

build/templates/component.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
module.exports = (renderedUsage, doc, config, fileName, requiresMd, { isSubComponent, hasSubComponents }) => {
4+
const { displayName, description, docsBlocks, tags, functional } = doc;
5+
const { deprecated, author, since, version, see, link } = tags || {};
6+
const frontMatter = [];
7+
if (!config.outFile && deprecated) {
8+
// to avoid having the squiggles in the left menu for deprecated items
9+
// use the frontmatter feature of vuepress
10+
frontMatter.push(`title: ${displayName}`);
11+
}
12+
if (hasSubComponents) {
13+
// show more than one level on subcomponents
14+
frontMatter.push('sidebarDepth: 2');
15+
}
16+
return `${frontMatter.length && !isSubComponent
17+
? `
18+
---
19+
${frontMatter.join('\n')}
20+
---
21+
`
22+
: ''}
23+
${isSubComponent || hasSubComponents ? '#' : ''}### ${deprecated ? `~~${displayName}~~` : displayName}
24+
25+
${deprecated ? `> **Deprecated** ${deprecated[0].description}\n` : ''}
26+
${description ? '> ' + description : ''}
27+
28+
${functional ? renderedUsage.functionalTag : ''}
29+
${author ? author.map(a => `Author: ${a.description}\n`) : ''}
30+
${since ? `Since: ${since[0].description}\n` : ''}
31+
${version ? `Version: ${version[0].description}\n` : ''}
32+
${see ? see.map(s => `[See](${s.description})\n`) : ''}
33+
${link ? link.map(l => `[See](${l.description})\n`) : ''}
34+
35+
${renderedUsage.props}
36+
${renderedUsage.methods}
37+
${renderedUsage.events}
38+
${renderedUsage.slots}
39+
${docsBlocks ? '---\n' + docsBlocks.join('\n---\n') : ''}
40+
41+
${requiresMd.length
42+
? '---\n' + requiresMd.map(component => component.content).join('\n---\n')
43+
: ''}
44+
`;
45+
};

build/templates/events.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
function mdclean(input) {
4+
return input.replace(/\r?\n/g, '<br>').replace(/\|/g, '\\|')
5+
}
6+
7+
function formatProperties(properties) {
8+
if (!properties) {
9+
return ''
10+
}
11+
return properties
12+
.map(property => {
13+
const { name, description, type } = property
14+
if (!type) {
15+
return ''
16+
}
17+
return `**${name}** \`${type.names.length ? type.names.join(', ') : ''}\` - ${description}`
18+
})
19+
.join('\n')
20+
}
21+
22+
const tmpl = (events) => {
23+
let ret = ''
24+
events.forEach(evt => {
25+
const { description = '', ...e } = evt
26+
const readableProperties = e.properties ? `${formatProperties(e.properties)}` : ''
27+
ret += `| <code>${mdclean(e.name)}</code> | ${mdclean(readableProperties)} | ${mdclean(description)}\n`
28+
})
29+
return ret
30+
}
31+
32+
module.exports = (events, opt = {}) => {
33+
return `
34+
${opt.isSubComponent || opt.hasSubComponents ? '#' : ''}#### Events
35+
36+
| Event name | Properties | Description |
37+
| -------------- |--------------- | -------------|
38+
${tmpl(events)}
39+
`
40+
}

build/templates/props.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
function mdclean(input) {
3+
return input.replace(/\r?\n/g, '<br>').replace(/\|/g, '\\|')
4+
}
5+
function isTag(v) {
6+
return !!v.content;
7+
}
8+
const renderTags = (tags) => {
9+
if (!tags) {
10+
return '';
11+
}
12+
return Object.entries(tags)
13+
.map(([tag, values]) => {
14+
return values.map(v => `<br/>\`@${tag}\` ${isTag(v) ? v.content : v.description}`).join('');
15+
})
16+
.join('');
17+
};
18+
const tmpl = (props) => {
19+
let ret = '';
20+
props.forEach(pr => {
21+
const p = pr.name
22+
let t = pr.description ?? ''
23+
t += renderTags(pr.tags)
24+
const n = pr.type?.name ?? ''
25+
const v = pr.values?.map(pv => `\`${pv}\``).join(', ') ?? '-'
26+
const d = pr.defaultValue?.value ?? ''
27+
ret += `| <code>${mdclean(p)}</code> | ${mdclean(t)} | ${mdclean(n)} | ${mdclean(v)} | ${mdclean(d)} |\n`;
28+
});
29+
return ret;
30+
};
31+
module.exports = (props, opt = {}) => {
32+
return `
33+
${opt.isSubComponent || opt.hasSubComponents ? '#' : ''}#### Props
34+
| Prop name | Description | Type | Values | Default |
35+
| ------------- | ----------- | --------- | ----------- | ----------- |
36+
${tmpl(props)}
37+
`;
38+
};

build/templates/slots.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
function mdclean(input) {
4+
return input.replace(/\r?\n/g, '<br>').replace(/\|/g, '\\|')
5+
}
6+
7+
const formatBindings = bindings => {
8+
if (!bindings) {
9+
return '';
10+
}
11+
return bindings
12+
.map(binding => {
13+
const { name, description, type } = binding;
14+
if (!type) {
15+
return '';
16+
}
17+
return `**${name}** \`${type.name === 'union' && type.elements
18+
? type.elements.map(({ name: insideName }) => insideName).join(', ')
19+
: type.name}\` - ${description}`;
20+
})
21+
.join('\n');
22+
};
23+
24+
module.exports = (slots, opt = {}) => {
25+
return `
26+
${opt.isSubComponent || opt.hasSubComponents ? '#' : ''}#### Slots
27+
28+
| Name | Description | Bindings |
29+
| ------------- | ------------ | -------- |
30+
${slots
31+
.map(slot => {
32+
const { description, bindings, name } = slot;
33+
const readableBindings = bindings ? `${formatBindings(bindings)}` : '';
34+
return `| ${mdclean(name)} | ${mdclean(description || '')} | ${mdclean(readableBindings)} |`; // replace returns by <br> to allow them in a table cell
35+
})
36+
.join('\n')}
37+
`;
38+
};

0 commit comments

Comments
 (0)