"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const crc_1 = __importDefault(require("crc")); const editHTMLElement_1 = __importDefault(require("./editHTMLElement")); const fs_1 = __importDefault(require("../../utils/fs")); const location_1 = __importDefault(require("../../utils/location")); /** Fetch all remote images */ function fetchRemoteImages(rootFolder, currentFile, $) { const currentDirectory = path_1.default.dirname(currentFile); return (0, editHTMLElement_1.default)($, "img", ($img) => { let src = $img.attr("src"); const extension = path_1.default.extname(src); if (!location_1.default.isExternal(src)) { return; } // We avoid generating twice the same PNG const hash = crc_1.default.crc32(src).toString(16); const fileName = hash + extension; const filePath = path_1.default.join(rootFolder, fileName); return fs_1.default .assertFile(filePath, () => { return fs_1.default.download(src, filePath); }) .then(() => { // Convert to relative src = location_1.default.relative(currentDirectory, fileName); $img.replaceWith(``); }); }); } exports.default = fetchRemoteImages;