UNPKG

burdy

Version:

Open Source Headless Platform

192 lines (191 loc) 9.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseInternalMetaContent = exports.parseContent = void 0; const object_1 = require("./object"); const lodash_1 = __importDefault(require("lodash")); const mappers_1 = require("./mappers"); const parseContent = (post, path) => { const metaObjFlattened = {}; ((post === null || post === void 0 ? void 0 : post.meta) || []).forEach((item) => { metaObjFlattened[item.key] = item.value; }); const metaObj = (0, object_1.unflatten)(metaObjFlattened); const assets = {}; const references = {}; const parseCheckbox = (content) => { return content === 'true' || content === true; }; const parseRichtext = (content) => { try { const richtext = JSON.parse(content); Object.keys((richtext === null || richtext === void 0 ? void 0 : richtext.entityMap) || {}).forEach(key => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; try { if (((_b = (_a = richtext === null || richtext === void 0 ? void 0 : richtext.entityMap) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.type) === 'IMAGE' && ((_e = (_d = (_c = richtext === null || richtext === void 0 ? void 0 : richtext.entityMap) === null || _c === void 0 ? void 0 : _c[key]) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.npath)) { richtext.entityMap[key].data.src = (0, mappers_1.getAssetsSrc)(richtext.entityMap[key].data.npath); } else if (((_g = (_f = richtext === null || richtext === void 0 ? void 0 : richtext.entityMap) === null || _f === void 0 ? void 0 : _f[key]) === null || _g === void 0 ? void 0 : _g.type) === 'COMPONENT') { richtext.entityMap[key].data = Object.assign(Object.assign({}, (richtext.entityMap[key].data || {})), { value: parseGroup(((_k = (_j = (_h = richtext === null || richtext === void 0 ? void 0 : richtext.entityMap) === null || _h === void 0 ? void 0 : _h[key]) === null || _j === void 0 ? void 0 : _j.data) === null || _k === void 0 ? void 0 : _k.value) || {}) }); } } catch (_l) { // } }); return richtext; } catch (_a) { return null; } }; const parseAssets = (content, path) => { try { const parsed = JSON.parse(content); (parsed || []).forEach((asset, index) => { assets[`${path}.${index}`] = asset === null || asset === void 0 ? void 0 : asset.npath; }); return parsed; } catch (_a) { return null; } }; const parseRepeatable = (content, path) => { const array = []; (content || []).forEach((elContent, index) => { const itemPath = path ? `${path}.[${index}]` : `[${index}]`; array.push(parseGroup(elContent, itemPath)); }); return array; }; const parseZone = (content, path) => { const array = []; (content || []).forEach((elContent, index) => { const itemPath = path ? `${path}.[${index}]` : `[${index}]`; array.push(parseGroup(elContent, itemPath)); }); return array; }; const parseRelation = (content, path) => { try { const parsed = JSON.parse(content); (parsed || []).forEach((relation, index) => { references[`${path}.${index}`] = relation === null || relation === void 0 ? void 0 : relation.slugPath; }); return parsed; } catch (_a) { return null; } }; const parseReferenceSingle = (content, path) => { references[path] = content === null || content === void 0 ? void 0 : content.slugPath; }; const parseReferenceMultiple = (content, path) => { if (Array.isArray(content || [])) { (content || []).forEach((reference, index) => { references[`${path}.${index}`] = reference === null || reference === void 0 ? void 0 : reference.slugPath; }); } return null; }; const parseGroup = (content = {}, path) => { if (lodash_1.default.isEmpty(content)) return null; const groupContent = lodash_1.default.cloneDeep(content); Object.keys(content).forEach(key => { if (key.endsWith('_$type')) { const contentKey = key.slice(0, key.indexOf('_$type')); const newPath = path ? `${path}.${contentKey}` : contentKey; switch (content[key]) { case 'images': case 'assets': groupContent[contentKey] = parseAssets(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'group': groupContent[contentKey] = parseGroup(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'repeatable': groupContent[contentKey] = parseRepeatable(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'custom': groupContent[contentKey] = parseGroup(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'richtext': groupContent[contentKey] = parseRichtext(content === null || content === void 0 ? void 0 : content[contentKey]); break; case 'zone': groupContent[contentKey] = parseZone(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'relation': groupContent[contentKey] = parseRelation(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'reference_single': groupContent[contentKey] = parseReferenceSingle(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'reference_multiple': groupContent[contentKey] = parseReferenceMultiple(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'checkbox': groupContent[contentKey] = parseCheckbox(content === null || content === void 0 ? void 0 : content[contentKey]); break; default: break; } } }); return groupContent; }; const group = parseGroup(metaObj === null || metaObj === void 0 ? void 0 : metaObj.content, path); return { content: group, assets, references }; }; exports.parseContent = parseContent; const parseInternalMetaContent = (post, path) => { const metaObjFlattened = {}; ((post === null || post === void 0 ? void 0 : post.meta) || []).forEach((item) => { metaObjFlattened[item.key] = item.value; }); const metaObj = (0, object_1.unflatten)(metaObjFlattened); const parseList = (content, path) => { const array = []; (content || []).filter(el => !!el).forEach((elContent, index) => { const itemPath = path ? `${path}.[${index}]` : `[${index}]`; array.push(parseGroup(elContent, itemPath)); }); return array; }; const parseGroup = (content = {}, path) => { if (!content) content = {}; const groupContent = lodash_1.default.cloneDeep(content); Object.keys(content).forEach(key => { if (key.endsWith('_$type')) { const contentKey = key.slice(0, key.indexOf('_$type')); const newPath = path ? `${path}.${contentKey}` : contentKey; switch (content[key]) { case 'group': case 'custom': groupContent[contentKey] = parseGroup(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; case 'zone': case 'repeatable': groupContent[contentKey] = parseList(content === null || content === void 0 ? void 0 : content[contentKey], newPath); break; default: break; } } }); return groupContent; }; const group = parseGroup(metaObj === null || metaObj === void 0 ? void 0 : metaObj.content, path); return Object.assign(Object.assign({}, metaObj), { content: group }); }; exports.parseInternalMetaContent = parseInternalMetaContent;