mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 23:55:20 +00:00
Merge pull request #1259 from GitbookIO/png-data-uri
Adding data-uri support for PNG Images in Books
This commit is contained in:
@@ -75,7 +75,7 @@ Config.prototype.getPluginDependencies = function() {
|
||||
@return {Config}
|
||||
*/
|
||||
Config.prototype.setPluginDependencies = function(deps) {
|
||||
var plugins = PluginDependency.listToArray(deps);
|
||||
var plugins = PluginDependency.listFromArray(deps);
|
||||
|
||||
return this.setValue('plugins', plugins);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
var cheerio = require('cheerio');
|
||||
var tmp = require('tmp');
|
||||
var inlinePng = require('../inlinePng');
|
||||
|
||||
describe('inlinePng', function() {
|
||||
var dir;
|
||||
|
||||
beforeEach(function() {
|
||||
dir = tmp.dirSync();
|
||||
});
|
||||
|
||||
pit('should write an inline PNG using data URI as a file', function() {
|
||||
var $ = cheerio.load('<img alt="GitBook Logo 20x20" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUEAYAAADdGcFOAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAAsTAAALEwEAmpwYAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3JpZW50YXRpb24+CiAgICAgIDw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');
|
||||
|
||||
return inlinePng(dir.name, 'index.html', $)
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(src);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
var svgToImg = require('./svgToImg');
|
||||
var svgToPng = require('./svgToPng');
|
||||
var inlinePng = require('./inlinePng');
|
||||
var resolveImages = require('./resolveImages');
|
||||
var fetchRemoteImages = require('./fetchRemoteImages');
|
||||
|
||||
@@ -20,7 +21,8 @@ function inlineAssets(rootFolder, currentFile) {
|
||||
.then(fetchRemoteImages.bind(null, rootFolder, currentFile, $))
|
||||
|
||||
.then(svgToImg.bind(null, rootFolder, currentFile, $))
|
||||
.then(svgToPng.bind(null, rootFolder, currentFile, $));
|
||||
.then(svgToPng.bind(null, rootFolder, currentFile, $))
|
||||
.then(inlinePng.bind(null, rootFolder, currentFile, $));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
var crc = require('crc');
|
||||
var path = require('path');
|
||||
|
||||
var imagesUtil = require('../../utils/images');
|
||||
var fs = require('../../utils/fs');
|
||||
var LocationUtils = require('../../utils/location');
|
||||
|
||||
var editHTMLElement = require('./editHTMLElement');
|
||||
|
||||
/**
|
||||
Convert all inline PNG images to PNG file
|
||||
|
||||
@param {String} rootFolder
|
||||
@param {HTMLDom} $
|
||||
@return {Promise}
|
||||
*/
|
||||
function inlinePng(rootFolder, currentFile, $) {
|
||||
var currentDirectory = path.dirname(currentFile);
|
||||
|
||||
return editHTMLElement($, 'img', function($img) {
|
||||
var src = $img.attr('src');
|
||||
if (!LocationUtils.isDataURI(src)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We avoid generating twice the same PNG
|
||||
var hash = crc.crc32(src).toString(16);
|
||||
var fileName = hash + '.png';
|
||||
|
||||
// Result file path
|
||||
var filePath = path.join(rootFolder, fileName);
|
||||
|
||||
return fs.assertFile(filePath, function() {
|
||||
return imagesUtil.convertInlinePNG(src, filePath);
|
||||
})
|
||||
.then(function() {
|
||||
// Convert filename to a relative filename
|
||||
fileName = LocationUtils.relative(currentDirectory, fileName);
|
||||
|
||||
// Replace src
|
||||
$img.attr('src', fileName);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = inlinePng;
|
||||
@@ -16,7 +16,7 @@ function resolveImages(currentFile, $) {
|
||||
return editHTMLElement($, 'img', function($img) {
|
||||
var src = $img.attr('src');
|
||||
|
||||
if (LocationUtils.isExternal(src)) {
|
||||
if (LocationUtils.isExternal(src) || LocationUtils.isDataURI(src)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
var is = require('is');
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
var Config = require('../models/config');
|
||||
|
||||
|
||||
@@ -9,6 +9,15 @@ describe('LocationUtils', function() {
|
||||
expect(LocationUtils.isExternal('test.md')).toBe(false);
|
||||
expect(LocationUtils.isExternal('folder/test.md')).toBe(false);
|
||||
expect(LocationUtils.isExternal('/folder/test.md')).toBe(false);
|
||||
expect(LocationUtils.isExternal('data:image/png')).toBe(false);
|
||||
});
|
||||
|
||||
it('should correctly test data:uri location', function() {
|
||||
expect(LocationUtils.isDataURI('data:image/png')).toBe(true);
|
||||
expect(LocationUtils.isDataURI('http://google.fr')).toBe(false);
|
||||
expect(LocationUtils.isDataURI('https://google.fr')).toBe(false);
|
||||
expect(LocationUtils.isDataURI('test.md')).toBe(false);
|
||||
expect(LocationUtils.isDataURI('data.md')).toBe(false);
|
||||
});
|
||||
|
||||
it('should correctly detect anchor location', function() {
|
||||
|
||||
+17
-1
@@ -38,7 +38,23 @@ function convertSVGBufferToPNG(buf, dest) {
|
||||
});
|
||||
}
|
||||
|
||||
// Converts a inline data: to png file
|
||||
function convertInlinePNG(source, dest) {
|
||||
if (!/^data\:image\/png/.test(source)) return Promise.reject(new Error('Source is not a PNG data-uri'));
|
||||
|
||||
var base64data = source.split('data:image/png;base64,')[1];
|
||||
var buf = new Buffer(base64data, 'base64');
|
||||
|
||||
return fs.writeFile(dest, buf)
|
||||
.then(function() {
|
||||
if (fs.existsSync(dest)) return;
|
||||
|
||||
throw new Error('Error converting '+source+' into '+dest);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
convertSVGToPNG: convertSVGToPNG,
|
||||
convertSVGBufferToPNG: convertSVGBufferToPNG
|
||||
convertSVGBufferToPNG: convertSVGBufferToPNG,
|
||||
convertInlinePNG: convertInlinePNG
|
||||
};
|
||||
+12
-2
@@ -4,7 +4,16 @@ var path = require('path');
|
||||
// Is the url an external url
|
||||
function isExternal(href) {
|
||||
try {
|
||||
return Boolean(url.parse(href).protocol);
|
||||
return Boolean(url.parse(href).protocol) && !isDataURI(href);
|
||||
} catch(err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Is the url an iniline data-uri
|
||||
function isDataURI(href) {
|
||||
try {
|
||||
return Boolean(url.parse(href).protocol) && (url.parse(href).protocol === 'data:');
|
||||
} catch(err) {
|
||||
return false;
|
||||
}
|
||||
@@ -39,7 +48,7 @@ function normalize(s) {
|
||||
@return {String}
|
||||
*/
|
||||
function toAbsolute(_href, dir, outdir) {
|
||||
if (isExternal(_href)) return _href;
|
||||
if (isExternal(_href) || isDataURI(_href)) return _href;
|
||||
outdir = outdir == undefined? dir : outdir;
|
||||
|
||||
_href = normalize(_href);
|
||||
@@ -97,6 +106,7 @@ function areIdenticalPaths(p1, p2) {
|
||||
|
||||
module.exports = {
|
||||
areIdenticalPaths: areIdenticalPaths,
|
||||
isDataURI: isDataURI,
|
||||
isExternal: isExternal,
|
||||
isRelative: isRelative,
|
||||
isAnchor: isAnchor,
|
||||
|
||||
Reference in New Issue
Block a user