UNPKG

noodl-loader

Version:

Loader for noodl applications

97 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const noodl_core_1 = require("noodl-core"); const yml_1 = require("../../utils/yml"); const constants_1 = require("../../constants"); const isImage = (v) => typeof v === 'string' && /.(jpg|jpeg|gif|png|svg|tif|bmp|webp)$/i.test(v); const extractImages = (key, node, path, { assetsUrl = '', cadlEndpoint, createAsset, control }) => { var _a; if ((0, yml_1.isMap)(node) && node.has('path') && node.get('type', false) === 'image') { const value = node.get('path', false) || ''; createAsset({ type: constants_1.ExtractType.Asset, id: value, props: { type: 'image', path: value, url: ((_a = value.startsWith) === null || _a === void 0 ? void 0 : _a.call(value, 'http')) ? value : `${assetsUrl}${value}`, }, }); } if ((0, yml_1.isScalar)(node) && noodl_core_1.is.str(node.value)) { let pathLength = path.length; let prevNode; if (pathLength > 0) { prevNode = path[pathLength - 1]; } else { // } if ((0, yml_1.isPair)(prevNode)) { const pairKey = String(prevNode.key); if (pairKey === 'path') { if (key === 'value') { const url = cadlEndpoint === null || cadlEndpoint === void 0 ? void 0 : cadlEndpoint.createAssetURL(node.value); createAsset({ type: constants_1.ExtractType.Asset, id: url, props: { url } }); } } else { if (key === 'value') { if (isImage(node.value)) { const url = cadlEndpoint === null || cadlEndpoint === void 0 ? void 0 : cadlEndpoint.createAssetURL(node.value); createAsset({ type: constants_1.ExtractType.Asset, id: url, props: { url } }); } } } } else { if (key === 'value') { if (isImage(node.value)) { const url = cadlEndpoint === null || cadlEndpoint === void 0 ? void 0 : cadlEndpoint.createAssetURL(node.value); createAsset({ type: constants_1.ExtractType.Asset, id: url, props: { url } }); } } } } else if ((0, yml_1.isMap)(node)) { for (const pair of node.items) { const pairKey = String(pair.key); if (pairKey === 'path') { if ((0, yml_1.isScalar)(pair.value)) { if (noodl_core_1.is.str(pair.value.value)) { const value = pair.value.value; if (isImage(value)) { const url = cadlEndpoint === null || cadlEndpoint === void 0 ? void 0 : cadlEndpoint.createAssetURL(value); createAsset({ type: constants_1.ExtractType.Asset, id: url, props: { url } }); } } } else if ((0, yml_1.isMap)(pair.value)) { // Emit/if } } else { if ((0, yml_1.isScalar)(pair.value)) { if (key === 'value') { const value = pair.value.value; if (noodl_core_1.is.str(value)) { if (noodl_core_1.is.reference(value)) { // } else if (isImage(value)) { const url = cadlEndpoint === null || cadlEndpoint === void 0 ? void 0 : cadlEndpoint.createAssetURL(value); createAsset({ type: constants_1.ExtractType.Asset, id: url, props: { url }, }); } } } } } } } }; exports.default = extractImages; //# sourceMappingURL=extract-images.js.map