@forge42/seo-tools
Version:
Framework agnostic set of helpers designed to help you create, maintain and develop your SEO
1 lines • 1.73 kB
Source Map (JSON)
{"version":3,"sources":["../../src/structured-data/breadcrumb.ts"],"sourcesContent":["/**\n * This helper is used to generate an breadcrumb ld+json structured data object.\n *\n *\n * Find more information about the breadcrumb schema here:\n *\n *\n * https://developers.google.com/search/docs/appearance/structured-data/breadcrumb\n * @param fullUrl string representing the current page (e.g. https://localhost:3000/blog/2021/01/01/article)\n * @param names optional string array representing the names of the breadcrumb items, matched 1:1 with the path\n * @returns BreadcrumbLdJson object to be used in head via json-ld script tag\n */\nexport const breadcrumbs = (fullUrl: string, names?: string[]) => {\n\tconst url = new URL(fullUrl)\n\tconst domain = url.origin\n\tconst path = url.pathname.split(\"/\").filter(Boolean)\n\tconst breadcrumb = path.map((_, index) => {\n\t\tconst pathToOpen = path.slice(0, index + 1).join(\"/\")\n\t\treturn {\n\t\t\t\"@type\": \"ListItem\",\n\t\t\tposition: index + 1,\n\t\t\tname: names?.[index] || _,\n\t\t\titem: `${domain}/${pathToOpen}`,\n\t\t}\n\t})\n\treturn {\n\t\t\"@context\": \"https://schema.org\",\n\t\t\"@type\": \"BreadcrumbList\",\n\t\titemListElement: breadcrumb,\n\t}\n}\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"]}