UNPKG

dokkie

Version:

Create good looking documentation from your Readme

124 lines 5.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadAssets = void 0; const { readFile, writeFile, mkdir, stat } = require("fs").promises; const path_1 = require("path"); const _1 = require("./"); const log = __importStar(require("cli-block")); const downloadImage = (image, settings) => __awaiter(void 0, void 0, void 0, function* () { try { let imageFile = ""; const filePath = path_1.join(process.cwd(), settings.output, "img", path_1.basename(image)); yield _1.createFolder(path_1.dirname(filePath)); if (image.includes("http")) { yield _1.download(image, filePath).then(() => __awaiter(void 0, void 0, void 0, function* () { if (settings.logging.includes("debug")) { const stats = yield stat(filePath); log.BLOCK_SETTINGS(stats); } })); } else { imageFile = yield readFile(image).then((r) => r.toString()); yield writeFile(filePath, imageFile).then(() => __awaiter(void 0, void 0, void 0, function* () { if (settings.logging.includes("debug")) { const stats = yield stat(filePath); log.BLOCK_SETTINGS(stats); } })); } } catch (err) { throw Error(err); } }); exports.downloadAssets = (settings) => __awaiter(void 0, void 0, void 0, function* () { var _a; // Get images from content const contentImages = []; const imageRegex = new RegExp('<img src="(.*?)"[^>]+>'); if (!settings.skip.includes("download")) try { settings.files.forEach((file, index) => { const images = file.html.match(imageRegex); if (images) images.forEach((img) => { if (!img.includes("<img")) contentImages.push({ fileIdx: index, image: img, }); }); }); } catch (err) { throw Error(err); } // If there arent any image. Do nothing. if (!settings.assets && contentImages.length < 1) return settings; !settings.logging.includes("silent") && log.BLOCK_MID("Assets"); yield _1.createFolder(path_1.join(settings.output, "/img")); // Process Assets if ((_a = settings.assets) === null || _a === void 0 ? void 0 : _a.logo) try { yield downloadImage(settings.assets.logo, settings).then(() => { const filename = "/img/" + path_1.basename(settings.assets.logo); settings.assets.logo = filename; !settings.logging.includes("silent") && log.BLOCK_LINE_SUCCESS(filename); }); } catch (err) { throw Error(err); } // Process Content images function filenameFromUrl(str) { return str.split("/")[str.split("/").length - 1]; } if (contentImages && !settings.skip.includes("download")) try { yield _1.asyncForEach(contentImages, (img) => __awaiter(void 0, void 0, void 0, function* () { yield downloadImage(img.image, settings).then(() => { const filename = `${settings.url}/img/${filenameFromUrl(img.image)}`; settings.files[img.fileIdx].html = settings.files[img.fileIdx].html.replace(img.image, filename); !settings.logging.includes("silent") && log.BLOCK_LINE_SUCCESS(filename); }); })); } catch (err) { !settings.logging.includes("silent") && log.BLOCK_LINE_ERROR(`Couldn\'t download ${err.message.match(/'([^']+)'/)[1]}`); } return settings; }); //# sourceMappingURL=assets.js.map