-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
136 lines (119 loc) · 2.91 KB
/
index.js
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
const path = require('path');
const fs = require('fs');
const compiler = require('vue-template-compiler');
const stripIndent = require('strip-indent');
const indentString = require('indent-string');
const parse = require('./parse');
const log = require('./util').log;
const config = require('./util').config;
// get markdown comment
function getMarkDown(obj) {
let md = '';
// name
let name = obj.name;
if (name) {
md += stripIndent(`
## Name
> ${name};
`);
}
// props
md += stripIndent(`
## Props
| Name | Type | Required | Default | validator |
| ----- | ---- | ------- | ------- | ------- |
`);
let props = obj.props || [];
props.forEach(prop => {
md += stripIndent(`| ${prop.name} | ${prop.type} | ${prop.required} | ${prop.default} | ${prop.validator} |` + '\n');
});
if (!props.length) {
md + '| | | | | |';
}
// events
md += stripIndent(`
## Events
| Name | Data | Code |
| ----- | ----- | ----- |
`);
let events = obj.events || [];
events.forEach(item => {
md += stripIndent(`| ${item.name} | ${item.data} | ${JSON.stringify(item.code)} |` + '\n');
});
if (!events.length) {
md += stripIndent(`| | |`);
}
// methods
md += stripIndent(`
## Methods
| Name | Code |
| ----- | ----- |
`);
let methods = obj.methods || [];
methods.forEach(item => {
md += stripIndent(`| ${item.name} | ${item.code} |` + '\n');
});
if (!methods.length) {
md += '| | |';
}
// Components
md += stripIndent(`
## Components
`);
let components = obj.components || [];
components.forEach(key => {
md += stripIndent(`
- ${key}
`);
});
// options
md += stripIndent(`
## Options
`);
let options = obj.options || [];
options.forEach(key => {
md += stripIndent(`
- ${key}
`);
});
return md;
}
// cache parsed md
const markdownCodes = {};
// handlers
exports.handlers = {
beforeParse (e) {
if (/\.vue$/.test(e.filename)) {
log(`parse file begin: ${e.filename}`);
const parsedComponent = compiler.parseComponent(e.source);
const code = parsedComponent.script ? parsedComponent.script.content : '';
const parsed = parse(code);
const md = getMarkDown(parsed);
markdownCodes[e.filename] = md;
e.source = code;
}
},
jsdocCommentFound(e) {
const tag = '@' + config.tag;
if (
/\.vue$/.test(e.filename)
&& e.comment.indexOf(tag) != -1
) {
let md = markdownCodes[e.filename];
e.comment = e.comment.replace(tag, md);
}
}
}
// defineTags
exports.defineTags = function (dictionary) {
const tag = config.tag;
dictionary.defineTag(tag, {
mustHaveValue: false,
onTagged (doclet, tag) {
const componentName = doclet.meta.filename.split('.').slice(0, -1).join('.');
doclet.scope = 'vue';
doclet.kind = 'module';
doclet.alias = 'vue-' + componentName;
}
});
}