Skip to content

Commit a08acac

Browse files
authored
feat(docs): generate docs for slots (#1363)
fixes #1362
1 parent 59f9b1f commit a08acac

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/compiler/docs/generate-doc-data.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async function getComponents(config: d.Config, compilerCtx: d.CompilerCtx, diagn
5252
.filter(([_, member]) => isDocsPublic(member.jsdoc));
5353

5454
const readme = await getUserReadmeContent(compilerCtx, readmePath);
55+
const docsTags = generateDocsTags(moduleFile.cmpMeta.jsdoc);
5556

5657
return {
5758
dirPath,
@@ -61,15 +62,16 @@ async function getComponents(config: d.Config, compilerCtx: d.CompilerCtx, diagn
6162
usagesDir,
6263
tag: moduleFile.cmpMeta.tagNameMeta,
6364
readme,
65+
docsTags,
6466
docs: generateDocs(readme, moduleFile.cmpMeta.jsdoc),
65-
docsTags: generateDocsTags(moduleFile.cmpMeta.jsdoc),
6667
usage: await generateUsages(config, compilerCtx, usagesDir),
6768
encapsulation: getEncapsulation(moduleFile.cmpMeta),
6869

6970
props: getProperties(membersMeta),
7071
methods: getMethods(membersMeta),
7172
events: getEvents(moduleFile.cmpMeta),
72-
styles: getStyles(moduleFile.cmpMeta)
73+
styles: getStyles(moduleFile.cmpMeta),
74+
slots: getSlots(docsTags)
7375
};
7476
});
7577
return Promise.all(promises);
@@ -170,6 +172,22 @@ function getStyles(cmpMeta: d.ComponentMeta): d.JsonDocsStyle[] {
170172
});
171173
}
172174

175+
function getSlots(tags: d.JsonDocsTags[]): d.JsonDocsSlot[] {
176+
return tags
177+
.filter(tag => tag.name === 'slot' && tag.text)
178+
.map(({text}) => {
179+
const [namePart, ...rest] = (' ' + text).split(' - ');
180+
return {
181+
name: namePart.trim(),
182+
docs: rest.join(' - ').trim()
183+
};
184+
})
185+
.sort((a, b) => {
186+
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
187+
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
188+
return 0;
189+
});
190+
}
173191

174192
function getAttrName(memberMeta: d.MemberMeta) {
175193
if (memberMeta.attribName) {

src/compiler/docs/generate-json-docs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function generateJsonDocs(compilerCtx: d.CompilerCtx, jsonOutputs:
1414
methods: cmp.methods,
1515
events: cmp.events,
1616
styles: cmp.styles,
17+
slots: cmp.slots
1718
}))
1819
};
1920
const jsonContent = JSON.stringify(json, null, 2);

src/compiler/docs/generate-readme-docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { eventsToMarkdown } from './markdown-events';
55
import { methodsToMarkdown } from './markdown-methods';
66
import { usageToMarkdown } from './markdown-usage';
77
import { stylesToMarkdown } from './markdown-css-props';
8+
import { slotsToMarkdown } from './markdown-slots';
89

910
export async function generateReadmeDocs(config: d.Config, compilerCtx: d.CompilerCtx, readmeOutputs: d.OutputTargetDocsReadme[], docs: d.JsonDocs) {
1011
await Promise.all(docs.components.map(async cmpData => {
@@ -44,6 +45,7 @@ export function generateMarkdown(userContent: string, cmp: d.JsonDocsComponent)
4445
...propsToMarkdown(cmp.props),
4546
...eventsToMarkdown(cmp.events),
4647
...methodsToMarkdown(cmp.methods),
48+
...slotsToMarkdown(cmp.slots),
4749
...stylesToMarkdown(cmp.styles),
4850
`----------------------------------------------`,
4951
'',
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as d from '../../declarations';
2+
import { MarkdownTable } from './docs-util';
3+
4+
5+
export function slotsToMarkdown(slots: d.JsonDocsSlot[]) {
6+
const content: string[] = [];
7+
if (slots.length === 0) {
8+
return content;
9+
}
10+
11+
content.push(`## Slots`);
12+
content.push(``);
13+
14+
const table = new MarkdownTable();
15+
table.addHeader(['Slot', 'Description']);
16+
17+
slots.forEach(style => {
18+
table.addRow([
19+
style.name === '' ? '' : `\`"${style.name}"\``,
20+
style.docs
21+
]);
22+
});
23+
24+
content.push(...table.toMarkdown());
25+
content.push(``);
26+
content.push(``);
27+
28+
return content;
29+
}

src/declarations/docs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface JsonDocsComponent {
2828
methods: JsonDocsMethod[];
2929
events: JsonDocsEvent[];
3030
styles: JsonDocsStyle[];
31+
slots: JsonDocsSlot[];
3132
}
3233

3334
export interface JsonDocsTags {
@@ -91,3 +92,8 @@ export interface JsonDocsStyle {
9192
docs: string;
9293
annotation: string;
9394
}
95+
96+
export interface JsonDocsSlot {
97+
name: string;
98+
docs: string;
99+
}

0 commit comments

Comments
 (0)