-
-
Notifications
You must be signed in to change notification settings - Fork 264
feat: add JSON‑LD Organization, BreadcrumbList, and Article schema #737
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,180 @@ | ||
| /** | ||
| * 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 clsx from "clsx"; | ||
| import {ThemeClassNames} from "@docusaurus/theme-common"; | ||
| import {useSidebarBreadcrumbs} from "@docusaurus/plugin-content-docs/client"; | ||
| import {useHomePageRoute} from "@docusaurus/theme-common/internal"; | ||
| import {useLocation} from "@docusaurus/router"; | ||
| import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; | ||
| import Link from "@docusaurus/Link"; | ||
| import {translate} from "@docusaurus/Translate"; | ||
| import Head from "@docusaurus/Head"; | ||
| import HomeBreadcrumbItem from "@theme/DocBreadcrumbs/Items/Home"; | ||
|
|
||
| import styles from "./styles.module.css"; | ||
|
|
||
| function BreadcrumbsItemLink({children, href, isLast}) { | ||
| const className = "breadcrumbs__link"; | ||
| if (isLast) { | ||
| return ( | ||
| <span className={className} itemProp="name"> | ||
| {children} | ||
| </span> | ||
| ); | ||
| } | ||
| return href ? ( | ||
| <Link className={className} href={href} itemProp="item"> | ||
| <span itemProp="name">{children}</span> | ||
| </Link> | ||
| ) : ( | ||
| <span className={className}>{children}</span> | ||
| ); | ||
| } | ||
|
|
||
| function BreadcrumbsItem({children, active, index, addMicrodata}) { | ||
| return ( | ||
| <li | ||
| {...(addMicrodata && { | ||
| itemScope: true, | ||
| itemProp: "itemListElement", | ||
| itemType: "https://schema.org/ListItem", | ||
| })} | ||
| className={clsx("breadcrumbs__item", { | ||
| "breadcrumbs__item--active": active, | ||
| })} | ||
| > | ||
| {children} | ||
| <meta itemProp="position" content={String(index + 1)} /> | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| export default function DocBreadcrumbs() { | ||
| const breadcrumbs = useSidebarBreadcrumbs(); | ||
| const homePageRoute = useHomePageRoute(); | ||
| const {siteConfig} = useDocusaurusContext(); | ||
| const {pathname} = useLocation(); | ||
|
|
||
| if (!breadcrumbs) { | ||
| return null; | ||
| } | ||
|
|
||
| const toAbsoluteUrl = (baseUrl, url) => { | ||
| if (!url) { | ||
| return null; | ||
| } | ||
| if (url.startsWith("http://") || url.startsWith("https://")) { | ||
| return url; | ||
| } | ||
| const trimmedBase = baseUrl?.replace(/\/$/, "") ?? ""; | ||
| const normalizedPath = url.startsWith("/") ? url : `/${url}`; | ||
| return `${trimmedBase}${normalizedPath}`; | ||
| }; | ||
|
|
||
| const breadcrumbItems = []; | ||
| const pushBreadcrumbItem = (item) => { | ||
| if (!item?.item || !item?.name) { | ||
| return; | ||
| } | ||
| if (breadcrumbItems.some((existing) => existing.item === item.item)) { | ||
| return; | ||
| } | ||
| breadcrumbItems.push(item); | ||
| }; | ||
|
|
||
| if (siteConfig?.url) { | ||
| pushBreadcrumbItem({name: "Home", item: siteConfig.url}); | ||
| } | ||
|
|
||
| if (siteConfig?.url && siteConfig?.baseUrl) { | ||
| const docsUrl = toAbsoluteUrl(siteConfig.url, siteConfig.baseUrl); | ||
| if (docsUrl && docsUrl !== siteConfig.url) { | ||
| pushBreadcrumbItem({name: "Docs", item: docsUrl}); | ||
| } | ||
| } | ||
|
|
||
| if (breadcrumbs.length > 0) { | ||
| breadcrumbs.forEach((crumb, index) => { | ||
| const isLast = index === breadcrumbs.length - 1; | ||
| const href = crumb.href || (isLast ? pathname : null); | ||
| const absoluteUrl = toAbsoluteUrl(siteConfig?.url, href); | ||
| if (!absoluteUrl) { | ||
| return; | ||
| } | ||
| pushBreadcrumbItem({ | ||
| name: crumb.label, | ||
| item: absoluteUrl, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const breadcrumbSchema = | ||
| breadcrumbItems.length > 0 | ||
| ? { | ||
| "@context": "https://schema.org", | ||
| "@type": "BreadcrumbList", | ||
| itemListElement: breadcrumbItems.map((item, index) => ({ | ||
| "@type": "ListItem", | ||
| position: index + 1, | ||
| name: item.name, | ||
| item: item.item, | ||
| })), | ||
| } | ||
| : null; | ||
|
|
||
| return ( | ||
| <> | ||
| {breadcrumbSchema && ( | ||
| <Head> | ||
| <script type="application/ld+json"> | ||
| {JSON.stringify(breadcrumbSchema)} | ||
| </script> | ||
| </Head> | ||
| )} | ||
| <nav | ||
| className={clsx( | ||
| ThemeClassNames.docs.docBreadcrumbs, | ||
| styles.breadcrumbsContainer | ||
| )} | ||
| aria-label={translate({ | ||
| id: "theme.docs.breadcrumbs.navAriaLabel", | ||
| message: "Breadcrumbs", | ||
| description: "The ARIA label for the breadcrumbs", | ||
| })} | ||
| > | ||
| <ul | ||
| className="breadcrumbs" | ||
| itemScope | ||
| itemType="https://schema.org/BreadcrumbList" | ||
| > | ||
| {homePageRoute && <HomeBreadcrumbItem />} | ||
| {breadcrumbs.map((item, idx) => { | ||
| const isLast = idx === breadcrumbs.length - 1; | ||
| const href = | ||
| item.type === "category" && item.linkUnlisted | ||
| ? undefined | ||
| : item.href; | ||
| return ( | ||
| <BreadcrumbsItem | ||
| key={idx} | ||
| active={isLast} | ||
| index={idx} | ||
| addMicrodata={!!href} | ||
| > | ||
| <BreadcrumbsItemLink href={href} isLast={isLast}> | ||
| {item.label} | ||
| </BreadcrumbsItemLink> | ||
| </BreadcrumbsItem> | ||
| ); | ||
| })} | ||
| </ul> | ||
| </nav> | ||
| </> | ||
| ); | ||
| } |
This file contains hidden or 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,11 @@ | ||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| .breadcrumbsContainer { | ||
| --ifm-breadcrumb-size-multiplier: 0.8; | ||
| margin-bottom: 0.8rem; | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this section, it would be useful in the future when we add search queries as a url similar to the blog website