From 8284ff95c16c79707ddd34b0b6bfcc0324614e44 Mon Sep 17 00:00:00 2001 From: Botao Wang Date: Mon, 7 Dec 2020 02:02:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9C=BA=E6=A0=B8?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E4=B8=AD=E5=A4=9A=E5=9B=BE=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98=20(#6315)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/gcores/category.js | 53 +++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) 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 ''; + } +}