diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-23 21:10:53 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-23 21:10:53 +0200 |
commit | a3df6c6f0c1068762f9d48cdff97ab5d4c583082 (patch) | |
tree | e43d732f7676430d0075814e11fcb4372803e9da /lib/output/modifiers/fetchRemoteImages.js | |
parent | e1fa977b5b1b3c03790de6e2c21ee39ba55d9555 (diff) | |
download | gitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.zip gitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.tar.gz gitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.tar.bz2 |
Add assets inliner modifier for HTML
Diffstat (limited to 'lib/output/modifiers/fetchRemoteImages.js')
-rw-r--r-- | lib/output/modifiers/fetchRemoteImages.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/output/modifiers/fetchRemoteImages.js b/lib/output/modifiers/fetchRemoteImages.js new file mode 100644 index 0000000..792bbb6 --- /dev/null +++ b/lib/output/modifiers/fetchRemoteImages.js @@ -0,0 +1,38 @@ +var path = require('path'); +var crc = require('crc'); + +var editHTMLElement = require('./editHTMLElement'); +var fs = require('../../utils/fs'); +var location = require('../../utils/location'); + +/** + Fetch all remote images + + @param {String} rootFolder + @param {HTMLDom} $ + @return {Promise} +*/ +function fetchRemoteImages(rootFolder, $) { + return editHTMLElement($, 'img', function($img) { + var src = $img.attr('src'); + var extension = path.extname(src); + + if (!location.isExternal(src)) { + return; + } + + // We avoid generating twice the same PNG + var hash = crc.crc32(src).toString(16); + var fileName = hash + extension; + var filePath = path.join(rootFolder, fileName); + + return fs.assertFile(filePath, function() { + return fs.download(src, filePath); + }) + .then(function() { + $img.replaceWith('<img src="/' + fileName + '" />'); + }); + }); +} + +module.exports = fetchRemoteImages; |