From 26dbbf0df1e1553fc45b8b020410ddf5a8404586 Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 17 Aug 2023 23:45:35 +0800 Subject: [PATCH] fix: point thumbnail of article to raw images on github --- src/pages/knowledge-base/index.page.tsx | 33 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/pages/knowledge-base/index.page.tsx b/src/pages/knowledge-base/index.page.tsx index 9e41cfa3..632969ea 100644 --- a/src/pages/knowledge-base/index.page.tsx +++ b/src/pages/knowledge-base/index.page.tsx @@ -5,7 +5,6 @@ import { useRouter } from 'next/router' import { useTranslation } from 'next-i18next' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import Pagination from 'src/components/Pagination' -import Image from 'next/image' import Category from '../../components/Category' import { Page } from '../../components/Page' import { getTimeFormatter } from '../../utils' @@ -27,9 +26,33 @@ const patchCover = (src: string) => { if (!src.startsWith('/')) { return src } + /* + * This is a patch to point cover thumbnail to the one generated on the article page + * It returns 404 if the article page has not been visited and the thumbnail has not been generated + */ return `/_next/image?url=${encodeURIComponent(src)}&w=384&q=75` } +const handleCoverNotFound = (e: React.SyntheticEvent) => { + const img = e.currentTarget + const { src } = img.dataset + + if (!src || !src.startsWith('/')) { + return + } + + /* + * This is a patch to point thumbnail to raw image on github + * https://github.com/Magickbase/nervos-official-website/issues/297#issuecomment-1682495769 + */ + const RAW_GITHUB_URL = 'https://raw.githubusercontent.com/NervosEducationHub/EducationHubArticles/main' + if (img.src.startsWith(RAW_GITHUB_URL)) { + return + } + const rawImageUrl = `${RAW_GITHUB_URL}${src.replace('/education_hub_articles', '')}` + img.src = rawImageUrl +} + const Index = ({ posts, populars, categories, pageCount }: Props) => { const [t] = useTranslation(['knowledge-base']) /* eslint-disable-next-line @typescript-eslint/unbound-method */ @@ -85,13 +108,15 @@ const Index = ({ posts, populars, categories, pageCount }: Props) => {
{post.excerpt}
{post.coverImage && ( - cover )} @@ -172,12 +197,14 @@ const Index = ({ posts, populars, categories, pageCount }: Props) => {
{post.title}
{post.excerpt}
{post.coverImage && ( - cover )} {post.category && (