diff --git a/lib/models/config.js b/lib/models/config.js index 83dd6d4c9..3310a9333 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -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); }; diff --git a/lib/output/modifiers/__tests__/inlinePng.js b/lib/output/modifiers/__tests__/inlinePng.js new file mode 100644 index 000000000..fb094f79e --- /dev/null +++ b/lib/output/modifiers/__tests__/inlinePng.js @@ -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('GitBook Logo 20x20'); + + return inlinePng(dir.name, 'index.html', $) + .then(function() { + var $img = $('img'); + var src = $img.attr('src'); + + expect(dir.name).toHaveFile(src); + }); + }); +}); + + diff --git a/lib/output/modifiers/inlineAssets.js b/lib/output/modifiers/inlineAssets.js index ee932eb58..7cd874b54 100644 --- a/lib/output/modifiers/inlineAssets.js +++ b/lib/output/modifiers/inlineAssets.js @@ -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, $)); }; } diff --git a/lib/output/modifiers/inlinePng.js b/lib/output/modifiers/inlinePng.js new file mode 100644 index 000000000..161f164d3 --- /dev/null +++ b/lib/output/modifiers/inlinePng.js @@ -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; diff --git a/lib/output/modifiers/resolveImages.js b/lib/output/modifiers/resolveImages.js index e401cf500..cc25cfa32 100644 --- a/lib/output/modifiers/resolveImages.js +++ b/lib/output/modifiers/resolveImages.js @@ -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; } diff --git a/lib/parse/parseConfig.js b/lib/parse/parseConfig.js index a1e9d69b2..5200de2f9 100644 --- a/lib/parse/parseConfig.js +++ b/lib/parse/parseConfig.js @@ -1,5 +1,3 @@ -var is = require('is'); - var Promise = require('../utils/promise'); var Config = require('../models/config'); diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js index f2037ffa3..1d75751dc 100644 --- a/lib/utils/__tests__/location.js +++ b/lib/utils/__tests__/location.js @@ -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() { diff --git a/lib/utils/images.js b/lib/utils/images.js index e387d6b36..6d4b927ad 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -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 }; \ No newline at end of file diff --git a/lib/utils/location.js b/lib/utils/location.js index 84a71ad6c..1afe4150e 100644 --- a/lib/utils/location.js +++ b/lib/utils/location.js @@ -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,