Skip to content

Commit 300df72

Browse files
authored
feat(docs): include usages in the markdown (#1254)
1 parent 0237291 commit 300df72

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AUTO_GENERATE_COMMENT, NOTE } from './constants';
33
import { propsToMarkdown } from './markdown-props';
44
import { eventsToMarkdown } from './markdown-events';
55
import { methodsToMarkdown } from './markdown-methods';
6+
import { usageToMarkdown } from './markdown-usage';
67
import { stylesToMarkdown } from './markdown-css-props';
78

89
export async function generateReadmeDocs(config: d.Config, compilerCtx: d.CompilerCtx, readmeOutputs: d.OutputTargetDocsReadme[], docs: d.JsonDocs) {
@@ -39,6 +40,7 @@ export function generateMarkdown(userContent: string, cmp: d.JsonDocsComponent)
3940
AUTO_GENERATE_COMMENT,
4041
'',
4142
'',
43+
...usageToMarkdown(cmp.usage),
4244
...propsToMarkdown(cmp.props),
4345
...eventsToMarkdown(cmp.events),
4446
...methodsToMarkdown(cmp.methods),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as d from '../../declarations';
2+
import { captializeFirstLetter } from '../../util/helpers';
3+
4+
export function usageToMarkdown(usages: d.JsonDocsUsage) {
5+
const content: string[] = [];
6+
const merged = mergeUsages(usages);
7+
if (merged.length === 0) {
8+
return content;
9+
}
10+
11+
content.push(`## Usage`);
12+
13+
merged.forEach(({name, text}) => {
14+
content.push('');
15+
content.push(`### ${captializeFirstLetter(name)}`);
16+
content.push('');
17+
content.push(text);
18+
content.push('');
19+
}),
20+
21+
content.push('');
22+
content.push('');
23+
24+
return content;
25+
}
26+
27+
export function mergeUsages(usages: d.JsonDocsUsage) {
28+
const keys = Object.keys(usages);
29+
const map = new Map<string, string[]>();
30+
keys.forEach(key => {
31+
const usage = usages[key].trim();
32+
const array = map.get(usage) || [];
33+
array.push(key);
34+
map.set(usage, array);
35+
});
36+
const merged: {name: string, text: string}[] = [];
37+
map.forEach((value, key) => {
38+
merged.push({
39+
name: value.join(' / '),
40+
text: key
41+
});
42+
});
43+
return merged;
44+
}
45+

0 commit comments

Comments
 (0)