import React, { FC } from 'react'; import { BreadcrumbList, ListItem, WithContext } from 'schema-dts'; import { DeferSeoProps } from '../types'; import { Overrides } from '../utils/shared-types'; import { JsonLd } from './jsonld'; export interface ItemListElements { /** * The URL to the webpage that represents the breadcrumb. * * @remarks * * There are two ways to specify item: * * URL: Specify the URL of the page. For example: * * @example * "https://example.com/books" */ item: string; /** * The title of the breadcrumb displayed for the user. */ name: string; /** * The position of the breadcrumb in the breadcrumb trail. Position 1 * signifies the beginning of the trail. */ position: number; } export interface BreadcrumbJsonLdProps extends DeferSeoProps, Overrides { /** * An array of breadcrumbs listed in a specific order. Specify each breadcrumb * with a ListItem For example: */ itemListElements: ItemListElements[]; } /** * A breadcrumb trail on a page indicates the page's position in the site * hierarchy. A user can navigate all the way up in the site hierarchy, one * level at a time, by starting from the last breadcrumb in the breadcrumb * trail. * * @remarks * * BreadCrumbJsonLd creates a * {@link https://schema.org/BreadcrumbList | BreadcrumbList} container item * that holds all elements in the list. * * ```jsx * import React from 'react'; * import { BreadcrumbJsonLd } from 'gatsby-plugin-next-seo'; * * export default () => ( * <> *

Breadcrumb JSON-LD

* * * ); * ``` * * */ export const BreadcrumbJsonLd: FC = ({ itemListElements = [], overrides = {}, defer = false, }) => { const json: WithContext = { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: itemListElements.map( ({ position, item, name }) => ({ '@type': 'ListItem', position, item: { '@id': item, name, '@type': 'Thing' }, }), ), ...overrides, }; return ; };