This repository was archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui-guide-builder.ts
92 lines (75 loc) · 2.37 KB
/
ui-guide-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { UIGuideSandbox, UIGuide, UIGuideExample, UIGuideExampleConfig, ComponentAPI, DopeDoc } from './interfaces'
import { bootstrap } from './boostrap'
const slugify = require('slugify')
export function createDopeDocs(sandbox: UIGuideSandbox) {
bootstrap(sandbox)
}
const GuideIDS: {[id: string]: string[]} = {}
export class DocsBuilder implements DopeDoc {
id: string
examples: UIGuideExample[] = []
constructor(public name: string, public description: string, public api?: ComponentAPI) {
const id = slugify(name).toLowerCase()
if (GuideIDS[id]) {
throw new Error(`You already have a UI Guide with the name of ${name}`)
}
GuideIDS[id] = []
this.id = id
}
example(name: string, config: UIGuideExampleConfig): DopeDoc {
const id = `${this.id}-${slugify(name).toLowerCase()}`
if (GuideIDS[this.id].find(i => i === id)) {
throw new Error(`You already have an example for UI Guide ${this.name} with the name of ${name}`)
}
GuideIDS[this.id].push(id)
this.examples.push({
id,
name,
providers: config.providers || [],
showSource: Boolean(config.showSource),
uiGuideName: this.name,
description: config.description,
template: config.template,
context: config.context,
styles: config.styles
})
return this
}
xexample(description: string, config: UIGuideExampleConfig): DopeDoc {
return this
}
Xexample(description: string, config: UIGuideExampleConfig): DopeDoc {
return this.xexample(description, config)
}
}
export function docsFor(
component: string,
description: string,
api: ComponentAPI = {inputs: [], outputs: []}
): DopeDoc {
api = addNA(api);
return new DocsBuilder(component, description, api)
}
function addNA(docsObj) {
let { inputs, outputs } = docsObj;
let parsedInputs;
let parsedOutputs;
const defaultString = 'n/a';
if (inputs) {
parsedInputs = inputs.map((val) => {
if (!val.default || val.default.length <= 1){
val.default = defaultString;
}
return val;
})
}
if (outputs) {
parsedOutputs = outputs.map((val) => {
if (!val.args || val.args.length <= 1) {
val.args = defaultString;
}
return val;
})
}
return docsObj = {inputs: parsedInputs, outputs: parsedOutputs};
}