From bda7b0ef5743849a737f7924e503aa637016f387 Mon Sep 17 00:00:00 2001 From: Jonas Ege Carlsen Date: Fri, 10 Nov 2023 13:40:24 +0100 Subject: [PATCH] Remove 404 concepts from article --- src/api/embedsApi.ts | 15 ++++++++++++++- src/api/resourceEmbedApi.ts | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api/embedsApi.ts b/src/api/embedsApi.ts index 5fb9ec6a..0210ec27 100644 --- a/src/api/embedsApi.ts +++ b/src/api/embedsApi.ts @@ -28,6 +28,7 @@ import { EmbedData, ConceptVisualElementMeta, CampaignBlockMetaData, + ConceptData, } from '@ndla/types-embed'; import { IImageMetaInformationV3 } from '@ndla/types-backend/image-api'; import { load } from 'cheerio'; @@ -165,6 +166,7 @@ export interface TransformOptions { absoluteUrl?: boolean; subject?: string; shortCircuitOnError?: boolean; + standalone?: boolean; } const footnoteMeta: Fetch< @@ -434,7 +436,18 @@ export const transformEmbed = async ( } else if (embedData.resource === 'related-content') { meta = await relatedContentMeta({ embedData, context, index, opts }); } else if (embedData.resource === 'concept') { - meta = await conceptMeta({ embedData, context, index, opts }); + const response: ConceptData | undefined = await conceptMeta({ + embedData, + context, + index, + opts, + }).catch(_ => undefined); + // If the concept does not exist and we are not requesting a standalone concept, remove it from the article. + if (!opts.standalone && !response) { + embed.embed.replaceWith(''); + return; + } + meta = response; embedData.pageUrl = `/${embedData.resource}/${embedData.contentId}`; } else if (embedData.resource === 'concept-list') { meta = await conceptListMeta({ embedData, context, index, opts }); diff --git a/src/api/resourceEmbedApi.ts b/src/api/resourceEmbedApi.ts index 72ec6db8..76878376 100644 --- a/src/api/resourceEmbedApi.ts +++ b/src/api/resourceEmbedApi.ts @@ -114,6 +114,7 @@ export const fetchResourceEmbed = async ( const embeds = getEmbedsFromContent(html)[0]; const embedPromise = await transformEmbed(embeds, context, 0, 0, { shortCircuitOnError: true, + standalone: true, }); const metadata = toArticleMetaData([embedPromise]); @@ -143,6 +144,7 @@ export const fetchResourceEmbeds = async ( embedsFromContent.map((embed, index) => transformEmbed(embed, context, index, 0, { shortCircuitOnError: true, + standalone: true, }), ), );