-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
87 lines (72 loc) · 2.27 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
const path = require('path');
const fs = require('fs');
const vuedoc = require('@vuedoc/md');
const compiler = require('vue-template-compiler');
const spawnSync = require('child_process').spawnSync;
const config = require('./util').config;
const log = require('./util').log;
// cache parsed md
const markdownCodes = {};
// handlers
exports.handlers = {
beforeParse (e) {
if (/\.vue$/.test(e.filename)) {
log(`parse file begin: ${e.filename}`);
// extract script
const parsedComponent = compiler.parseComponent(e.source);
const code = parsedComponent.script ? parsedComponent.script.content : '';
// extract SFC info
const options = Object.assign({}, config, {
filecontent: e.source
});
try {
// node parsed-md.js
const md = spawnSync('node', [path.resolve(__dirname, './parse-md.js')], {
cwd: process.cwd(),
env: process.env,
input: JSON.stringify(options)
});
// parse md error
if (md.error && md.error.toString().trim())
log('parse md error: ' + md.error.toString(), true);
if (md.error && md.stderr.toString().trim())
log('parse md error: ' + md.stderr.toString(), true);
// extract data
const result = md.stdout.toString();
const matches = result.match(/JSDOC_VUEDOC_BEGIN([\s\S]*?)JSDOC_VUEDOC_END/);
// cache md content
markdownCodes[e.filename] = '';
if (matches) {
markdownCodes[e.filename] = matches[1].trim();
}
} catch(e) {
log(`parse SFC info error: ${e.filename}`);
log(e);
}
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 = 'description';
doclet.alias = 'vue-' + componentName;
}
});
}