Skip to content

Instantly share code, notes, and snippets.

@Kukunin
Last active May 28, 2022 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kukunin/4a13acf948019d05ae9a01777729230a to your computer and use it in GitHub Desktop.
Save Kukunin/4a13acf948019d05ae9a01777729230a to your computer and use it in GitHub Desktop.
A template for a typical transformation for gulp
const through = require('through2'),
PluginError = require('gulp-util').PluginError;
module.exports = function myTransformation(options) {
if(!options) {
options = {};
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
// return as is
cb(null, file);
} else if (file.isBuffer()) {
try {
const content = file.contents.toString('utf8');
// do any transformation
file.contents = new Buffer(content, 'utf8');
cb(null, file);
}
catch (err) {
throw new PluginError('my-transformation', err);
}
} else if (file.isStream()) {
throw new PluginError('my-transformation', 'Streams are not supported!');
}
});
};
@Kukunin
Copy link
Author

Kukunin commented Jul 9, 2020 via email

@Marshumov
Copy link

Вся магия происходит в 16-18 строках. В оригинале имеем Buffer - просто масив байт. Мы воспринимаем его как utf8, делаем трансформацию и назад. Если исходный набор байт с русским текстом не в utf8 (а в cp1251, например), тогда будут проблемы с кодировкой. В зависимости от самой трансформации и кодировки результата, вам может понадобиться кодировать cp1251 в utf8. как это сделать, можно посмотреть тут https://stackoverflow.com/questions/8693400/converting-from-windows-1251-to-utf-8-in-node-js чт, 9 июл. 2020 г. в 22:31, Nikita notifications@github.com:

@Marshumov commented on this gist. ------------------------------ Здравствуйте. Используя ваш код я сталкиваюсь с проблемой кодировок, русские символы отображаются в виде кода на us-ascii , никак не могу решить проблему. Не подскажите решение? — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://gist.github.com/4a13acf948019d05ae9a01777729230a#gistcomment-3370104, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB2DKJHT5GDAZJOY7V7KADR2YLJRANCNFSM4OV5EJ6Q .

Спасибо вам большое!
У меня ошибка с кодировкой возникла из-за cheerio, который использовался дальше во время работы скрипта. Долго не мог пофиксить баг и найти причину )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment