diff --git a/apps/website/scripts/check-models-html.ts b/apps/website/scripts/check-models-html.ts index 500afc8fde4..55aa2a85afe 100644 --- a/apps/website/scripts/check-models-html.ts +++ b/apps/website/scripts/check-models-html.ts @@ -1,10 +1,16 @@ import { readFileSync } from 'node:fs' import { join } from 'node:path' +import { isIndexableModelPage } from '@/config/indexing' +import { getRoutes } from '@/config/routes' import { workshopModels } from '@/config/workshop-browse-content' import { getRouterWorkshopModelDetail } from '@/config/workshop-router-content' import { auditExampleGallery } from './models-gallery-audit' -import { auditMediaLabels, auditModelPage } from './models-html-audit' +import { + auditMediaLabels, + auditModelPage, + auditModelsHub +} from './models-html-audit' const DIST = join(process.cwd(), 'dist') @@ -18,7 +24,13 @@ const pagedModels = workshopModels.flatMap(({ href, name, slug }) => href === undefined ? [] : [{ href, name, slug }] ) -const errors = pagedModels.flatMap((model) => { +const hubPath = getRoutes().workshop +const hubErrors = auditModelsHub( + readFileSync(join(DIST, hubPath, 'index.html'), 'utf-8'), + (href) => isIndexableModelPage(href) +).map((error) => `${hubPath}: ${error}`) + +const modelErrors = pagedModels.flatMap((model) => { const html = readFileSync(join(DIST, model.href, 'index.html'), 'utf-8') return [ ...auditModelPage(html, model.name), @@ -26,6 +38,7 @@ const errors = pagedModels.flatMap((model) => { ...auditExampleGallery(html, examplesOf(model.slug).length) ].map((error) => `${model.href}: ${error}`) }) +const errors = [...hubErrors, ...modelErrors] if (errors.length > 0) { console.error(`[models-html] ${errors.length} problem(s):`) @@ -33,5 +46,5 @@ if (errors.length > 0) { process.exit(1) } console.warn( - `[models-html] ${pagedModels.length} model pages carry their model in the HTML.` + `[models-html] ${pagedModels.length} model pages carry their model in the HTML; ${hubPath} lists them in its JSON-LD.` ) diff --git a/apps/website/scripts/models-html-audit.test.ts b/apps/website/scripts/models-html-audit.test.ts index 091ddbaa43a..8b40199465f 100644 --- a/apps/website/scripts/models-html-audit.test.ts +++ b/apps/website/scripts/models-html-audit.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it } from 'vitest' -import { auditMediaLabels, auditModelPage } from './models-html-audit' +import { + auditMediaLabels, + auditModelPage, + auditModelsHub +} from './models-html-audit' const showcase = '

Grok Imagine in ComfyUI

' const related = (cards: string) => @@ -84,3 +88,94 @@ describe(auditMediaLabels, () => { expect(auditMediaLabels(html)).toEqual(errors) }) }) + +describe(auditModelsHub, () => { + const graph = (...nodes: object[]) => + `` + const itemList = (...paths: string[]) => ({ + '@type': 'ItemList', + numberOfItems: paths.length, + itemListElement: paths.map((path, index) => ({ + '@type': 'ListItem', + position: index + 1, + url: `https://comfy.org${path}` + })) + }) + const directory = (...paths: string[]) => + `
` + const collection = { '@type': 'CollectionPage' } + const breadcrumbs = { '@type': 'BreadcrumbList' } + const everyLink = () => true + + it.for([ + { + name: 'a collection listing every directory link in order', + html: + graph(collection, breadcrumbs, itemList('/a/', '/b/')) + + directory('/a/', '/b/'), + errors: [] + }, + { + name: 'a page that lost its collection markup', + html: graph({ '@type': 'WebPage' }, breadcrumbs) + directory('/a/'), + errors: [ + 'expected 1 CollectionPage, found 0', + 'expected 1 ItemList, found 0' + ] + }, + { + name: 'a list that counts fewer models than the directory links', + html: + graph(collection, breadcrumbs, itemList('/a/')) + + directory('/a/', '/b/'), + errors: [ + 'ItemList numberOfItems 1 does not match 2 directory links', + 'ItemList URLs differ from the directory links or their order' + ] + }, + { + name: 'a list in a different order from the directory', + html: + graph(collection, breadcrumbs, itemList('/b/', '/a/')) + + directory('/a/', '/b/'), + errors: ['ItemList URLs differ from the directory links or their order'] + }, + { + name: 'a list on another origin with the same paths', + html: + graph(collection, breadcrumbs, { + ...itemList('/a/'), + itemListElement: [ + { + '@type': 'ListItem', + position: 1, + url: 'https://other.example/a/' + } + ] + }) + directory('/a/'), + errors: ['ItemList URLs differ from the directory links or their order'] + }, + { + name: 'an empty directory', + html: graph(collection, breadcrumbs, itemList('/a/')), + errors: [ + 'expected 0 ItemList, found 1', + 'renders no model links in the directory', + 'ItemList numberOfItems 1 does not match 0 directory links', + 'ItemList URLs differ from the directory links or their order' + ] + } + ])('$name', ({ html, errors }) => { + expect(auditModelsHub(html, everyLink)).toEqual(errors) + }) + + it('expects only the directory links the launch keeps', () => { + expect( + auditModelsHub( + graph(collection, breadcrumbs, itemList('/a/')) + + directory('/a/', '/b/'), + (href) => href === '/a/' + ) + ).toEqual([]) + }) +}) diff --git a/apps/website/scripts/models-html-audit.ts b/apps/website/scripts/models-html-audit.ts index ceee30dbfa5..d8b0b00810c 100644 --- a/apps/website/scripts/models-html-audit.ts +++ b/apps/website/scripts/models-html-audit.ts @@ -65,3 +65,61 @@ export function auditMediaLabels(html: string): string[] { }) return [...images, ...videos] } + +const SITE_ORIGIN = 'https://comfy.org' +const JSON_LD_BLOCK = + /]*type="application\/ld\+json"[^>]*>([\s\S]*?)<\/script>/g + +interface JsonLdNode { + '@type'?: string + numberOfItems?: number + itemListElement?: { url: string }[] +} + +function jsonLdNodes(html: string): JsonLdNode[] { + return [...html.matchAll(JSON_LD_BLOCK)].flatMap(([, json]) => { + const parsed = JSON.parse(json) + return parsed['@graph'] ?? [parsed] + }) +} + +function directoryHrefs(html: string): string[] { + const directory = + html.split('data-testid="models-directory"')[1]?.split('')[0] ?? + '' + return [...directory.matchAll(/]*\shref="([^"]*)"/g)].map( + ([, href]) => href + ) +} + +export function auditModelsHub( + html: string, + isListed: (href: string) => boolean +): string[] { + const nodes = jsonLdNodes(html) + const ofType = (type: string) => + nodes.filter((node) => node['@type'] === type) + const links = directoryHrefs(html) + const expected = links.filter(isListed) + const expectedCounts = { + CollectionPage: 1, + BreadcrumbList: 1, + ItemList: expected.length > 0 ? 1 : 0 + } + const errors = Object.entries(expectedCounts).flatMap(([type, count]) => { + const found = ofType(type).length + return found === count ? [] : [`expected ${count} ${type}, found ${found}`] + }) + if (links.length === 0) errors.push('renders no model links in the directory') + const itemList = ofType('ItemList').at(0) + if (!itemList) return errors + const listed = (itemList.itemListElement ?? []).map(({ url }) => url) + const expectedUrls = expected.map((href) => new URL(href, SITE_ORIGIN).href) + if (itemList.numberOfItems !== expected.length) + errors.push( + `ItemList numberOfItems ${itemList.numberOfItems} does not match ${expected.length} directory links` + ) + if (listed.join() !== expectedUrls.join()) + errors.push('ItemList URLs differ from the directory links or their order') + return errors +} diff --git a/apps/website/src/routes/models/index.astro b/apps/website/src/routes/models/index.astro index d43c1a2ca44..bc2e39b7d8b 100644 --- a/apps/website/src/routes/models/index.astro +++ b/apps/website/src/routes/models/index.astro @@ -3,6 +3,8 @@ import BaseLayout from '@/layouts/BaseLayout.astro' import { workshopModels } from '@/config/workshop-browse-content' import ModelsPage from '@/components/workshop/ModelsPage.vue' import ModelsShowcase from '@/components/models/ModelsShowcase.astro' +import { pageContext } from '@/utils/jsonLd' +import { modelsHubJsonLd } from '@/utils/modelJsonLd' import { modelsHubMeta } from './models-hub-meta' import { modelsByProvider } from './models-directory' @@ -11,9 +13,27 @@ const directory = modelsByProvider( workshopModels, Astro.locals.t('workshop.filter.other') ) +const { siteUrl, url } = pageContext( + Astro.site, + Astro.url.pathname, + Astro.currentLocale +) +const jsonLd = modelsHubJsonLd({ + models: directory.flatMap((group) => group.models), + url, + siteUrl +}) --- - + diff --git a/apps/website/src/utils/modelJsonLd.test.ts b/apps/website/src/utils/modelJsonLd.test.ts index 5e6549f43f2..ff87b12c039 100644 --- a/apps/website/src/utils/modelJsonLd.test.ts +++ b/apps/website/src/utils/modelJsonLd.test.ts @@ -1,12 +1,27 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, onTestFinished, vi } from 'vitest' +import type { ModelPageLaunch } from '@/config/model-page-launch' import { MODEL_DEVELOPERS, modelDeveloper } from '@/config/model-vendors' +import { modelsUrlKind, modelsUrlPaths } from '@/config/models-url-registry' +import { workshopModels } from '@/config/workshop-browse-content' import { getWorkshopPageDetail, workshopPagePaths } from '@/config/workshop-page-content' import type { JsonLdNode } from './jsonLd' -import { modelPageJsonLd } from './modelJsonLd' +import { modelPageJsonLd, modelsHubJsonLd } from './modelJsonLd' +import { modelsByProvider } from '@/routes/models/models-directory' + +const launch = vi.hoisted( + (): { + launchedModelPages: ModelPageLaunch + launchedWorkflowPages: boolean + } => ({ + launchedModelPages: 'all', + launchedWorkflowPages: false + }) +) +vi.mock(import('@/config/model-page-launch'), () => launch) const siteUrl = 'https://comfy.org' const url = 'https://comfy.org/hub/models/example/' @@ -137,3 +152,110 @@ describe('MODEL_DEVELOPERS', () => { if (developer?.wikidata) expect(developer.wikidata).toMatch(/^Q\d+$/) }) }) + +describe('modelsHubJsonLd', () => { + const hubUrl = 'https://comfy.org/hub/models/' + const directoryModels = modelsByProvider(workshopModels, 'Other').flatMap( + (group) => group.models + ) + const directoryUrls = directoryModels.flatMap(({ href }) => + href === undefined ? [] : [`${siteUrl}${href}`] + ) + const [workflowPath] = modelsUrlPaths('workflow') + + function listedUrls( + models: Parameters[0]['models'], + launched: ModelPageLaunch = 'all' + ) { + return modelsHubJsonLd({ + models, + url: hubUrl, + siteUrl, + launched + }).extraJsonLd.flatMap(({ itemListElement }) => + Array.isArray(itemListElement) + ? itemListElement.map((element: { url: string }) => element.url) + : [] + ) + } + + it('lists every directory link, in the order the directory shows them', () => { + const { pageType, mainEntityId, extraJsonLd } = modelsHubJsonLd({ + models: directoryModels, + url: hubUrl, + siteUrl, + launched: 'all' + }) + expect(directoryUrls.length).toBeGreaterThan(0) + expect(pageType).toBe('CollectionPage') + expect(mainEntityId).toBe(`${hubUrl}#itemlist`) + expect(extraJsonLd[0]).toMatchObject({ + '@type': 'ItemList', + numberOfItems: directoryUrls.length + }) + expect(listedUrls(directoryModels)).toEqual(directoryUrls) + }) + + it('lists only absolute, unique model page URLs from the registry', () => { + const urls = listedUrls(directoryModels) + expect(new Set(urls).size).toBe(urls.length) + expect( + urls.map((listed) => { + const { origin, pathname } = new URL(listed) + return { + origin, + trailingSlash: pathname.endsWith('/'), + kind: modelsUrlKind(pathname) + } + }) + ).toEqual( + urls.map(() => ({ origin: siteUrl, trailingSlash: true, kind: 'model' })) + ) + }) + + it('leaves out a launched workflow page and links that are not model pages', () => { + const [listedModel] = directoryModels + expect(workflowPath).toBeDefined() + launch.launchedWorkflowPages = true + onTestFinished(() => { + launch.launchedWorkflowPages = false + }) + expect( + listedUrls([ + { name: 'No page' }, + { name: 'Workflow', href: workflowPath }, + { name: 'Unknown', href: '/hub/models/not-a-model/' }, + listedModel + ]) + ).toEqual([`${siteUrl}${listedModel.href}`]) + }) + + it('lists only the model pages the launch keeps', () => { + const [kept] = directoryModels + const dropped = directoryModels + .filter(({ routerId }) => routerId !== kept.routerId) + .slice(0, 1) + expect(dropped).toHaveLength(1) + expect(listedUrls([kept, ...dropped], new Set([kept.routerId]))).toEqual([ + `${siteUrl}${kept.href}` + ]) + }) + + it('resolves a link without a trailing slash to the canonical page URL', () => { + const [model] = directoryModels + expect( + listedUrls([{ name: model.name, href: model.href?.replace(/\/$/, '') }]) + ).toEqual([`${siteUrl}${model.href}`]) + }) + + it('keeps the Home > Models breadcrumb but drops the list when nothing is listed', () => { + expect(modelsHubJsonLd({ models: [], url: hubUrl, siteUrl })).toEqual({ + pageType: 'CollectionPage', + breadcrumbs: [ + { name: 'Home', url: 'https://comfy.org/' }, + { name: 'Models' } + ], + extraJsonLd: [] + }) + }) +}) diff --git a/apps/website/src/utils/modelJsonLd.ts b/apps/website/src/utils/modelJsonLd.ts index 2a9f46c51ee..15e1bf703af 100644 --- a/apps/website/src/utils/modelJsonLd.ts +++ b/apps/website/src/utils/modelJsonLd.ts @@ -1,8 +1,18 @@ +import { isIndexableModelPage } from '@/config/indexing' +import { launchedModelPages } from '@/config/model-page-launch' +import type { ModelPageLaunch } from '@/config/model-page-launch' import type { ModelDeveloper } from '@/config/model-vendors' +import { modelsUrlKind } from '@/config/models-url-registry' import { getRoutes } from '@/config/routes' import { t } from '@/i18n/translations' import type { Crumb, JsonLdNode } from './jsonLd' -import { jsonLdId, organizationId, softwareApplicationNode } from './jsonLd' +import { + absoluteUrl, + itemListNode, + jsonLdId, + organizationId, + softwareApplicationNode +} from './jsonLd' interface ModelPageJsonLdInput { model: { @@ -91,3 +101,44 @@ export function modelPageJsonLd({ ] } } + +interface ModelsHubJsonLdInput { + /** The directory's models, in the order the page lists them. */ + models: readonly { name: string; href?: string }[] + url: string + siteUrl: string + launched?: ModelPageLaunch +} + +export function modelsHubJsonLd({ + models, + url, + siteUrl, + launched = launchedModelPages +}: ModelsHubJsonLdInput): { + pageType: 'CollectionPage' + mainEntityId?: string + breadcrumbs: Crumb[] + extraJsonLd: JsonLdNode[] +} { + const site = new URL(siteUrl) + const listed = models.flatMap(({ name, href }) => + href !== undefined && + modelsUrlKind(href) === 'model' && + isIndexableModelPage(href, launched) + ? [{ name, url: absoluteUrl(site, href) }] + : [] + ) + const breadcrumbs = [ + { name: t('breadcrumb.home'), url: `${siteUrl}/` }, + { name: t('workshop.title') } + ] + if (listed.length === 0) + return { pageType: 'CollectionPage', breadcrumbs, extraJsonLd: [] } + return { + pageType: 'CollectionPage', + mainEntityId: jsonLdId(url, 'itemlist'), + breadcrumbs, + extraJsonLd: [itemListNode(url, t('workshop.title'), listed)] + } +}