const fs = require("fs");
const cheerio = require("cheerio")
const fire_url = './symbol.svg'
/* 代码格式如下.
```html
exp
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol viewBox="0 0 32 32" id="edit">
<title>edit</title>
path...
</symbol>
<symbol viewBox="0 0 32 32" id="edit">
<title>edit</title>
path...
</symbol>
</svg>
```
// 要生成如下.
<svg>
<title>id</title>
<path></path>
</svg>
*/
fs.readFile(fire_url, 'utf8', function (err, res) {
if (err) {
return false
}
// console.log(res)
readSvg(res)
})
function readSvg (data) {
if (data === '') {
return false
}
var $ = cheerio.load(data);
let sum = 0
$("symbol").each(function (i, e) {
var item = $(e)
var str = '<svg '
str += 'id = "' + item.attr("id") + '" viewBox = "' + item.attr("viewBox") + '">'
str += item.html()
str += '</svg>'
writeSvg(str, item.attr("id"))
sum ++
})
console.log(`all item number is ${sum}`)
}
function writeSvg (data, name) {
if (data === '') return
var file_name = name + '.svg'
fs.writeFile(file_name, data, function (err) {
if (err) {
return
}
console.log( file_name + 'is saved')
})
}
进入阿里icon,点击右上角上传即可.