-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (72 loc) · 2.07 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
var postcss = require('postcss');
var yaml = require('js-yaml');
var extend = require('extend');
var defaultOptions = {
detect: function(firstLine) {
return firstLine.match(/^\* *$/);
}
};
/**
* @param {String} css
* @param {Object} [options]
* @param {Function} [options.detect]
* @return {Array}
*/
function parse(css, options) {
options = extend({}, defaultOptions, options || {});
css = css.replace(/\r/g, '');
var lines = css.split(/\n/g);
var pendingCss = postcss().process(css);
// Accessing .css forces postcss to work synchronously. So this line is important
// even though we don't actually use the result. See https://stackoverflow.com/a/36464396
var processedCss = pendingCss.css;
return pendingCss.root.nodes.filter(function (rule) {
if (rule.type === 'comment') {
return options.detect(rule.text.split(/\n/)[0] || '');
}
return false;
}).map(function (rule, index, rules) {
var comment = normalizeComment(rule.text);
var nextRule = rules[index + 1];
var css = nextRule
? lines.slice(rule.source.end.line, nextRule.source.start.line - 1).join('\n')
: lines.slice(rule.source.end.line).join('\n');
return {
annotation: yaml.safeLoad(comment),
css: css,
comment: comment,
rawComment: rule.comment,
position: rule.source
};
});
};
/**
* @param {String} path
* @param {Object} [options]
* @param {Function} callback
*/
function parseFile(path, options, callback) {
if (arguments.length < 3) {
callback = options;
options = {};
if (typeof callback !== 'function') {
throw 'callback parameter must be a function.';
}
}
require('fs').readFile(path, {encoding: 'utf8'}, function(error, css) {
if (error) {
return callback(error);
}
callback(undefined, parse(css.toString('utf8')));
});
}
function normalizeComment(comment) {
return comment
.split(/\n/g)
.slice(1)
.join('\n')
.replace(/\n *\*/mg, '\n')
.replace(/^ *\*/, '')
.replace(/^\n+|\n+ *$/g,'');
}
module.exports = {parse: parse, parseFile: parseFile};