diff --git a/lib/routes/gcores/category.js b/lib/routes/gcores/category.js index ee0e0169f..d1f17f78f 100644 --- a/lib/routes/gcores/category.js +++ b/lib/routes/gcores/category.js @@ -51,12 +51,28 @@ module.exports = async (ctx) => { // replace figure with img const articleContent = JSON.parse(articleData.attributes.content); - const imgs = Object.values(articleContent.entityMap) - .filter((e) => e.type === 'IMAGE') - .map((e) => e.data.path); + const entityRangeMap = {}; + for (const block of articleContent.blocks || []) { + if (block.entityRanges.length) { + entityRangeMap[block.key] = block.entityRanges; + } + } + $('figure').each((i, elem) => { - if (imgs[i]) { - $(elem).replaceWith($(``)); + const keyAttr = elem.attribs['data-offset-key']; + const keyMatch = /^(\w+)-(\d+)-(\d)$/.exec(keyAttr); + let actualContent = ''; + if (keyMatch) { + const [, key, index] = keyMatch; + if (entityRangeMap[key] && entityRangeMap[key][index]) { + const entityKey = entityRangeMap[key] && entityRangeMap[key][index].key; + const entity = articleContent.entityMap[entityKey]; + actualContent = convertEntityToContent(entity); + } + } + + if (actualContent) { + $(elem).replaceWith(actualContent); } }); @@ -82,3 +98,30 @@ module.exports = async (ctx) => { item: out, }; }; + +function convertEntityToContent(entity) { + const { type, data } = entity; + switch (type) { + case 'IMAGE': + return ` +
+${data.caption || ''} +${data.caption ? `
${data.caption}
` : ''} +
`; + + case 'GALLERY': + return data.images + .map( + (image, i, arr) => ` +
+${image.caption || ''} +
${data.caption || ''} (${i + 1}/${arr.length}) ${image.caption || ''}
+
+ ` + ) + .join(''); + + default: + return ''; + } +}