-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
146 lines (122 loc) · 4.04 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
137
138
139
140
141
142
143
144
145
146
module.exports = function (config) {
'use strict';
var logger = require('fancy-log');
var _createBlockClass,
_createElemClass,
_createModClass,
_createMixClass,
_createClassList,
_assignClassList;
config = config ||
{
elemPrefix: '__',
modPrefix: '_',
modDlmtr: '_'
};
_createBlockClass = function (block) {
return block;
};
_createElemClass = function (blockClass, elemName) {
return blockClass + config.elemPrefix + elemName;
};
_createModClass = function (baseClass, mods) {
var className = '',
modClass;
mods
.replace(/\s{2,}/g, ' ') // remove more than one whitespace
.replace(/\:\s?/g, config.modDlmtr) // remove whitespace after the semicolon
.split(' ')
.forEach(function (mod) {
modClass = baseClass + config.modPrefix + mod;
className += ' ' + modClass;
});
return className;
};
_createMixClass = function (baseClass, mix) {
var mixClass = '',
block,
elem,
mods,
mixes,
blockRegExp = /block\:([\S]*)\b/,
elemRegExp = /elem\:([\S]*)\b/,
modRegExp = /mods\:\[(.*)\]/;
mixes = mix
.replace(/\s{2,}/g, ' ') // remove more than one whitespace
.replace(/\:\s/g, ':') // remove whitespace after the semicolon
.replace(/\,\s/g, ',') // remove whitespace after the comma
.split(',');
mixes.forEach(function (mix) {
block = blockRegExp.exec(mix) || [];
elem = elemRegExp.exec(mix) || [];
mods = modRegExp.exec(mix) || [];
if (!block) {
logger.log('Please add block attribute to a mix definition: ', mixes);
}
mixClass += ' ' + _createClassList({
block: block[1],
elem: elem[1],
mods: mods[1],
mix: ''
});
});
return mixClass;
};
_createClassList = function (selector) {
var result = '',
block,
elem;
if (selector.block && !selector.elem) {
block = _createBlockClass(selector.block);
result = block;
}
if (selector.block && selector.elem) {
elem = _createElemClass(selector.block, selector.elem);
result = elem;
}
if (selector.mods) {
result += _createModClass(result, selector.mods);
}
if (selector.mix) {
result += _createMixClass(result, selector.mix);
}
return result;
};
_assignClassList = function (attributes, node) {
var classes,
classSet = {
block: '',
elem: '',
mod: '',
mix: ''
};
['block', 'elem', 'mods', 'mix'].forEach(function (attr) {
if (attr in attributes) {
classSet[attr] = attributes[attr];
delete node.attrs[attr];
}
});
classes = _createClassList(classSet);
if (classes) {
node.attrs.class = node.attrs.class ?
[classes, node.attrs.class].join(' ') :
classes;
return classSet;
}
};
return function posthtmlBem(tree) {
tree.match({attrs: {block: true}}, function (node) {
var nodeAttrs;
nodeAttrs = _assignClassList(node.attrs, node);
if (node.content && Array.isArray(node.content)) {
node.content.forEach(function (children) {
if (children.attrs && children.attrs.elem && !children.attrs.block) {
children.attrs.block = nodeAttrs.block;
}
});
}
return node;
});
return tree;
};
};