From 66637260f56fc384a77208d2c213cd92f8db5156 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sat, 10 Feb 2024 07:14:53 +0000 Subject: [PATCH] fix: getAuthor / getImage move --- .../BlogListPage/StructuredData/index.tsx | 20 +++++---- .../BlogPostPage/StructuredData/index.tsx | 45 ++++++++++++------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/BlogListPage/StructuredData/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogListPage/StructuredData/index.tsx index 01081da1e7d9..db533e2ae0bc 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogListPage/StructuredData/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogListPage/StructuredData/index.tsx @@ -14,8 +14,19 @@ import { } from '@docusaurus/theme-common'; import type {Props} from '@theme/BlogListPage/StructuredData'; import StructuredData from '@theme/StructuredData'; +import type {Author} from '@docusaurus/plugin-content-blog'; import type {Blog, WithContext} from 'schema-dts'; +function getAuthor(authors: Author[]) { + const authorsStructuredData = authors.map(makePersonStructuredData); + return { + author: + authorsStructuredData.length === 1 + ? authorsStructuredData[0] + : authorsStructuredData, + }; +} + function getImage( image: string | undefined, withBaseUrl: (url: string, options?: BaseUrlOptions | undefined) => string, @@ -58,10 +69,6 @@ export default function BlogListPageStructuredData(props: Props): JSX.Element { const image = assets.image ?? frontMatter.image; const keywords = frontMatter.keywords ?? []; - const authorsStructuredData = metadata.authors.map( - makePersonStructuredData, - ); - const blogUrl = `${siteConfig.url}${metadata.permalink}`; return { @@ -73,10 +80,7 @@ export default function BlogListPageStructuredData(props: Props): JSX.Element { name: title, description, datePublished: date, - author: - authorsStructuredData.length === 1 - ? authorsStructuredData[0] - : authorsStructuredData, + ...getAuthor(metadata.authors), ...getImage(image, withBaseUrl, title), ...(keywords ? {keywords} : {}), }; diff --git a/packages/docusaurus-theme-classic/src/theme/BlogPostPage/StructuredData/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogPostPage/StructuredData/index.tsx index f7e5fe9bd3ee..9a09b1b84481 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogPostPage/StructuredData/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogPostPage/StructuredData/index.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import {useBaseUrlUtils} from '@docusaurus/useBaseUrl'; +import {type BaseUrlOptions, useBaseUrlUtils} from '@docusaurus/useBaseUrl'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import { makeImageStructuredData, @@ -15,6 +15,32 @@ import { import type {Props} from '@theme/BlogPostPage/StructuredData'; import StructuredData from '@theme/StructuredData'; import type {BlogPosting, WithContext} from 'schema-dts'; +import type {Author} from '@docusaurus/plugin-content-blog'; + +function getAuthor(authors: Author[]) { + const authorsStructuredData = authors.map(makePersonStructuredData); + return { + author: + authorsStructuredData.length === 1 + ? authorsStructuredData[0] + : authorsStructuredData, + }; +} + +function getImage( + image: string | undefined, + withBaseUrl: (url: string, options?: BaseUrlOptions | undefined) => string, + title: string, +) { + return image + ? { + image: makeImageStructuredData({ + imageUrl: withBaseUrl(image, {absolute: true}), + caption: `title image for the blog post: ${title}`, + }), + } + : {}; +} export default function BlogPostStructuredData(props: Props): JSX.Element { const {siteConfig} = useDocusaurusContext(); @@ -25,9 +51,6 @@ export default function BlogPostStructuredData(props: Props): JSX.Element { const image = assets.image ?? frontMatter.image; const keywords = frontMatter.keywords ?? []; - // an array of https://schema.org/Person - const authorsStructuredData = metadata.authors.map(makePersonStructuredData); - const url = `${siteConfig.url}${metadata.permalink}`; // details on structured data support: https://schema.org/BlogPosting @@ -43,18 +66,8 @@ export default function BlogPostStructuredData(props: Props): JSX.Element { name: title, description, datePublished: date, - author: - authorsStructuredData.length === 1 - ? authorsStructuredData[0] - : authorsStructuredData, - ...(image - ? { - image: makeImageStructuredData({ - imageUrl: withBaseUrl(image, {absolute: true}), - caption: `title image for the blog post: ${title}`, - }), - } - : {}), + ...getAuthor(metadata.authors), + ...getImage(image, withBaseUrl, title), ...(keywords ? {keywords} : {}), isPartOf: { '@type': 'Blog',