-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathiconize.js
55 lines (48 loc) · 1.36 KB
/
iconize.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
'use strict';
/*
* iconize
* <svg-icon type="ant-design" name="aliwangwang" class="self-define"></svg-icon>
*/
const iconsMetaByName = require('./const/icons-meta-by-name');
const generateSvgIcon = require('./util/generate-svg-icon');
const generateSvgSprite = require('./util/generate-svg-sprite');
const regexps = {
icon: /<svg-icon\s+([-=\w\d'"\s]+)\s*\/?>(<\/svg-icon>)?/gi,
attrs: /(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/gi
};
function buildParamsFromString(string) {
let match;
let attr;
let value;
const params = {};
while (match = regexps.attrs.exec(string)) {
attr = match[1];
value = match[2].replace(/'|"/, '');
params[attr] = value;
}
params.type = params.type || 'font-awesome';
params.prefix = params.prefix || iconsMetaByName[params.type].prefix;
return params;
}
module.exports = (str, options, callback) => {
const icons = [];
let html = str.toString();
let match;
let tag;
let params;
options = options || {};
while (match = regexps.icon.exec(html)) {
tag = match[0];
params = buildParamsFromString(match[1]);
icons.push(params);
html = html.replace(tag, generateSvgIcon(params));
}
if (options.sprite) {
generateSvgSprite(icons, (sprite) => {
html = html.replace(/<body.*?>/, (match) => match + sprite);
callback(html);
})
} else {
callback(html);
}
};