From 1febd0b49f0586353b25a89e6b6fe85394736d44 Mon Sep 17 00:00:00 2001 From: Igor Korovchenko Date: Wed, 27 Dec 2023 17:28:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B8=D0=BD=D0=B8=D1=82=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=BC=D0=BE=20=D0=BF=D0=BE=D0=B4=20=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D1=83=D1=8E=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8E=2011ty=20(#?= =?UTF-8?q?1229)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/index.11tydata.js | 38 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/views/index.11tydata.js b/src/views/index.11tydata.js index 2ba4e68e..4e9d7fe5 100644 --- a/src/views/index.11tydata.js +++ b/src/views/index.11tydata.js @@ -26,6 +26,11 @@ const defaultPromo = { title: 'Друг для друга', } +const asyncFilter = async (arr, predicate) => { + const results = await Promise.all(arr.map(predicate)) + return arr.filter((_v, index) => results[index]) +} + module.exports = { layout: 'base.njk', permalink: '/', @@ -33,31 +38,36 @@ module.exports = { documentDescription: 'Документация для разработчиков на понятном языке', eleventyComputed: { - promoData: function (data) { + promoData: async function (data) { const { collections } = data const { promos } = collections - const promo = promos - .filter((p) => { - const currentDate = new Date() - return currentDate >= new Date(p.data.startDate) && currentDate <= new Date(p.data.endDate) - }) - .map((p) => { + const filteredPromos = await asyncFilter(promos, async (promo) => { + const currentDate = new Date() + const cache = await promo.template._frontMatterDataCache + return currentDate >= new Date(cache.startDate) && currentDate <= new Date(cache.endDate) + }) + + const formattedPromos = await Promise.all( + filteredPromos.map(async (promo) => { + const content = await promo.template.inputContent + const cache = await promo.template._frontMatterDataCache const output = { - color: p.data.color, - content: p.template.inputContent + color: cache.color, + content: content .split('---')[2] .split('\n') .filter((s) => s !== '') .join('\n'), - design: p.data.design, - links: p.data.links, - title: p.data.title, + design: cache.design, + links: cache.links, + title: cache.title, } return output - })[0] + }), + ) - return promo || defaultPromo + return formattedPromos[0] || defaultPromo }, }, }