Skip to content

Commit

Permalink
fix: getAuthor / getImage move
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyreilly committed Feb 10, 2024
1 parent 885dbaf commit 6663726
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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} : {}),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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',
Expand Down

0 comments on commit 6663726

Please sign in to comment.