oadp-material
Version:
oadp-material
93 lines (92 loc) • 2.37 kB
JavaScript
;
exports.__esModule = true;
exports.parseData = exports["default"] = exports.STATE_MARK = exports.NodeType = exports.ContentType = void 0;
var STATE_MARK = exports.STATE_MARK = {
'-': 'disabled',
// tslint:disable-next-line:object-literal-sort-keys
'*': 'active',
'~': 'hover',
'': 'normal'
};
var NodeType = exports.NodeType = /*#__PURE__*/function (NodeType) {
NodeType["divider"] = "divider";
NodeType["node"] = "node";
NodeType["comment"] = "comment";
return NodeType;
}({});
var ContentType = exports.ContentType = /*#__PURE__*/function (ContentType) {
ContentType["text"] = "text";
ContentType["icon"] = "icon";
return ContentType;
}({});
var getChildren = function getChildren(template) {
if (template === void 0) {
template = '';
}
if (!template) {
return [];
}
return template.replace(/(\[.*?\])/g, '\n$1\n').split('\n').filter(function (v) {
return !!v;
}).map(function (d) {
switch (true) {
case /^\[(.*)\]$/.test(d):
return {
type: 'icon',
value: RegExp.$1
};
default:
return {
type: 'text',
value: d
};
}
});
};
var parseData = exports.parseData = function parseData(text, options) {
if (options === void 0) {
options = {
parseContent: false
};
}
if (!text) {
return [];
}
var root = {
type: NodeType.node,
state: 'normal',
value: '',
children: []
};
var stack = [root];
text.split('\n').filter(function (line) {
return line.trim();
}).forEach(function (line) {
var re = /^(\t*)([#\-~*]?)(.*)$/.exec(line);
var indent = (re[1] || '').length;
var prefix = re[2] || '';
var item = {
type: NodeType.node,
state: 'normal',
value: re[3] || '',
children: []
};
if (prefix === '-' && /^-{2,}$/.test(item.value)) {
item.type = NodeType.divider;
} else if (prefix === '#') {
item.type = NodeType.comment;
} else {
item.state = STATE_MARK[prefix];
}
if (item.type === 'node' && options.parseContent) {
item.value = getChildren(item.value);
}
while (indent <= stack.length - 2) {
stack.pop();
}
stack[stack.length - 1].children.push(item);
stack.push(item);
});
return root.children;
};
var _default = exports["default"] = parseData;