-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplate.js
52 lines (47 loc) · 1.28 KB
/
template.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
'use strict';
const TEMPLATE = `
@function iconfont-item($name) {
$slash: str-index($name, '/');
$group: null;
@if ($slash) {
$group: str-slice($name, 0, $slash - 1);
$name: str-slice($name, $slash + 1);
} @else {
$group: nth(map-keys($__iconfont__data), 1);
}
$group: iconfont-group($group);
@if (false == map-has-key($group, $name)) {
@warn 'Undefined Iconfont Glyph!';
@return '';
}
@return map-get($group, $name);
}
@function iconfont-group($group: null) {
@if (null == $group) {
$group: nth(map-keys($__iconfont__data), 1);
}
@if (false == map-has-key($__iconfont__data, $group)) {
@warn 'Undefined Iconfont Family!';
@return ();
}
@return map-get($__iconfont__data, $group);
}`;
function toSCSS(glyphs) {
return JSON.stringify(glyphs, null, '\t')
.replace(/\{/g, '(')
.replace(/\}/g, ')')
.replace(/\\\\/g, '\\');
}
module.exports = function(args) {
const family = args.family;
const glyphs = args.unicodes.reduce(function(glyphs, glyph) {
glyphs[glyph.name] = '\\' + glyph.unicode.charCodeAt(0).toString(16).toLowerCase();
return glyphs;
}, {});
const data = {};
data[family] = glyphs;
return [
`$__iconfont__data: map-merge(if(global_variable_exists('__iconfont__data'), $__iconfont__data, ()), ${toSCSS(data)});`,
TEMPLATE
].join('\n\n');
};