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

refactor(theme): use JSON-LD instead of microdata for blog structured data #9669

Merged
merged 23 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e0da5cf
feat: JSON-LD structured data implementation for blog
johnnyreilly Dec 25, 2023
6521052
Merge branch 'main' of https://github.com/johnnyreilly/docusaurus int…
johnnyreilly Dec 26, 2023
2bc8ca6
fix: tests
johnnyreilly Dec 26, 2023
25afce0
Update packages/docusaurus-theme-classic/src/theme/BlogPostPage/Struc…
johnnyreilly Jan 29, 2024
1df8ce4
Update packages/docusaurus-theme-classic/src/theme/BlogListPage/Struc…
johnnyreilly Jan 29, 2024
7c88ae0
feat: migrate to blogMetadata bundle/prop as suggested by @slorber
johnnyreilly Feb 9, 2024
79224e1
feat: dedicated StructuredData component
johnnyreilly Feb 9, 2024
c21d57a
feat: add structuredDataUtils
johnnyreilly Feb 9, 2024
c5961ea
feat: add schema-dts
johnnyreilly Feb 9, 2024
4cc7a50
fix: single blogMetadata
johnnyreilly Feb 9, 2024
885dbaf
fix: split out getImage
johnnyreilly Feb 10, 2024
6663726
fix: getAuthor / getImage move
johnnyreilly Feb 10, 2024
8ffcb58
fix: getBlogPost
johnnyreilly Feb 10, 2024
ce1b664
fix: baseBlogPermalink -> blogBasePath
johnnyreilly Feb 10, 2024
3ce60a1
fix: migrate structuredData logic to theme-common
johnnyreilly Feb 10, 2024
356d8f2
fix: move StructuredData to theme-common
johnnyreilly Feb 10, 2024
55433eb
fix: remove unnecessary prop
johnnyreilly Feb 10, 2024
e59c882
fix: less prop drilling
johnnyreilly Feb 10, 2024
0be47bd
fix: address review feedback
johnnyreilly Feb 14, 2024
6d26f5c
Add blog plugin useBlogMetadata() client hook + refactor to use it
slorber Feb 14, 2024
84a13d1
fix blog tests???
slorber Feb 14, 2024
20256fa
revert usage of StructuredData comp in docs
slorber Feb 15, 2024
96073e8
remove StructuredData component
slorber Feb 15, 2024
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 @@ -174,8 +174,6 @@ describe('blog plugin', () => {
title: 'date-matter',
description: `date inside front matter`,
authors: [],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
date: new Date('2019-01-01'),
formattedDate: 'January 1, 2019',
frontMatter: {
Expand Down Expand Up @@ -221,8 +219,6 @@ describe('blog plugin', () => {
title: 'Docusaurus maintainer (translated)',
},
],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
date: new Date('2018-12-14'),
formattedDate: 'December 14, 2018',
frontMatter: {
Expand Down Expand Up @@ -254,8 +250,6 @@ describe('blog plugin', () => {
title: 'Complex Slug',
description: `complex url slug`,
authors: [],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
prevItem: undefined,
nextItem: {
permalink: '/blog/simple/slug',
Expand Down Expand Up @@ -302,8 +296,6 @@ describe('blog plugin', () => {
imageURL: undefined,
},
],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
prevItem: undefined,
nextItem: {
permalink: '/blog/draft',
Expand Down Expand Up @@ -335,8 +327,6 @@ describe('blog plugin', () => {
title: 'some heading',
description: '',
authors: [],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
date: new Date('2019-01-02'),
formattedDate: 'January 2, 2019',
frontMatter: {
Expand Down Expand Up @@ -503,8 +493,6 @@ describe('blog plugin', () => {
title: 'no date',
description: `no date`,
authors: [],
baseBlogPermalink: '/blog',
baseBlogTitle: 'Blog',
date: noDateSourceTime,
formattedDate,
frontMatter: {},
Expand Down
5 changes: 0 additions & 5 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ async function processBlogSourceFile(
truncateMarker,
showReadingTime,
editUrl,
blogTitle: baseBlogTitle,
} = options;

// Lookup in localized folder in priority
Expand Down Expand Up @@ -330,8 +329,6 @@ async function processBlogSourceFile(
return undefined;
}

const baseBlogPermalink = normalizeUrl([baseUrl, routeBasePath]);

const tagsBasePath = normalizeUrl([
baseUrl,
routeBasePath,
Expand All @@ -343,8 +340,6 @@ async function processBlogSourceFile(
id: slug,
metadata: {
permalink,
baseBlogPermalink,
baseBlogTitle,
editUrl: getBlogEditUrl(),
source: aliasedSource,
title,
Expand Down
16 changes: 16 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default async function pluginContentBlog(
blogArchiveComponent,
routeBasePath,
archiveBasePath,
blogTitle,
} = options;

const {addRoute, createData} = actions;
Expand Down Expand Up @@ -255,6 +256,8 @@ export default async function pluginContentBlog(
),
);

const baseBlogPermalink = normalizeUrl([baseUrl, routeBasePath]);

// Create routes for blog entries.
await Promise.all(
blogPosts.map(async (blogPost) => {
Expand All @@ -266,13 +269,26 @@ export default async function pluginContentBlog(
JSON.stringify(metadata, null, 2),
);

const blogMetadataPath = await createData(
`${docuHash(`${metadata.source}-blogMetadata`)}.json`,
JSON.stringify(
{
baseBlogPermalink,
blogTitle,
},
null,
2,
),
);

johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
addRoute({
path: metadata.permalink,
component: blogPostComponent,
exact: true,
modules: {
sidebar: aliasedSource(sidebarProp),
content: metadata.source,
blogMetadata: aliasedSource(blogMetadataPath),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the
readonly formattedDate: string;
/** Full link including base URL. */
readonly permalink: string;
/** the path to the base of the blog */
readonly baseBlogPermalink: string;
/** title of the overall blog */
readonly baseBlogTitle: string;
/**
* Description used in the meta. Could be an empty string (empty content)
*/
Expand Down Expand Up @@ -465,6 +461,13 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the
blogTagsListPath: string;
};

export type BlogMetadata = {
/** the path to the base of the blog */
baseBlogPermalink: string;
/** title of the overall blog */
blogTitle: string;
};

export type BlogTags = {
[permalink: string]: BlogTag;
};
Expand Down Expand Up @@ -536,6 +539,7 @@ declare module '@theme/BlogPostPage' {
BlogPostFrontMatter,
BlogSidebar,
PropBlogPostContent,
BlogMetadata,
} from '@docusaurus/plugin-content-blog';

export type FrontMatter = BlogPostFrontMatter;
Expand All @@ -547,6 +551,8 @@ declare module '@theme/BlogPostPage' {
readonly sidebar: BlogSidebar;
/** Content of this post as an MDX component, with useful metadata. */
readonly content: Content;
/** Metadata about the blog. */
readonly blogMetadata: BlogMetadata;
}

export default function BlogPostPage(props: Props): JSX.Element;
Expand All @@ -560,6 +566,7 @@ declare module '@theme/BlogPostPage/StructuredData' {
import type {
BlogPostFrontMatter,
PropBlogPostContent,
BlogMetadata,
} from '@docusaurus/plugin-content-blog';

export type FrontMatter = BlogPostFrontMatter;
Expand All @@ -572,6 +579,7 @@ declare module '@theme/BlogPostPage/StructuredData' {
readonly assets: Assets;
readonly frontMatter: FrontMatter;
readonly metadata: Metadata;
readonly blogMetadata: BlogMetadata;
}

export default function BlogPostStructuredData(props: Props): JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export default function BlogPostStructuredData(props: Props): JSX.Element {
...(keywords ? {keywords} : {}),
isPartOf: {
'@type': 'Blog',
'@id': `${siteConfig.url}${metadata.baseBlogPermalink}`,
name: metadata.baseBlogTitle,
'@id': `${siteConfig.url}${props.blogMetadata.baseBlogPermalink}`,
name: props.blogMetadata.blogTitle,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import BlogPostPageStructuredData from '@theme/BlogPostPage/StructuredData';
import TOC from '@theme/TOC';
import type {Props} from '@theme/BlogPostPage';
import Unlisted from '@theme/Unlisted';
import type {BlogSidebar} from '@docusaurus/plugin-content-blog';
import type {BlogMetadata, BlogSidebar} from '@docusaurus/plugin-content-blog';

function BlogPostPageContent({
sidebar,
blogMetadata,
children,
}: {
sidebar: BlogSidebar;
blogMetadata: BlogMetadata;
children: ReactNode;
}): JSX.Element {
const {metadata, toc, assets} = useBlogPost();
Expand Down Expand Up @@ -51,6 +53,7 @@ function BlogPostPageContent({
frontMatter={frontMatter}
assets={assets}
metadata={metadata}
blogMetadata={blogMetadata}
/>

<BlogPostItem>{children}</BlogPostItem>
Expand All @@ -72,7 +75,9 @@ export default function BlogPostPage(props: Props): JSX.Element {
ThemeClassNames.page.blogPostPage,
)}>
<BlogPostPageMetadata />
<BlogPostPageContent sidebar={props.sidebar}>
<BlogPostPageContent
sidebar={props.sidebar}
blogMetadata={props.blogMetadata}>
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
<BlogPostContent />
</BlogPostPageContent>
</HtmlClassNameProvider>
Expand Down
Loading