Format with prettier

This commit is contained in:
Dan Mindru 2021-09-01 21:36:29 +02:00
parent 27d92ec469
commit 2387d542e7
1 changed files with 106 additions and 107 deletions

View File

@ -14,132 +14,131 @@ const INLINE_ATTR = 'inline';
const NOT_INLINE_ATTR = `!${INLINE_ATTR}`; const NOT_INLINE_ATTR = `!${INLINE_ATTR}`;
function inlineimg(options = {}) { function inlineimg(options = {}) {
var selector = options.selector || 'img[src]'; var selector = options.selector || 'img[src]';
var attribute = options.attribute || 'src'; var attribute = options.attribute || 'src';
var getHTTP = options.getHTTP || false; var getHTTP = options.getHTTP || false;
return through.obj(function (file, encoding, callback) { return through.obj(function(file, encoding, callback) {
if (file.isStream()) { if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return callback(); return callback();
}
if (file.isBuffer()) {
var contents = file.contents.toString(encoding);
// Load it into cheerio's virtual DOM for easy manipulation
var $ = cheerio.load(contents);
var inline_flag = $(`img[${INLINE_ATTR}]`);
// If images with an inline attr are found that is the selection we want
var img_tags = inline_flag.length ? inline_flag : $(selector);
var count = 0;
img_tags.each(function() {
var $img = $(this);
var src = $img.attr(attribute);
// Save the file format from the extension
var ext_format = path.extname(src).substr(1);
// If inline_flag tags were found we want to remove the inline tag
if (inline_flag.length) {
$img.removeAttr(INLINE_ATTR);
} }
if (file.isBuffer()) { // Find !inline attribute
var contents = file.contents.toString(encoding); var not_inline_flag = $img.attr(NOT_INLINE_ATTR);
// Load it into cheerio's virtual DOM for easy manipulation
var $ = cheerio.load(contents);
var inline_flag = $(`img[${INLINE_ATTR}]`);
// If images with an inline attr are found that is the selection we want
var img_tags = inline_flag.length ? inline_flag : $(selector);
var count = 0;
img_tags.each(function () { if (typeof not_inline_flag !== typeof undefined && not_inline_flag !== false) {
var $img = $(this); // Remove the tag and don't process this file
var src = $img.attr(attribute); return $img.removeAttr(NOT_INLINE_ATTR);
// Save the file format from the extension }
var ext_format = path.extname(src).substr(1);
// If inline_flag tags were found we want to remove the inline tag // Count async ops
if (inline_flag.length) { count++;
$img.removeAttr(INLINE_ATTR);
}
// Find !inline attribute getSrcBase64(options.basedir || file.base, getHTTP, src, function(err, result, res_format, skip_formatting) {
var not_inline_flag = $img.attr(NOT_INLINE_ATTR); if (err) {
console.error(err);
if (typeof not_inline_flag !== typeof undefined && not_inline_flag !== false) { } else {
// Remove the tag and don't process this file // Need a format in and a result for this to work
return $img.removeAttr(NOT_INLINE_ATTR); if (!skip_formatting) {
} if (result && (ext_format || res_format)) {
$img.attr('src', `data:image/${ext_format};base64,${result}`);
// Count async ops } else {
count++; $img.attr('src', ``);
$img.attr('alt', `Image not found, Please check Url`);
getSrcBase64(options.basedir || file.base, getHTTP, src, function (err, result, res_format, skip_formatting) { console.error(`Failed to identify format of ${src}!`);
if (err) { }
console.error(err);
} else {
// Need a format in and a result for this to work
if (!skip_formatting) {
if (result && (ext_format || res_format)) {
$img.attr('src', `data:image/${ext_format};base64,${result}`);
} else {
$img.attr('src', ``);
$img.attr('alt', `Image not found, Please check Url`);
console.error(`Failed to identify format of ${src}!`);
}
}
if (!--count) {
file.contents = Buffer.from($.html());
callback(null, file);
}
}
});
});
// If no files are processing we don't need to wait as none were ever started
if (!img_tags.length) {
file.contents = Buffer.from($.html());
callback(null, file);
} }
}
}); if (!--count) {
file.contents = Buffer.from($.html());
callback(null, file);
}
}
});
});
// If no files are processing we don't need to wait as none were ever started
if (!img_tags.length) {
file.contents = Buffer.from($.html());
callback(null, file);
}
}
});
} }
function getHTTPBase64(url, callback) { function getHTTPBase64(url, callback) {
// Get applicable library // Get applicable library
var lib = url.startsWith('https') ? https : http; var lib = url.startsWith('https') ? https : http;
// Initiate a git request to our URL // Initiate a git request to our URL
var req = lib.get(url, (res) => { var req = lib.get(url, res => {
// Check for redirect // Check for redirect
if (res.statusCode >= 301 && res.statusCode < 400 && res.headers.location) { if (res.statusCode >= 301 && res.statusCode < 400 && res.headers.location) {
// Redirect // Redirect
return getHTTPBase64(res.headers.location, callback); return getHTTPBase64(res.headers.location, callback);
} }
// Check for HTTP errors // Check for HTTP errors
if (res.statusCode < 200 || res.statusCode >= 400) { if (res.statusCode < 200 || res.statusCode >= 400) {
return callback(new Error('Failed to load page, status code: ' + res.statusCode)); return callback(new Error('Failed to load page, status code: ' + res.statusCode));
} }
// Get file format // Get file format
var format; var format;
if (res.headers['content-type']) { if (res.headers['content-type']) {
var matches = res.headers['content-type'].match(MIME_TYPE_REGEX); var matches = res.headers['content-type'].match(MIME_TYPE_REGEX);
if (matches) format = matches[1]; if (matches) format = matches[1];
} }
// Create an empty buffer to store the body in // Create an empty buffer to store the body in
var body = Buffer.from([]); var body = Buffer.from([]);
// Append each chunk to the body // Append each chunk to the body
res.on('data', (chunk) => body = Buffer.concat([body, chunk])); res.on('data', chunk => (body = Buffer.concat([body, chunk])));
// Done callback // Done callback
res.on('end', () => callback(null, body.toString('base64'), format)); res.on('end', () => callback(null, body.toString('base64'), format));
}); });
// Listen for network errors // Listen for network errors
req.on('error', (err) => callback(err)); req.on('error', err => callback(err));
} }
function getSrcBase64(base, getHTTP, src, callback) { function getSrcBase64(base, getHTTP, src, callback) {
if (!url.parse(src).hostname) { if (!url.parse(src).hostname) {
// Get local file // Get local file
var file_path = path.join(base, src); var file_path = path.join(base, src);
if (fs.existsSync(file_path)) { if (fs.existsSync(file_path)) {
fs.readFile(file_path, 'base64', callback); fs.readFile(file_path, 'base64', callback);
}
else {
callback(null);
}
} else { } else {
// Get remote file callback(null);
if (getHTTP) {
getHTTPBase64(src, callback);
} else {
callback(null, src, null, true)
}
} }
} else {
// Get remote file
if (getHTTP) {
getHTTPBase64(src, callback);
} else {
callback(null, src, null, true);
}
}
} }
module.exports.inlineimg = inlineimg; module.exports.inlineimg = inlineimg;