写作与节点cheerio修改后的文件
我运行一个节点脚本读取.SVG字体文件,并将其作为一个字符串变量Cheerio,修改它,并试图写入磁盘
问题是,尽管脚本似乎工作,输出文件是相同的输入文件,如无修改发生。
在我看来,就好像当初的“svgFont”变量,我传递给cheerio不被修改的。
所以,我需要无论是从cheerio直接通过修改回它,或输出到fs写。但我不能看到如何。
const cheerio = require('cheerio');
var fs = require('fs');
// read the svg font
fs.readFile('font.svg', function read(err, data) {
if (err) {
throw err;
}
// convert to string and pass to cheerio
var svgFont = data.toString();
const $ = cheerio.load(svgFont, {
normalizeWhitespace: true,
xmlMode: true
});
// loop all glyph tags
$("glyph").each(function(i,el){
// modify things
});
// Finally write the font with the modified paths
fs.writeFile("outFont.svg", svgFont, function(err) {
if(err) {
throw err;
}
console.log("The file was saved!");
});
});
回答如下:
您可以使用fs-cheerio包写入文件。在你的代码的原始变量没有被修改。这是一个被修改的解析cheerio表示。