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
19 changes: 16 additions & 3 deletions apps/website/scripts/check-models-html.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -18,20 +24,27 @@ 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),
...auditMediaLabels(html),
...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):`)
for (const error of errors) console.error(` ${error}`)
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.`
)
97 changes: 96 additions & 1 deletion apps/website/scripts/models-html-audit.test.ts
Original file line number Diff line number Diff line change
@@ -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 = '<h1>Grok Imagine in <span>ComfyUI</span></h1>'
const related = (cards: string) =>
Expand Down Expand Up @@ -84,3 +88,94 @@ describe(auditMediaLabels, () => {
expect(auditMediaLabels(html)).toEqual(errors)
})
})

describe(auditModelsHub, () => {
const graph = (...nodes: object[]) =>
`<script type="application/ld+json">${JSON.stringify({ '@context': 'https://schema.org', '@graph': nodes })}</script>`
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[]) =>
`<section data-testid="models-directory"><ul>${paths.map((path) => `<li><a href="${path}" class="link">Model</a></li>`).join('')}</ul></section><footer><a href="/about/">About</a></footer>`
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([])
})
})
58 changes: 58 additions & 0 deletions apps/website/scripts/models-html-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,61 @@ export function auditMediaLabels(html: string): string[] {
})
return [...images, ...videos]
}

const SITE_ORIGIN = 'https://comfy.org'
const JSON_LD_BLOCK =
/<script[^>]*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('</section>')[0] ??
''
return [...directory.matchAll(/<a\b[^>]*\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
}
22 changes: 21 additions & 1 deletion apps/website/src/routes/models/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
})
---

<BaseLayout {title} {description} preloadFonts={['Medium']}>
<BaseLayout
{title}
{description}
preloadFonts={['Medium']}
pageType={jsonLd.pageType}
Comment thread
MaanilVerma marked this conversation as resolved.
Resolved
breadcrumbs={jsonLd.breadcrumbs}
mainEntityId={jsonLd.mainEntityId}
extraJsonLd={jsonLd.extraJsonLd}
>
<script>
import '@/scripts/hubNavigation'
</script>
Expand Down
126 changes: 124 additions & 2 deletions apps/website/src/utils/modelJsonLd.test.ts
Original file line number Diff line number Diff line change
@@ -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/'
Expand Down Expand Up @@ -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}`]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
)
const [workflowPath] = modelsUrlPaths('workflow')

function listedUrls(
models: Parameters<typeof modelsHubJsonLd>[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)
Comment thread
MaanilVerma marked this conversation as resolved.
Resolved
})

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: []
})
})
})
Loading
Loading