Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: structured data for blog posts #5322

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React from 'react';
import BlogLayout from '@theme/BlogLayout';
import BlogPostItem from '@theme/BlogPostItem';
import BlogPostStructuredData from '@theme/BlogPostStructuredData';
import BlogPostPaginator from '@theme/BlogPostPaginator';
import type {Props} from '@theme/BlogPostPage';
import {ThemeClassNames} from '@docusaurus/theme-common';
Expand All @@ -30,6 +31,11 @@ function BlogPostPage(props: Props): JSX.Element {
? BlogPostContents.toc
: undefined
}>
<BlogPostStructuredData
frontMatter={frontMatter}
frontMatterAssets={frontMatterAssets}
metadata={metadata}
/>
<BlogPostItem
frontMatter={frontMatter}
frontMatterAssets={frontMatterAssets}
Expand Down
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;
18 changes: 18 additions & 0 deletions packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ declare module '@theme/BlogPostItem' {
export default BlogPostItem;
}

declare module '@theme/BlogPostStructuredData' {
import type {
FrontMatter,
FrontMatterAssets,
Metadata,
} from '@theme/BlogPostPage';

export type Props = {
readonly frontMatter: FrontMatter;
readonly frontMatterAssets: FrontMatterAssets;
readonly metadata: Metadata;
readonly truncated?: string | boolean;
};

const BlogPostStructuredData: (props: Props) => JSX.Element;
export default BlogPostStructuredData;
}

declare module '@theme/BlogPostPaginator' {
type Item = {readonly title: string; readonly permalink: string};

Expand Down