Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 9 additions & 7 deletions src/lib/components/BreadcrumbJsonLd.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -28,7 +30,7 @@

<svelte:head>
{#if jsonLd}
<!-- eslint-disable-next-line svelte/no-at-html-tags -- static JSON-LD, no user input -->
<!-- trunk-ignore(eslint/svelte/no-at-html-tags): static JSON-LD, no user input -->

Check notice on line 33 in src/lib/components/BreadcrumbJsonLd.svelte

View workflow job for this annotation

GitHub Actions / Trunk Check

trunk(ignore-disabled-linter)

[new] 'eslint/svelte/no-at-html-tags' is a disabled linter
{@html jsonLd}
{/if}
</svelte:head>
100 changes: 100 additions & 0 deletions src/lib/components/breadcrumb-json-ld.test.mjs
Original file line number Diff line number Diff line change
@@ -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(/<script type="application\/ld\+json">(.*?)<\/script>/gs)]
if (!breadcrumbs?.length) {
assert.equal(scripts.length, 0)
assert.doesNotMatch(head, /BreadcrumbList/)
return
}
assert.equal(scripts.length, 1)
const schema = JSON.parse(scripts[0][1])
assert.equal(schema['@context'], 'https://schema.org')
assert.equal(schema['@type'], 'BreadcrumbList')
assert.deepEqual(
schema.itemListElement.map((item) => item.position),
schema.itemListElement.map((_, index) => index + 1)
)
return schema.itemListElement
}

test('omits an unlinked grouping ancestor and retains the final current page', () => {
assert.deepEqual(
renderTrail([{ title: 'Docs', href: '/docs' }, { title: 'Renderers' }, { title: 'HTML' }]),
[home, docs, { '@type': 'ListItem', position: 3, name: 'HTML' }]
)
})

test('omits multiple unlinked ancestors, including an empty href', () => {
assert.deepEqual(
renderTrail([
{ title: 'Docs', href: '/docs' },
{ title: 'Advanced' },
{ title: 'Renderers', href: '' },
{ title: 'HTML' }
]),
[home, docs, { '@type': 'ListItem', position: 3, name: 'HTML' }]
)
})

test('preserves a linked final crumb', () => {
assert.deepEqual(renderTrail([{ title: 'Docs', href: '/docs' }]), [home, docs])
})

test('preserves an already valid trail with an unlinked final crumb', () => {
assert.deepEqual(
renderTrail([{ title: 'Docs', href: '/docs' }, { title: 'Getting Started' }]),
[home, docs, { '@type': 'ListItem', position: 3, name: 'Getting Started' }]
)
})

test('retains a sole unlinked current page after Home', () => {
assert.deepEqual(renderTrail([{ title: 'Docs' }]), [
home,
{ '@type': 'ListItem', position: 2, name: 'Docs' }
])
})

test('emits no BreadcrumbList for empty or absent context', () => {
renderTrail([])
renderTrail(undefined)
})
17 changes: 17 additions & 0 deletions src/lib/components/fixtures/BreadcrumbJsonLd.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
import { untrack } from 'svelte'
import type { DocsKitConfig } from '../../config.js'
import { type BreadcrumbContext, setBreadcrumbContext } from '../../contexts/breadcrumb.js'
import BreadcrumbJsonLd from '../BreadcrumbJsonLd.svelte'

const { context, config } = $props<{
context?: BreadcrumbContext
config: DocsKitConfig
}>()

untrack(() => {
if (context) setBreadcrumbContext(context)
})
</script>

<BreadcrumbJsonLd {config} />
Loading