From 2e6c93b8672a2f670c058efe122a2f245a5c3d3b Mon Sep 17 00:00:00 2001 From: Jason Kummerl Date: Tue, 22 Sep 2026 06:27:26 -0400 Subject: [PATCH] fix: omit non-navigable ancestors from breadcrumb schema --- README.md | 21 ++++ package.json | 2 +- src/lib/components/BreadcrumbJsonLd.svelte | 16 +-- .../components/breadcrumb-json-ld.test.mjs | 100 ++++++++++++++++++ .../fixtures/BreadcrumbJsonLd.test.svelte | 17 +++ 5 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 README.md create mode 100644 src/lib/components/breadcrumb-json-ld.test.mjs create mode 100644 src/lib/components/fixtures/BreadcrumbJsonLd.test.svelte diff --git a/README.md b/README.md new file mode 100644 index 0000000..8961b1d --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# @humanspeak/docs-kit + +Shared documentation infrastructure for Humanspeak Svelte libraries. + +## Breadcrumb structured data + +`BreadcrumbJsonLd` serializes the breadcrumb context into a `BreadcrumbList`, +prepending Home. Intermediate breadcrumbs without an href (including an empty +href) are UI-only grouping labels and are omitted from structured data. The final +breadcrumb represents the current page and is retained even without an href. +Positions are contiguous after filtering, and empty or absent context emits no +breadcrumb list. + +For example, Docs (`/docs`) → Renderers (unlinked) → HTML (unlinked) produces +Home → Docs → HTML in JSON-LD. The original context is unchanged, so visible +navigation can still show Renderers. Linked breadcrumbs keep their existing URL +construction: the canonical `config.url` followed by the breadcrumb href. Supply +a base URL without a trailing slash and site-relative hrefs beginning with `/`. + +Run `pnpm test` to build the package and run the Node tests, including SSR tests +that compile the actual breadcrumb component and render it with a context fixture. diff --git a/package.json b/package.json index ae43390..8bdb37c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "svelte-kit sync && svelte-package", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", - "test": "pnpm build && node --test src/lib/vite/*.test.mjs", + "test": "pnpm build && node --test src/lib/vite/*.test.mjs src/lib/components/*.test.mjs", "format": "prettier --write .", "lint": "prettier --check . && eslint .", "lint:fix": "npm run format && eslint . --fix", diff --git a/src/lib/components/BreadcrumbJsonLd.svelte b/src/lib/components/BreadcrumbJsonLd.svelte index 6a8a428..84c02a7 100644 --- a/src/lib/components/BreadcrumbJsonLd.svelte +++ b/src/lib/components/BreadcrumbJsonLd.svelte @@ -11,12 +11,14 @@ if (items.length === 0) return '' const listItems = [ { '@type': 'ListItem', position: 1, name: 'Home', item: `${config.url}/` }, - ...items.map((b, i) => ({ - '@type': 'ListItem', - position: i + 2, - name: b.title, - ...(b.href ? { item: `${config.url}${b.href}` } : {}) - })) + ...items + .filter((b, i) => b.href || i === items.length - 1) + .map((b, i) => ({ + '@type': 'ListItem', + position: i + 2, + name: b.title, + ...(b.href ? { item: `${config.url}${b.href}` } : {}) + })) ] return `<${'script'} type="application/ld+json">${JSON.stringify({ '@context': 'https://schema.org', @@ -28,7 +30,7 @@ {#if jsonLd} - + {@html jsonLd} {/if} diff --git a/src/lib/components/breadcrumb-json-ld.test.mjs b/src/lib/components/breadcrumb-json-ld.test.mjs new file mode 100644 index 0000000..0ca2046 --- /dev/null +++ b/src/lib/components/breadcrumb-json-ld.test.mjs @@ -0,0 +1,100 @@ +import assert from 'node:assert/strict' +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { after, test } from 'node:test' +import { compile } from 'svelte/compiler' +import { render } from 'svelte/server' + +// Keep generated modules beneath the package so Svelte resolves normally. +// Both components use the same existing context module, including on the baseline. +const output = await mkdtemp(new URL('../../../.svelte-kit/breadcrumb-test-', import.meta.url)) +after(() => rm(output, { recursive: true, force: true })) +const contextUrl = new URL('../../../dist/contexts/breadcrumb.js', import.meta.url).href + +async function compileComponent(relativePath, filename, replacements) { + const source = await readFile(new URL(relativePath, import.meta.url), 'utf8') + let { code } = compile(source, { filename: relativePath, generate: 'server' }).js + for (const [from, to] of replacements) code = code.replaceAll(from, to) + await writeFile(`${output}/${filename}`, code) +} + +await compileComponent('./BreadcrumbJsonLd.svelte', 'BreadcrumbJsonLd.mjs', [ + ['../contexts/breadcrumb.js', contextUrl] +]) +await compileComponent('./fixtures/BreadcrumbJsonLd.test.svelte', 'Fixture.mjs', [ + ['../../contexts/breadcrumb.js', contextUrl], + ['../BreadcrumbJsonLd.svelte', './BreadcrumbJsonLd.mjs'] +]) +const { default: Fixture } = await import(`${output}/Fixture.mjs`) + +const config = { url: 'https://example.test' } +const home = { '@type': 'ListItem', position: 1, name: 'Home', item: `${config.url}/` } +const docs = { '@type': 'ListItem', position: 2, name: 'Docs', item: `${config.url}/docs` } + +function renderTrail(breadcrumbs) { + const context = breadcrumbs === undefined ? undefined : { breadcrumbs } + const before = structuredClone(context) + if (context) { + context.breadcrumbs.forEach(Object.freeze) + Object.freeze(context.breadcrumbs) + Object.freeze(context) + } + const { head } = render(Fixture, { props: { config, context } }) + assert.deepEqual(context, before, 'serialization must preserve the UI context') + const scripts = [...head.matchAll(/ + +