-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSON-LD structured data implementation for blog
- Loading branch information
1 parent
643a7fe
commit e0da5cf
Showing
13 changed files
with
255 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/docusaurus-theme-classic/src/theme/BlogListPage/StructuredData/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import {useBaseUrlUtils} from '@docusaurus/useBaseUrl'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import type {Props} from '@theme/BlogListPage/StructuredData'; | ||
|
||
export default function BlogListPageStructuredData(props: Props): JSX.Element { | ||
const {siteConfig} = useDocusaurusContext(); | ||
const {withBaseUrl} = useBaseUrlUtils(); | ||
|
||
const { | ||
metadata: {blogDescription, blogTitle, permalink}, | ||
} = props; | ||
|
||
const url = `${siteConfig.url}${permalink}`; | ||
|
||
// details on structured data support: https://schema.org/Blog | ||
const blogStructuredData = { | ||
'@context': 'https://schema.org', | ||
'@type': 'Blog', | ||
'@id': url, | ||
mainEntityOfPage: url, | ||
headline: blogTitle, | ||
description: blogDescription, | ||
blogPost: props.items.map((blogItem) => { | ||
const { | ||
content: {assets, frontMatter, metadata}, | ||
} = blogItem; | ||
const {date, title, description} = metadata; | ||
|
||
const image = assets.image ?? frontMatter.image; | ||
const keywords = frontMatter.keywords ?? []; | ||
|
||
// an array of https://schema.org/Person | ||
const authorsStructuredData = metadata.authors.map((author) => ({ | ||
'@type': 'Person', | ||
...(author.name ? {name: author.name} : {}), | ||
...(author.title ? {description: author.title} : {}), | ||
...(author.url ? {url: author.url} : {}), | ||
...(author.email ? {email: author.email} : {}), | ||
...(author.imageURL ? {image: author.imageURL} : {}), | ||
})); | ||
|
||
const blogUrl = `${siteConfig.url}${metadata.permalink}`; | ||
const imageUrl = image ? withBaseUrl(image, {absolute: true}) : undefined; | ||
|
||
return { | ||
'@type': 'BlogPosting', | ||
'@id': blogUrl, | ||
mainEntityOfPage: blogUrl, | ||
url: blogUrl, | ||
headline: title, | ||
name: title, | ||
description, | ||
datePublished: date, | ||
author: | ||
authorsStructuredData.length === 1 | ||
? authorsStructuredData[0] | ||
: authorsStructuredData, | ||
...(image | ||
? { | ||
// details on structured data support: https://schema.org/ImageObject | ||
image: { | ||
'@type': 'ImageObject', | ||
'@id': imageUrl, | ||
url: imageUrl, | ||
contentUrl: imageUrl, | ||
caption: `title image for the blog post: ${title}`, | ||
}, | ||
} | ||
: {}), | ||
...(keywords ? {keywords} : {}), | ||
}; | ||
}), | ||
}; | ||
|
||
return ( | ||
<script | ||
type="application/ld+json" | ||
// We're using dangerouslySetInnerHTML because we want to avoid React | ||
// transforming transforming quotes into " which upsets parsers. | ||
// The entire contents is a stringified JSON object so it is safe | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{ | ||
__html: JSON.stringify(blogStructuredData), | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.