MCPcopy
hub / github.com/GitbookIO/gitbook / fetchRemoteImages

Function fetchRemoteImages

lib/output/modifiers/fetchRemoteImages.js:16–42  ·  view source on GitHub ↗

Fetch all remote images @param {String} rootFolder @param {String} currentFile @param {HTMLDom} $ @return {Promise}

(rootFolder, currentFile, $)

Source from the content-addressed store, hash-verified

14 @return {Promise}
15*/
16function fetchRemoteImages(rootFolder, currentFile, $) {
17 var currentDirectory = path.dirname(currentFile);
18
19 return editHTMLElement($, 'img', function($img) {
20 var src = $img.attr('src');
21 var extension = path.extname(src);
22
23 if (!LocationUtils.isExternal(src)) {
24 return;
25 }
26
27 // We avoid generating twice the same PNG
28 var hash = crc.crc32(src).toString(16);
29 var fileName = hash + extension;
30 var filePath = path.join(rootFolder, fileName);
31
32 return fs.assertFile(filePath, function() {
33 return fs.download(src, filePath);
34 })
35 .then(function() {
36 // Convert to relative
37 src = LocationUtils.relative(currentDirectory, fileName);
38
39 $img.replaceWith('<img src="' + src + '" />');
40 });
41 });
42}
43
44module.exports = fetchRemoteImages;

Callers 1

Calls 1

editHTMLElementFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…