-
-
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.
fix(seo): docs breadcrumb structured data should use JSON-LD and filt…
…er unliked categories (#10888) Co-authored-by: sebastien <lorber.sebastien@gmail.com>
- Loading branch information
1 parent
cd7875b
commit 45065e8
Showing
8 changed files
with
107 additions
and
59 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
32 changes: 32 additions & 0 deletions
32
packages/docusaurus-plugin-content-docs/src/client/structuredDataUtils.ts
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,32 @@ | ||
/** | ||
* 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 useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import type {PropSidebarBreadcrumbsItem} from '@docusaurus/plugin-content-docs'; | ||
import type {WithContext, BreadcrumbList} from 'schema-dts'; | ||
|
||
export function useBreadcrumbsStructuredData({ | ||
breadcrumbs, | ||
}: { | ||
breadcrumbs: PropSidebarBreadcrumbsItem[]; | ||
}): WithContext<BreadcrumbList> { | ||
const {siteConfig} = useDocusaurusContext(); | ||
return { | ||
'@context': 'https://schema.org', | ||
'@type': 'BreadcrumbList', | ||
itemListElement: breadcrumbs | ||
// We filter breadcrumb items without links, they are not allowed | ||
// See also https://github.com/facebook/docusaurus/issues/9319#issuecomment-2643560845 | ||
.filter((breadcrumb) => breadcrumb.href) | ||
.map((breadcrumb, index) => ({ | ||
'@type': 'ListItem', | ||
position: index + 1, | ||
name: breadcrumb.label, | ||
item: `${siteConfig.url}${breadcrumb.href}`, | ||
})), | ||
}; | ||
} |
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
24 changes: 24 additions & 0 deletions
24
packages/docusaurus-theme-classic/src/theme/DocBreadcrumbs/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,24 @@ | ||
/** | ||
* 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, {type ReactNode} from 'react'; | ||
import Head from '@docusaurus/Head'; | ||
import {useBreadcrumbsStructuredData} from '@docusaurus/plugin-content-docs/client'; | ||
import type {Props} from '@theme/DocBreadcrumbs/StructuredData'; | ||
|
||
export default function DocBreadcrumbsStructuredData(props: Props): ReactNode { | ||
const structuredData = useBreadcrumbsStructuredData({ | ||
breadcrumbs: props.breadcrumbs, | ||
}); | ||
return ( | ||
<Head> | ||
<script type="application/ld+json"> | ||
{JSON.stringify(structuredData)} | ||
</script> | ||
</Head> | ||
); | ||
} |
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