diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-03 12:10:09 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-05-03 12:10:09 +0200 |
commit | c84eaa83dd3bf307d5cc35b01febb2b6cc6ffbab (patch) | |
tree | f65d81365e10ccd831455df300db3e8c4086a9a8 /lib/utils | |
parent | 6b17d08892828818216a260576c83b7203c2098f (diff) | |
parent | 74c3ff80cbc64eb4fb4252ca4cc08076d44c6be2 (diff) | |
download | gitbook-c84eaa83dd3bf307d5cc35b01febb2b6cc6ffbab.zip gitbook-c84eaa83dd3bf307d5cc35b01febb2b6cc6ffbab.tar.gz gitbook-c84eaa83dd3bf307d5cc35b01febb2b6cc6ffbab.tar.bz2 |
Merge pull request #1259 from GitbookIO/png-data-uri
Adding data-uri support for PNG Images in Books
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/__tests__/location.js | 9 | ||||
-rw-r--r-- | lib/utils/images.js | 18 | ||||
-rw-r--r-- | lib/utils/location.js | 14 |
3 files changed, 38 insertions, 3 deletions
diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js index f2037ff..1d75751 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 e387d6b..6d4b927 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 84a71ad..1afe415 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, |