mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add method to fetch remote image with AssetsInliner
This commit is contained in:
@@ -3,7 +3,12 @@ var util = require('util');
|
||||
var path = require('path');
|
||||
|
||||
var FolderOutput = require('./folder');
|
||||
var Promise = require('../utils/promise');
|
||||
var fs = require('../utils/fs');
|
||||
var imagesUtil = require('../utils/images');
|
||||
var location = require('../utils/location');
|
||||
|
||||
var DEFAULT_ASSETS_FOLDER = 'assets';
|
||||
|
||||
/*
|
||||
Utility mixin to inline all the assets in a book:
|
||||
@@ -32,19 +37,45 @@ AssetsInliner.prototype.onOutputSVG = function(page, svg) {
|
||||
};
|
||||
|
||||
// Output an image as a file
|
||||
AssetsInliner.prototype.onOutputImage = function(page, imgFile) {
|
||||
if (path.extname(imgFile).toLowerCase() != '.svg') {
|
||||
return imgFile;
|
||||
AssetsInliner.prototype.onOutputImage = function(page, src) {
|
||||
var that = this;
|
||||
var isSVG = false;
|
||||
var ext = path.extname(src).toLowerCase();
|
||||
if (ext == '.svg') {
|
||||
isSVG = false;
|
||||
ext = '.png';
|
||||
}
|
||||
|
||||
// Convert SVG to PNG
|
||||
var filename = _.uniqueId('svg_') + '.png';
|
||||
return imagesUtil.convertSVGToPNG(page.resolve(imgFile), this.resolve(filename))
|
||||
return Promise()
|
||||
|
||||
// Return relative path from the page
|
||||
.thenResolve(function() {
|
||||
return page.relative('/' + filename);
|
||||
});
|
||||
// Allocate a new file
|
||||
.then(function() {
|
||||
|
||||
return that.
|
||||
})
|
||||
|
||||
// Download file if external
|
||||
.then(function() {
|
||||
if (!location.isExternal(src)) return;
|
||||
|
||||
return fs.download(src, )
|
||||
|
||||
})
|
||||
.then(function() {
|
||||
if (path.extname(src).toLowerCase() != '.svg') {
|
||||
return src;
|
||||
}
|
||||
|
||||
// Convert SVG to PNG
|
||||
var filename = _.uniqueId('svg_') + '.png';
|
||||
return imagesUtil.convertSVGToPNG(page.resolve(src), this.resolve(filename))
|
||||
.thenResolve('/' + filename);
|
||||
})
|
||||
|
||||
// Return relative path from the page
|
||||
.thenResolve(function(filename) {
|
||||
return page.relative('/' + filename);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -106,9 +106,9 @@ Output.prototype.onOutputSVG = function(page, svg) {
|
||||
};
|
||||
|
||||
// Output an image as a file
|
||||
// Normalize the relative link
|
||||
Output.prototype.onOutputImage = function(page, imgFile) {
|
||||
// Don't replace it
|
||||
return imgFile;
|
||||
return page.relative(imgFile);
|
||||
};
|
||||
|
||||
// Finish the generation
|
||||
|
||||
@@ -74,5 +74,11 @@ FolderOutput.prototype.writeFile = function(filename, buf) {
|
||||
});
|
||||
};
|
||||
|
||||
// Create a new unique file
|
||||
// Returns its filename
|
||||
FolderOutput.prototype.createNewFile = function(base, filename) {
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports = FolderOutput;
|
||||
|
||||
+27
-1
@@ -2,6 +2,8 @@ var fs = require('graceful-fs');
|
||||
var mkdirp = require('mkdirp');
|
||||
var destroy = require('destroy');
|
||||
var tmp = require('tmp');
|
||||
var request = require('request');
|
||||
var path = require('path');
|
||||
|
||||
var Promise = require('./promise');
|
||||
|
||||
@@ -59,6 +61,28 @@ function genTmpFile(opts) {
|
||||
.get(0);
|
||||
}
|
||||
|
||||
// Download an image
|
||||
function download(uri, dest) {
|
||||
return writeStream(dest, request(uri));
|
||||
}
|
||||
|
||||
// Find a filename available in a folder
|
||||
function uniqueFilename(base, filename) {
|
||||
var ext = path.extname(filename);
|
||||
filename = path.resolve(base, filename);
|
||||
filename = path.join(path.dirname(filename), path.basename(filename, ext));
|
||||
|
||||
var _filename = filename+ext;
|
||||
|
||||
var i = 0;
|
||||
while (fs.existsSync(filename)) {
|
||||
_filename = filename + '_' + i + ext;
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
return path.relative(base, _filename);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
exists: fileExists,
|
||||
existsSync: fs.existsSync,
|
||||
@@ -70,5 +94,7 @@ module.exports = {
|
||||
readdir: Promise.nfbind(fs.readdir),
|
||||
writeStream: writeStream,
|
||||
copy: copyFile,
|
||||
tmpFile: genTmpFile
|
||||
tmpFile: genTmpFile,
|
||||
download: download,
|
||||
uniqueFilename: uniqueFilename
|
||||
};
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@
|
||||
"crc": "3.2.1",
|
||||
"bash-color": "0.0.3",
|
||||
"urijs": "1.17.0",
|
||||
"request": "2.51.0",
|
||||
"request": "2.69.0",
|
||||
"npm": "2.4.1",
|
||||
"dom-serializer": "0.1.0",
|
||||
"spawn-cmd": "0.0.2",
|
||||
|
||||
@@ -47,5 +47,9 @@ describe('Assets Inliner Output', function() {
|
||||
output.should.have.file(src);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Remote Assets', function() {
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user