-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: structured data for blog posts
- Loading branch information
1 parent
1815187
commit aa5b95b
Showing
3 changed files
with
70 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
packages/docusaurus-theme-classic/src/theme/BlogPostStructuredData/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,46 @@ | ||
/** | ||
* 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 Head from '@docusaurus/Head'; | ||
import type {Props} from '@theme/BlogPostStructuredData'; | ||
|
||
function BlogPostStructuredData(props: Props): JSX.Element { | ||
const {frontMatter, frontMatterAssets, metadata} = props; | ||
const {date, title, description} = metadata; | ||
|
||
const image = frontMatterAssets.image ?? frontMatter.image; | ||
|
||
const authorURL = frontMatter.author_url || frontMatter.authorURL; | ||
const authorTitle = frontMatter.author_title || frontMatter.authorTitle; | ||
|
||
// details on structured data support: https://developers.google.com/search/docs/data-types/article#non-amp | ||
// and https://schema.org/BlogPosting | ||
const blogPostStructuredData = { | ||
'@context': 'https://schema.org', | ||
'@type': 'BlogPosting', | ||
headline: title, | ||
description, | ||
...(image ? {image: [image]} : {}), | ||
datePublished: date, | ||
author: { | ||
'@type': 'Person', | ||
name: authorTitle, | ||
url: authorURL, | ||
}, | ||
}; | ||
|
||
return ( | ||
<Head> | ||
<script type="application/ld+json"> | ||
{JSON.stringify(blogPostStructuredData)} | ||
</script> | ||
</Head> | ||
); | ||
} | ||
|
||
export default BlogPostStructuredData; |
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