This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.ts
169 lines (139 loc) · 4.21 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { paramCase } from 'change-case'
import dedent from 'dedent-tabs'
import hljs from 'highlight.js'
import marked from 'marked'
import Vue, { ComponentOptions } from 'vue'
import { InfoAddonOptions } from '../options'
import { ComponentInfo, StoryInfo } from '../types/info'
import { getJSXFromRenderFn } from '../utils/getJSXFromRenderFn'
import { decideTargets } from './decideTargets'
import { extractDocgenInfo } from './extractDocgenInfo'
import { getProps } from './getProps'
export function extract(
story: ComponentOptions<Vue>,
kindName: string,
storyName: string,
options: InfoAddonOptions
): StoryInfo {
const targets = decideTargets(story, options)
const descriptions =
getDescriptionsFromStory(story) || formatPropsDescription(story)
const components = Object.keys(targets).map<ComponentInfo>(name => {
const component = targets[name]
const kebabName = paramCase(name)
const propDescriptions =
(descriptions[kebabName] && descriptions[kebabName].props) || {}
const eventDescriptions =
(descriptions[kebabName] && descriptions[kebabName].events) || {}
const slotDescriptions =
(descriptions[kebabName] && descriptions[kebabName].slots) || {}
if (options.useDocgen && '__docgenInfo' in component) {
const partial = extractDocgenInfo(component)
const hydrateStoryDescription = <
T extends { name: string; description?: string }
>(
defs: T[],
description: Description
): T[] => {
return defs.map(def => {
if (def.name in description) {
return {
...def,
description: description[def.name]
}
}
return def
})
}
return {
name,
...partial,
props:
partial.props &&
hydrateStoryDescription(partial.props, propDescriptions),
events:
partial.events &&
hydrateStoryDescription(partial.events, eventDescriptions),
slots:
partial.slots &&
hydrateStoryDescription(partial.slots, slotDescriptions)
}
}
const props = getProps(component).map(prop => {
if (prop.name in propDescriptions) {
return {
...prop,
description: propDescriptions[prop.name]
}
}
return prop
})
const events = Object.keys(eventDescriptions).map(eventName => {
return {
name: eventName,
description: eventDescriptions[eventName]
}
})
const slots = Object.keys(slotDescriptions).map(slotName => {
return {
name: slotName,
description: slotDescriptions[slotName]
}
})
return { name, props, events, slots }
})
// Set up markdown renderer for summary
const renderer = new marked.Renderer()
renderer.code = (code, lang) =>
`<pre><code class="hljs">${
hljs.highlightAuto(code, lang ? [lang] : undefined).value
}</code></pre>`
marked.setOptions({ renderer })
return {
title: kindName,
subtitle: storyName,
summary: marked(dedent(options.summary)),
storySource: dedent(
story.template || getJSXFromRenderFn(story.render! as any)
),
jsxStory: !!story.render,
components
}
}
// Description for props, events and methods.
interface Description {
[name: string]: string
}
// Description object user
interface Descriptions {
[componentName: string]: {
props?: Description
events?: Description
slots?: Description
}
}
const getDescriptionsFromStory = (story: any): Descriptions | null => {
if (!story.description) {
return null
}
const ret: Descriptions = {}
for (const component of Object.keys(story.description)) {
ret[paramCase(component)] = story.description[component]
}
return ret
}
const formatPropsDescription = (story: any): Descriptions => {
if (!story.propsDescription) {
return {}
}
console.warn(
'[storybook-addon-vue-info] `propsDescription` is deprecated. Please consider switching to `description.props`'
)
const components: Descriptions = {}
for (const component of Object.keys(story.propsDescription)) {
components[paramCase(component)] = {
props: story.propsDescription[component]
}
}
return components
}