From 198ea1330539d916c511152dd64cb224d4cc44bb Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 24 Nov 2022 17:03:51 +0000 Subject: [PATCH] fix: structured data markup stringify --- src/components/site/SitePage.tsx | 36 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/components/site/SitePage.tsx b/src/components/site/SitePage.tsx index 1f6d5cc8..7aae4b8e 100644 --- a/src/components/site/SitePage.tsx +++ b/src/components/site/SitePage.tsx @@ -10,27 +10,31 @@ export const SitePage: React.FC<{ page?: Note | null site?: Profile | null }> = ({ page, site }) => { - const author = useGetUserSites(page?.authors[0]) + const author = useGetUserSites(page?.authors?.[0]) function addPageJsonLd() { return { - __html: `{ + __html: JSON.stringify({ "@context": "https://schema.org", "@type": "BlogPosting", - "headline": "${page?.title}", - "image": [ - "${page?.cover}" - ], - "datePublished": "${page?.date_published}", - "dateModified": "${page?.date_updated}", - "author": [{ - "@type": "Person", - "name": "${author?.data?.[0]?.name}", - "url": "${getSiteLink({ - subdomain: author?.data?.[0].username || "", - })}" - }] - }`, + headline: page?.title, + ...(page?.cover && { + image: [page?.cover], + }), + datePublished: page?.date_published, + dateModified: page?.date_updated, + ...(author && { + author: [ + { + "@type": "Person", + name: author?.data?.[0]?.name, + url: getSiteLink({ + subdomain: author?.data?.[0]?.username || "", + }), + }, + ], + }), + }), } }