forked from KatChaotic/sveltedoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (108 loc) · 4.48 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
const fs = require('fs');
const path = require('path');
const Parser = require('./lib/parser');
const DEFAULT_ENCODING = 'utf8';
const DEFAULT_IGNORED_VISIBILITIES = ['protected', 'private'];
function parseOptions(options) {
if (!options || (!options.filename && !options.fileContent)) {
throw new Error('One of options.filename or options.filecontent is required');
}
options.encoding = options.encoding || DEFAULT_ENCODING;
options.ignoredVisibilities = options.ignoredVisibilities || DEFAULT_IGNORED_VISIBILITIES;
}
module.exports.parse = (options) => new Promise((resolve, reject) => {
try {
parseOptions(options);
if (!options.source) {
if (options.filename) {
if (path.extname(options.filename) === '.js') {
options.source = {
template: '',
script: fs.readFileSync(options.filename, options.encoding)
};
} else {
options.source = loadSourceFromFileContent(
fs.readFileSync(options.filename, options.encoding));
}
} else {
options.source = loadSourceFromFileContent(options.fileContent);
}
}
const component = {};
const parser = new Parser(options);
parser.features.forEach((feature) => {
switch (feature) {
case 'name':
case 'description':
component[feature] = null;
parser.on(feature, (value) => (component[feature] = value));
break;
case 'keywords':
component[feature] = [];
parser.on(feature, (value) => (component[feature] = value));
break;
default:
component[feature] = [];
const eventName = Parser.getEventName(feature);
parser.on(eventName, (value) => {
const itemIndex = component[feature].findIndex(item => item.name === value.name);
if (itemIndex < 0) {
component[feature].push(value);
} else {
component[feature][itemIndex] = value;
}
});
}
});
parser.on('end', () => {
parser.features.forEach((feature) => {
if (component[feature] instanceof Array) {
component[feature] = component[feature].filter((item) => {
return !options.ignoredVisibilities.includes(item.visibility);
});
}
});
resolve(component);
});
parser.on('failure', (error) => {
reject(error);
});
parser.walk();
} catch (error) {
reject(error);
}
});
function extractContentFromHtmlBlock(content, blockName) {
let leftContent = content;
let innerBlockContent = '';
let attributes = '';
let offset = 0;
const blockStart = leftContent.indexOf(`<${blockName}`);
if (blockStart >= 0) {
const blockEnd = leftContent.indexOf(`</${blockName}>`, blockStart + blockName.length + 1);
if (blockEnd >= 0) {
const openTagEndIndex = leftContent.indexOf('>', blockStart + blockName.length);
attributes = leftContent.substr(blockStart + blockName.length + 1, openTagEndIndex - blockStart - blockName.length - 1);
innerBlockContent = leftContent.substr(openTagEndIndex + 1, blockEnd - openTagEndIndex - 1);
offset = openTagEndIndex + 1;
leftContent = leftContent.substr(0, blockStart) + leftContent.substr(blockEnd + blockName.length + 3);
}
}
return {
leftContent: leftContent,
innerContent: innerBlockContent,
attributes: attributes,
offset: offset
};
}
function loadSourceFromFileContent(fileContent) {
const script = extractContentFromHtmlBlock(fileContent, 'script');
const style = extractContentFromHtmlBlock(script.leftContent, 'style');
return {
template: style.leftContent,
script: script.innerContent,
scriptOffset: script.offset,
style: style.innerContent,
styleOffset: style.offset
};
}