-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVueDocumenter.vue
702 lines (605 loc) · 28.5 KB
/
VueDocumenter.vue
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<template>
<div :class="cssClassContainer || cssOverrideClasses.container">
<div style="display: none;">
<slot></slot>
</div>
<div :class="cssClassRow ||cssOverrideClasses.row">
<div v-if="enableTableOfContents" :class="cssClassTableOfContentsColumn || cssOverrideClasses.tableOfContentsColumn">
<div :class="cssClassCard || cssOverrideClasses.card" style="position: sticky; top: 0;">
<h2>Components</h2>
<ul :class="cssClassTableOfContentsColumnList || cssOverrideClasses.tableOfContentsColumnList">
<li v-for="component in loadedComponents" :key="`table-of-contents-${component.name}`">
<a :href="`#${getKebabCaseFromCamelCase(component.name)}`">{{ getKebabCaseFromCamelCase(component.name) }}</a>
</li>
</ul>
</div>
</div>
<div :class="cssClassComponentsColumn || cssOverrideClasses.componentsColumn">
<div v-for="component in loadedComponents" :class="cssClassComponentsColumnComponent || cssOverrideClasses.componentsColumnComponent" style="margin-bottom: 3rem;" :key="`component-${component.name}`">
<h1 :id="getKebabCaseFromCamelCase(component.name)"><{{ getKebabCaseFromCamelCase(component.name) }}></h1>
<p v-if="component.description" v-html="component.description"></p>
<h2>Properties</h2>
<div :class="cssClassComponentsColumnComponentTableWrapper || cssOverrideClasses.componentsColumnComponentTableWrapper" style="margin-bottom: 1rem;">
<table :class="cssClassComponentsColumnComponentTable || cssOverrideClasses.componentsColumnComponentTable">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Default Value</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<template v-if="component.properties.length > 0">
<tr v-for="property in component.properties" :key="`property-${property.name}`" :class="{ 'deprecated': property.deprecated }">
<td>
{{ property.type !== 'string' ? ':' : '' }}{{ property.name }}
<span v-if="property.required" :class="cssClassBadgeRequired || cssOverrideClasses.badgeRequired" :title="property.required">Required</span>
<span v-if="property.deprecated" :class="cssClassBadgeDeprecated || cssOverrideClasses.badgeDeprecated" :title="property.deprecated" data-tippy>Deprecated <i class="fas fa-question-circle"></i></span>
</td>
<td v-html="property.description"></td>
<td>{{ property.type }}</td>
<td>
<div v-html="getHighlightedCodeString(property.defaultValue, (property.type === 'string' ? 'none' : 'javascript'))"></div>
</td>
<td>
<div v-html="getHighlightedCodeString(property.example, (property.type === 'string' ? 'none' : 'javascript'))"></div>
</td>
</tr>
</template>
<tr v-else>
<td colspan="999">
No properties
</td>
</tr>
</tbody>
</table>
</div>
<h2>Events (<a href="https://vuejs.org/v2/api/#vm-emit" target="_blank">Vue Docs</a>)</h2>
<div :class="cssClassComponentsColumnComponentTableWrapper || cssOverrideClasses.componentsColumnComponentTableWrapper" style="margin-bottom: 1rem;">
<table :class="cssClassComponentsColumnComponentTable || cssOverrideClasses.componentsColumnComponentTable">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Example Value</th>
<th>Usage</th>
</tr>
</thead>
<tbody>
<template v-if="component.events.length > 0">
<tr v-for="event in component.events" :key="`event-${event.name}`">
<td>{{ event.name }}</td>
<td v-html="event.description"></td>
<td v-html="getHighlightedCodeString(event.example, 'javascript')"></td>
<td v-html="getEventExampleString(component, event)"></td>
</tr>
</template>
<tr v-else>
<td colspan="999">No events</td>
</tr>
</tbody>
</table>
</div>
<div :class="cssClassRow || cssOverrideClasses.row">
<div :class="cssClassExampleMinimalColumn || cssOverrideClasses.exampleMinimal">
<h2>Minimal Example</h2>
<div v-html="getHighlightedCodeString(getComponentExampleHtml(component, true), 'html')"></div>
</div>
<div :class="cssClassExampleFullColumn || cssOverrideClasses.exampleFull">
<h2>Full Example</h2>
<div v-html="getHighlightedCodeString(getComponentExampleHtml(component), 'html')"></div>
</div>
</div>
</div>
</div>
</div>
<div :class="cssClassInstructions || cssOverrideClasses.instructions">
<div>
<h2>If you're building a new Vue component and you want it to be auto-documented here</h2>
<div>
<p>
<span :class="cssClassBadgeRequired || cssOverrideClasses.badgeRequired">Required</span> The component must have it's properties defined with <code>type</code> and <code>default</code> properties, see <a href="https://vuejs.org/v2/guide/components-props.html#Prop-Validation" target="_blank">Prop Validation</a>
</p>
<p>
<span :class="cssClassBadgeOptional || cssOverrideClasses.badgeOptional">Optional</span> The component maybe also include a top level <code>meta</code> property with a description, slot, and event availability, ex.
<code style="white-space: pre;">
meta: {
description: 'Optional component description that\'ll get piped into the documentation',
slots: {
default: {
type: 'component',
valid: ['ComponentNameOne', 'ComponentNameTwo'],
},
named: [{
name: 'filters',
type: 'component',
valid: ['OtherComponentName'],
}],
},
events: [{
name: 'date-selected',
description: 'On date click',
example: '2019-02-11',
}],
},
</code>
</p>
<p>
<span :class="cssClassBadgeOptional || cssOverrideClasses.badgeOptional">Optional</span> The component's properties may also include an additional <code>meta</code> property in the property definition, ex.
<code style="white-space: pre;">
meta: {
required: [true|false],
example: 'Example implementation code',
description: 'Optional description of the property',
deprecated: 'Deprecation note that will appear on hover',
},
</code>
</p>
</div>
</div>
</div>
</div>
</template>
<script>
import Prism from 'prismjs';
import NormalizeWhitespace from 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace';
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true,
});
export default {
meta: {
description: 'See Vue Documenter on <a href="https://github.com/theianjohnson/vue-documenter" target="_blank">Github</a> and <a href="https://npmjs.org/vue-documenter" target="_blank">NPM</a>',
slots: {
default: {
type: 'component',
valid: ['AnyComponent'],
},
},
},
components: {
},
props: {
cssOverrideClasses: {
type: Object,
default: () => {
return {
badgeDeprecated: 'badge badge-warning',
badgeOptional: 'badge badge-secondary',
badgeRequired: 'badge badge-danger',
card: 'card card-body',
componentsColumn: 'col-sm-9',
componentsColumnComponent: 'card card-body',
componentsColumnComponentTableWrapper: 'table-responsive',
componentsColumnComponentTable: 'table table-striped',
container: 'container-fluid',
exampleMinimal: 'col-sm-6',
exampleFull: 'col-sm-6',
instructions: 'alert alert-info',
tableOfContentsColumn: 'col-sm-3',
tableOfContentsColumnList: '',
row: 'row',
}
},
meta: {
deprecated: 'Override classes are now split out into their own properties',
example: '{ card: \'your-custom-card-class\' }',
}
},
cssClassBadgeDeprecated: {
type: String,
default: 'badge badge-warning',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassBadgeOptional: {
type: String,
default: 'badge badge-secondary',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassBadgeRequired: {
type: String,
default: 'badge badge-danger',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassCard: {
type: String,
default: 'card card-body',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassComponentsColumn: {
type: String,
default: 'col-sm-9',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassComponentsColumnComponent: {
type: String,
default: 'card card-body',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassComponentsColumnComponentTableWrapper: {
type: String,
default: 'table-responsive',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassComponentsColumnComponentTable: {
type: String,
default: 'table table-striped',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassContainer: {
type: String,
default: 'container-fluid',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassExampleMinimalColumn: {
type: String,
default: 'col-sm-6',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassExampleFullColumn: {
type: String,
default: 'col-sm-6',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassInstructions: {
type: String,
default: 'alert alert-info',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassTableOfContentsColumn: {
type: String,
default: 'col-sm-3',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassTableOfContentsColumnList: {
type: String,
default: '',
meta: {
example: 'your-custom-css-class-string',
},
},
cssClassRow: {
type: String,
default: 'row',
meta: {
example: 'your-custom-css-class-string',
},
},
enableTableOfContents: {
type: Boolean,
default: true,
meta: {
example: 'false',
description: 'Show or hide the table of contents',
},
},
},
data() {
return {
loadedComponents: [],
};
},
created() {
this.cssClasses = { ...this.cssClasses, ...this.cssOverrideClasses };
},
mounted() {
this.loadComponents();
},
methods: {
loadComponents() {
const components = this.$slots.default && this.$slots.default.filter(vnode => vnode.tag !== undefined);
if (!components) {
return [];
}
for (let i = 0; i < components.length; i += 1) {
const properties = this.loadProperties(components[i]);
const component = {
name: components[i].componentOptions.tag,
description: components[i].componentInstance.$options.meta && components[i].componentInstance.$options.meta.description,
properties: [...properties],
events: (components[i].componentInstance.$options.meta && components[i].componentInstance.$options.meta.events) ? components[i].componentInstance.$options.meta.events : [],
slots: (components[i].componentInstance.$options.meta && components[i].componentInstance.$options.meta.slots) ? components[i].componentInstance.$options.meta.slots : [],
};
this.loadedComponents.push(component);
}
return true;
},
getComponentExampleHtml(component, minimal = false) {
const componentTag = this.getKebabCaseFromCamelCase(component.name);
// Build the events HTML
let events = '';
if(!minimal) {
events = component.events.map(event => `\t@${event.name}="someFunction"`).join("\n");
if (events) {
events = `\n${events}\n`;
}
}
// Build the properties HTML
const componentProperties = minimal ? component.properties && component.properties.filter(property => property.required && !property.deprecated) : component.properties && component.properties.filter(property => !property.deprecated);
let properties = componentProperties.map(property => `\t${property.type === 'string' ? '' : ':'}${property.name}="${property.example.replace(/[\r\n\t]+/g, ' ').replace(/\s+/g, ' ').trim()}"`).join("\n");
if (properties) {
properties = `${(events ? '' : "\n")}${properties}\n`;
}
// Build the slots HTML
let slots = '';
if(!minimal) {
if (component.slots) {
if (component.slots.named && component.slots.named.length > 0) {
component.slots.named.forEach((componentSlot) => {
let validSlotContents = '';
if (componentSlot.type === 'component') {
componentSlot.valid.forEach((validComponent) => {
const validComponentTagName = this.getKebabCaseFromCamelCase(validComponent);
validSlotContents = `<${validComponentTagName}></${validComponentTagName}>`;
});
}
slots = `\n${slots}\t<template v-slot:${componentSlot.name}>${validSlotContents}</template>`;
});
}
if (component.slots.default) {
slots = `${slots}\n\t<template v-slot:default>`;
let validSlotContents = '';
if (component.slots.default.type === 'component') {
component.slots.default.valid.forEach((validComponent) => {
const validComponentTagName = this.getKebabCaseFromCamelCase(validComponent);
validSlotContents = `<${validComponentTagName}></${validComponentTagName}>`;
slots = `${slots}\n\t\t${validSlotContents}`;
});
}
slots = `${slots}\n\t</template>`;
}
}
if (slots) {
slots = `${slots}\n`;
}
}
const html = `<${componentTag}${events}${properties}>${slots}</${componentTag}>`;
return html;
},
/*
getComponentExampleHtmlWithMarkup(component, minimal = false) {
const componentTag = this.getKebabCaseFromCamelCase(component.name);
// Build the properties HTML
const componentProperties = minimal ? component.properties && component.properties.filter(property => property.required && !property.deprecated) : component.properties && component.properties.filter(property => !property.deprecated);
let properties = componentProperties.map(property => `<span class="property">${property.type === 'string' ? '' : ':'}${property.name}</span>="<span class="value">${property.example}</span>"`).join('<br>');
if (properties) {
properties = `<br>${properties}<br>`;
}
// Build the slots HTML
let slots = '';
if(!minimal) {
if (component.slots) {
if (component.slots.named && component.slots.named.length > 0) {
component.slots.named.forEach((componentSlot) => {
if (componentSlot.type === 'component') {
componentSlot.valid.forEach((validComponent) => {
const validComponentTagName = this.getKebabCaseFromCamelCase(validComponent);
slots = `
${slots}
<br>
<div class="slot">
<template v-slot:${componentSlot.name}>
<br>
<span class="slot-contents"><${validComponentTagName}></${validComponentTagName}></span>
<br>
</template>
</div>
`;
});
}
});
}
if (component.slots.default) {
slots = `${slots}
<br>
<div class="slot">
<<span class="tag">template</span> <span class="property">v-slot:default</span>>
`;
if (component.slots.default.type === 'component') {
component.slots.default.valid.forEach((validComponent) => {
const validComponentTagName = this.getKebabCaseFromCamelCase(validComponent);
slots = `${slots}<br><span class="slot-contents"><a href="#${validComponentTagName}"><<span class="tag">${validComponentTagName}</span>></<span class="tag">${validComponentTagName}</span>></a></span>`;
});
}
slots = `${slots}
<br>
</<span class="tag">template</span>>
</div>
`;
}
}
if (slots) {
slots = `${slots}`;
}
}
const html = `<<span class="tag">${componentTag}</span>${properties}>${slots}</<span class="tag">${componentTag}</span>>`;
return html;
},
*/
getHighlightedCodeString(html, language) {
return `<pre style="background: none; margin: 0; padding: 0;"><code class="language-${language}">${html}</code></pre>`;
},
loadProperties(vnode) {
if (!vnode) {
return false;
}
const properties = [];
const propertyKeys = vnode.componentInstance.$options.props && Object.keys(vnode.componentInstance.$options.props);
if(propertyKeys) {
propertyKeys.sort();
for (let i = 0; i < propertyKeys.length; i += 1) {
const item = propertyKeys[i];
const name = this.getKebabCaseFromCamelCase(item);
const type = this.getPropertyType(vnode.componentInstance.$options.props[item].type);
let defaultValue = vnode.componentInstance.$options.props[item].default && vnode.componentInstance.$options.props[item].default.toString();
const required = vnode.componentInstance.$options.props[item].meta && vnode.componentInstance.$options.props[item].meta.required;
const deprecated = vnode.componentInstance.$options.props[item].meta && vnode.componentInstance.$options.props[item].meta.deprecated;
let example = vnode.componentInstance.$options.props[item].meta && vnode.componentInstance.$options.props[item].meta.example;
const description = vnode.componentInstance.$options.props[item].meta && vnode.componentInstance.$options.props[item].meta.description;
switch (type) {
case 'array':
example = example || '[4, 8, 15, 16, 23, 42]';
example = this.attemptCodeUnindent(example);
if (defaultValue) {
const cleanedDefaultValue = defaultValue.match(/{\s*return\s*(.*);?\s*}/);
if (cleanedDefaultValue && cleanedDefaultValue[1] !== undefined) {
defaultValue = cleanedDefaultValue[1];
}
// Attempt to de-indent if necessary
defaultValue = this.attemptCodeUnindent(defaultValue);
}
break;
case 'object':
example = example || '{ key1: value1, key2: value2 }';
example = this.attemptCodeUnindent(example);
if (defaultValue) {
let cleanedDefaultValue;
const containsFunction = defaultValue.match(/(?:_default|function)\(\)/);
if(containsFunction) {
cleanedDefaultValue = defaultValue.match(/(?:_default|function)\(\) (?:{\s*return )?([\s\S]*)/);
} else {
cleanedDefaultValue = defaultValue.match(/{\s*return\s*([\s\S]*});?\s*}$/);
}
if (cleanedDefaultValue && cleanedDefaultValue[1] !== undefined) {
defaultValue = cleanedDefaultValue[1].replace(/;\s*}$/, '');
}
// Attempt to de-indent if necessary
defaultValue = this.attemptCodeUnindent(defaultValue);
}
break;
case 'string':
example = example || 'Some text';
break;
case 'function':
example = example || '(value) => { return value.toUpperCase() }';
example = this.attemptCodeUnindent(example);
if (defaultValue) {
const cleanedDefaultValue = defaultValue.match(/{(.*)}/);
if (cleanedDefaultValue && cleanedDefaultValue[1] !== undefined) {
defaultValue = cleanedDefaultValue[1];
}
// Attempt to de-indent if necessary
defaultValue = this.attemptCodeUnindent(defaultValue);
}
break;
case 'number':
example = example || '14';
break;
case 'boolean':
example = example || 'true';
break;
default:
example = example || '';
}
if(example) {
example = example.replace(/"/g, "'");
}
properties.push({
name,
type,
defaultValue,
example,
description,
required,
deprecated,
});
}
if (properties.length > 0) {
properties.sort((a, b) => {
const aIsRequired = !!a.required;
const bIsRequired = !!b.required;
if (aIsRequired === bIsRequired) {
return a.name > b.name;
}
if (aIsRequired !== bIsRequired) {
if (aIsRequired && !bIsRequired) {
return -1;
}
return 1;
}
return 1;
});
}
}
return properties;
},
getEventExampleString(component, event) {
const html = `<${this.getKebabCaseFromCamelCase(component.name)} @${event.name}="someFunction" />`;
return this.getHighlightedCodeString(html, 'html');
},
getKebabCaseFromCamelCase(internalName) {
return internalName && internalName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
},
getPropertyType(typeFunctionString) {
if(typeFunctionString) {
const typeFunction = typeFunctionString.toString().split(' ')[1];
return typeFunction.substr(0, typeFunction.length - 2).toLowerCase();
}
return 'unknown';
},
attemptCodeUnindent(string) {
const codeLines = string.split("\n");
const lastLine = codeLines.slice(-1)[0];
const leadSpacingToRemove = lastLine.match(/^\s*/)[0];
if(leadSpacingToRemove) {
let adjustedCodeLines = [];
codeLines.forEach(line => {
adjustedCodeLines.push(line.replace(new RegExp(`^${leadSpacingToRemove}`), ''));
});
string = adjustedCodeLines.join("\n");
}
return string;
},
},
};
</script>
<style lang="scss" scoped>
@import '../node_modules/prismjs/themes/prism.css';
h2 {
margin-top: 1rem;
}
table td {
white-space: nowrap;
}
.alert {
h2 {
margin-top: .25rem;
margin-bottom: 1rem;
}
}
/*
.indent {
margin-left: 2rem;
}
*/
</style>