UNPKG

@mirrormedia/lilith-draft-renderer

Version:
147 lines (125 loc) 5.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertRecommendInContentBlock = exports.getSideIndexEntityData = exports.getFirstBlockEntityType = void 0; var _common = require("./common"); // eslint-disable-next-line prettier/prettier const insertRecommendInContentBlock = (rawContentBlock, insertRecommend) => { const relatedPostsEntityMaps = insertRecommend === null || insertRecommend === void 0 ? void 0 : insertRecommend.map(post => ({ data: { posts: [{ heroImage: (post === null || post === void 0 ? void 0 : post.heroImage) || {}, id: (post === null || post === void 0 ? void 0 : post.id) || '', name: (post === null || post === void 0 ? void 0 : post.title) || '', ogImage: (post === null || post === void 0 ? void 0 : post.ogImage) || null, slug: (post === null || post === void 0 ? void 0 : post.slug) || '', subtitle: null }] }, type: 'RELATEDPOST', mutability: 'IMMUTABLE' })); const insertRelatedEntities = relatedPostsEntityMaps.reduce((accumulator, current) => { var _current$data; // +1000 to increase diversity to avoid `key` duplication const entityKey = Number(current === null || current === void 0 ? void 0 : (_current$data = current.data) === null || _current$data === void 0 ? void 0 : _current$data.posts[0].id) + 1000; return { ...accumulator, [entityKey]: current }; }, {}); const entityMapWithInsertRecommend = { ...rawContentBlock.entityMap, ...insertRelatedEntities }; const relatedPostsBlocks = insertRecommend.map((post, index) => { // +1000 to increase diversity to avoid `key` duplication const entityKey = Number(post.id) + 1000; return { key: `insertRecommend-${index}`, data: {}, text: ' ', //notice: if text: '' will show error type: 'atomic', depth: 0, entityRanges: [{ key: entityKey, length: 1, offset: 0 }], inlineStyleRanges: [] }; }); function insertRecommendBlocks(data) { let i = 0; let count = 0; // B: insert recommends based on related-posts amount const paragraphs = data === null || data === void 0 ? void 0 : data.filter(item => (item === null || item === void 0 ? void 0 : item.type) === 'unstyled' && (item === null || item === void 0 ? void 0 : item.text.length)); let divideAmount; if (relatedPostsBlocks.length) { divideAmount = Math.round((paragraphs === null || paragraphs === void 0 ? void 0 : paragraphs.length) / (relatedPostsBlocks.length + 1)) || (paragraphs !== null && paragraphs !== void 0 && paragraphs.length ? 1 : 0); } else { divideAmount = 0; } if (data !== null && data !== void 0 && data.length) { while (i < data.length && divideAmount) { var _data$i, _data$i2; if (((_data$i = data[i]) === null || _data$i === void 0 ? void 0 : _data$i.type) === 'unstyled' && (_data$i2 = data[i]) !== null && _data$i2 !== void 0 && _data$i2.text.length) { count++; const item = relatedPostsBlocks[count / divideAmount - 1]; if (count % divideAmount === 0 && item) { data.splice(i + 1, 0, item); } } i++; } } // A: insert recommends each 5 paragraphs (same as READr 2.0) // if (data?.length) { // while (i < data.length) { // if (data[i]?.type === 'unstyled' && data[i]?.text.length) { // count++ // const item = relatedPostsBlocks[count / 5 - 1] // if (count % 5 === 0 && item) { // data.splice(i + 1, 0, item) // } // } // i++ // } // } return data; } const contentWithoutEmptyBlock = (0, _common.removeEmptyContentBlock)(rawContentBlock); const contentWithInsertRecommend = { blocks: insertRecommendBlocks(contentWithoutEmptyBlock === null || contentWithoutEmptyBlock === void 0 ? void 0 : contentWithoutEmptyBlock.blocks), entityMap: entityMapWithInsertRecommend }; return contentWithInsertRecommend; }; exports.insertRecommendInContentBlock = insertRecommendInContentBlock; const getFirstBlockEntityType = rawContentBlock => { const contentBlocks = (0, _common.removeEmptyContentBlock)(rawContentBlock); if (contentBlocks) { var _contentBlocks$entity; return contentBlocks === null || contentBlocks === void 0 ? void 0 : (_contentBlocks$entity = contentBlocks.entityMap[0]) === null || _contentBlocks$entity === void 0 ? void 0 : _contentBlocks$entity.type; } else { return undefined; } }; exports.getFirstBlockEntityType = getFirstBlockEntityType; const getSideIndexEntityData = rawContentBlock => { const contentBlocks = (0, _common.removeEmptyContentBlock)(rawContentBlock); if (contentBlocks !== null && contentBlocks !== void 0 && contentBlocks.entityMap) { return Object.values(contentBlocks.entityMap).filter(entity => entity.type === 'SIDEINDEX').map(entity => { const content = entity.data ?? {}; const sideIndexTitle = content.sideIndexText || content.h2Text || ''; const key = sideIndexTitle.replace(/\s+/g, ''); return { title: sideIndexTitle, id: `header-${key}`, href: (content === null || content === void 0 ? void 0 : content.sideIndexUrl) || null, isActive: false }; }); } else { return undefined; } }; exports.getSideIndexEntityData = getSideIndexEntityData;