UNPKG

@forge42/seo-tools

Version:

Framework agnostic set of helpers designed to help you create, maintain and develop your SEO

1 lines 1.79 kB
{"version":3,"sources":["../../src/structured-data/breadcrumb.ts"],"sourcesContent":["/**\r\n * This helper is used to generate an breadcrumb ld+json structured data object.\r\n *\r\n *\r\n * Find more information about the breadcrumb schema here:\r\n *\r\n *\r\n * https://developers.google.com/search/docs/appearance/structured-data/breadcrumb\r\n * @param fullUrl string representing the current page (e.g. https://localhost:3000/blog/2021/01/01/article)\r\n * @param names optional string array representing the names of the breadcrumb items, matched 1:1 with the path\r\n * @returns BreadcrumbLdJson object to be used in head via json-ld script tag\r\n */\r\nexport const breadcrumbs = (fullUrl: string, names?: string[]) => {\r\n\tconst url = new URL(fullUrl)\r\n\tconst domain = url.origin\r\n\tconst path = url.pathname.split(\"/\").filter(Boolean)\r\n\tconst breadcrumb = path.map((_, index) => {\r\n\t\tconst pathToOpen = path.slice(0, index + 1).join(\"/\")\r\n\t\treturn {\r\n\t\t\t\"@type\": \"ListItem\",\r\n\t\t\tposition: index + 1,\r\n\t\t\tname: names?.[index] || _,\r\n\t\t\titem: `${domain}/${pathToOpen}`,\r\n\t\t}\r\n\t})\r\n\treturn {\r\n\t\t\"@context\": \"https://schema.org\",\r\n\t\t\"@type\": \"BreadcrumbList\",\r\n\t\titemListElement: breadcrumb,\r\n\t}\r\n}\r\n"],"mappings":"AAYO,IAAMA,EAAc,CAACC,EAAiBC,IAAqB,CACjE,IAAMC,EAAM,IAAI,IAAIF,CAAO,EACrBG,EAASD,EAAI,OACbE,EAAOF,EAAI,SAAS,MAAM,GAAG,EAAE,OAAO,OAAO,EAUnD,MAAO,CACN,WAAY,qBACZ,QAAS,iBACT,gBAZkBE,EAAK,IAAI,CAACC,EAAGC,IAAU,CACzC,IAAMC,EAAaH,EAAK,MAAM,EAAGE,EAAQ,CAAC,EAAE,KAAK,GAAG,EACpD,MAAO,CACN,QAAS,WACT,SAAUA,EAAQ,EAClB,KAAML,IAAQK,CAAK,GAAKD,EACxB,KAAM,GAAGF,CAAM,IAAII,CAAU,EAC9B,CACD,CAAC,CAKD,CACD","names":["breadcrumbs","fullUrl","names","url","domain","path","_","index","pathToOpen"]}