-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
90 lines (67 loc) · 2.57 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
var bemNaming = require('bem-naming'),
stringifyObj = require('stringify-object'),
parser = require('emmet/lib/parser/abbreviation');
function expandBemjson(abbreviation, parentBlock, opts) {
if (typeof parentBlock === 'object') {
opts = parentBlock;
parentBlock = null;
}
opts || (opts = {});
var naming = bemNaming(opts.naming);
function isShortcut(item) {
return (item[0] === naming.elemDelim[0]) || (item[0] === naming.modDelim[0]);
}
function getParent(tree) {
var parent = tree.parent,
name = parent.name();
if (!parent || !name) return;
return isShortcut(name) ? getParent(parent) : bemNaming.parse(name).block;
}
function walk(tree) {
if (!tree.abbreviation) {
var bemjson = tree.children.map(walk);
if (!bemjson.length) return;
return bemjson.length === 1 ? bemjson[0] : bemjson;
}
var item = tree.name(),
parent = getParent(tree);
if (isShortcut(item)) {
item = (parent || parentBlock || 'parentBlockStubPlaceholder') + item;
}
var entity = naming.parse(item);
if (!entity) return item;
var content = (tree.content ? [tree.content] : [])
.concat(tree.children.map(walk));
if (entity.modName) {
entity.block === 'parentBlockStubPlaceholder' && (entity.block === 'parent');
var modFieldName = entity.elem ? 'elemMods' : 'mods';
entity[modFieldName] = {};
entity[modFieldName][entity.modName] = entity.modVal;
delete entity.modName;
delete entity.modVal;
}
// remove block field if it matches its parent block name
if (naming.isElem(entity) && entity.block === parent || entity.block === 'parentBlockStubPlaceholder') {
delete entity.block;
}
if (!content.length) {
entity.content = {};
} else {
entity.content = content.length === 1 ? content[0] : content;
}
return entity;
}
var tree = parser.parse(abbreviation);
return walk(tree);
};
module.exports = expandBemjson;
module.exports.stringify = function(abbreviation, parentBlock, opts) {
if (typeof parentBlock === 'object') {
opts = parentBlock;
parentBlock = null;
}
opts || (opts = {});
opts.indent || (opts.indent = ' ');
var bemjson = expandBemjson(abbreviation, parentBlock, opts);
return typeof bemjson === 'string' ? bemjson : stringifyObj(bemjson, opts);
};