-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 2.13 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
// Load in the main postcss module
var postcss = require('postcss');
// Wrapper
module.exports = postcss.plugin('postcss-table-of-contents', function () {
return function (css) {
var i = []; // Variable for match counting
var ii = []; // Variable for match counting
var iii = []; // Variable for match counting
// This must be revised:
// Placeholder for table of contents: (#)
// var tocPlaceholder = /\(#\)/;
// Function for replace placeholders below
function replacer(match, p1, p3, p5) {
if (match === '#)') {
// Replace for first level
i[p1] = ( i[p1] || 0 ) + 1;
return i[p1] + ')';
} else if (match === '##)') {
// Replace for second level
i[p1] = i[p1] || 0;
ii[p3] = ( ii[p3] || 0 ) + 1;
return i[p1] + '.' + ii[p3] + ')';
} else {
// Replace for third level
i[p1] = i[p1] || 0;
ii[p3] = ii[p3] || 0;
iii[p5] = ( iii[p5] || 0 ) + 1;
return i[p1] + '.' + ii[p3] + '.' + iii[p5] + ')';
}
}
// Traverses the container’s descendant nodes
css.walkComments(function (comment) {
var String = comment.toString(); // Get comments as string
// Check, if table of contents placeholder is in string
// This must be revised:
// var isInString = tocPlaceholder.test(String);
// if (isInString === false) {
// Reset old numberings with numbering placeholder #)
// This must be revised:
// var StringMatchTmp = String.replace(/(\S)\)/, '#');
// Replace numbering placeholders with increment number
var StringMatch = String.replace(/(#)(\)?)(#?)(\)?)(#?\)?)/,
replacer);
// Replace all comments with our new string
comment.replaceWith(StringMatch);
// } else {
// Get array of #) comments
// Cum back later ;-)
// }
});
};
});