diff --git a/apps/website/src/config/workshop-workflow-content.test.ts b/apps/website/src/config/workshop-workflow-content.test.ts index 732e9964555..f5922936a00 100644 --- a/apps/website/src/config/workshop-workflow-content.test.ts +++ b/apps/website/src/config/workshop-workflow-content.test.ts @@ -6,6 +6,7 @@ import { assert, describe, expect, it } from 'vitest' import displayJson from '../content/workshop-display.json' import categories from '../content/workshop-workflow-categories.json' import { workshopDisplayEntriesSchema } from '../content/workshop-display.schema' +import { modelTitle } from '../lib/workshop/model-title' import { workshopExecutionId } from './models-catalogue' import { initialWorkshopPageState } from './workshop-page-state' import { urlUploadField, validateForm } from './workshop-playground' @@ -264,3 +265,10 @@ describe('curated workflow pages', () => { } ) }) + +describe('workshop pages', () => { + it('gives every model and workflow page its own title', () => { + const titles = workshopPages.map((model) => modelTitle(model)) + expect(new Set(titles).size).toBe(titles.length) + }) +}) diff --git a/apps/website/src/i18n/translations.ts b/apps/website/src/i18n/translations.ts index 7e53b6fac9d..ecf788b7333 100644 --- a/apps/website/src/i18n/translations.ts +++ b/apps/website/src/i18n/translations.ts @@ -9755,6 +9755,18 @@ Enterprise` 'workshop.model.tabs.playground': { en: 'Playground', 'zh-CN': 'Playground' }, 'workshop.model.tabs.api': { en: 'API', 'zh-CN': 'API' }, 'workshop.model.tabs.details': { en: 'Details', 'zh-CN': '详情' }, + 'workshop.model.meta.title': { + en: '{name} API & Playground - Comfy', + 'zh-CN': '{name} API 与 Playground - Comfy' + }, + 'workshop.model.meta.titleUnbranded': { + en: '{name} API & Playground', + 'zh-CN': '{name} API 与 Playground' + }, + 'workshop.model.meta.titleApi': { + en: '{name} API - Comfy', + 'zh-CN': '{name} API - Comfy' + }, 'workshop.model.relatedProvider': { en: 'More from {provider}', 'zh-CN': '{provider} 的更多模型' diff --git a/apps/website/src/lib/workshop/model-title.test.ts b/apps/website/src/lib/workshop/model-title.test.ts new file mode 100644 index 00000000000..0048a2680cd --- /dev/null +++ b/apps/website/src/lib/workshop/model-title.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, it } from 'vitest' + +import type { Locale } from '../../i18n/translations' +import { t } from '../../i18n/translations' +import { modelTitle } from './model-title' + +const nameOfLength = (length: number) => 'x'.repeat(length) +const full = (name: string, locale: Locale = 'en') => + t('workshop.model.meta.title', locale, { name }) +const unbranded = (name: string) => + t('workshop.model.meta.titleUnbranded', 'en', { name }) +const apiOnly = (name: string) => + t('workshop.model.meta.titleApi', 'en', { name }) + +describe('modelTitle', () => { + it.for([ + { + name: 'names the API and playground when they fit', + modelName: 'Seedream 4.0', + expected: full('Seedream 4.0') + }, + { + name: 'keeps the full title at exactly the limit', + modelName: nameOfLength(35), + expected: full(nameOfLength(35)) + }, + { + name: 'drops the brand before the playground', + modelName: nameOfLength(36), + expected: unbranded(nameOfLength(36)) + }, + { + name: 'keeps the unbranded title at exactly the limit', + modelName: nameOfLength(43), + expected: unbranded(nameOfLength(43)) + }, + { + name: 'falls back to the API title when the playground no longer fits', + modelName: nameOfLength(44), + expected: apiOnly(nameOfLength(44)) + }, + { + name: 'keeps the whole name in the API title at exactly the limit', + modelName: nameOfLength(48), + expected: apiOnly(nameOfLength(48)) + }, + { + name: 'shortens a longer name at a word boundary', + modelName: + 'Seed Audio 1.0 Multilingual Text-to-Speech with Voice Cloning Preview', + expected: apiOnly('Seed Audio 1.0 Multilingual Text-to-Speech with…') + }, + { + name: 'cuts a long name with no spaces to fit', + modelName: nameOfLength(70), + expected: apiOnly(`${nameOfLength(47)}…`) + }, + { + name: 'writes the Chinese title with the Chinese conjunction', + locale: 'zh-CN' as const, + modelName: 'Seedream 4.0', + expected: full('Seedream 4.0', 'zh-CN') + } + ])('$name', ({ modelName, locale, expected }) => { + const title = modelTitle({ name: modelName }, locale) + expect(title).toBe(expected) + expect(title.length).toBeLessThanOrEqual(60) + }) +}) diff --git a/apps/website/src/lib/workshop/model-title.ts b/apps/website/src/lib/workshop/model-title.ts new file mode 100644 index 00000000000..884a71853c5 --- /dev/null +++ b/apps/website/src/lib/workshop/model-title.ts @@ -0,0 +1,24 @@ +import type { Locale } from '../../i18n/translations' +import { t } from '../../i18n/translations' + +const TITLE_MAX = 60 +const ELLIPSIS = '…' + +function shortenAtWord(text: string, max: number) { + if (text.length <= max) return text + const limit = max - ELLIPSIS.length + const wordEnd = text.lastIndexOf(' ', limit) + return `${text.slice(0, wordEnd > 0 ? wordEnd : limit)}${ELLIPSIS}` +} + +export function modelTitle(model: { name: string }, locale: Locale = 'en') { + const fitting = ( + ['workshop.model.meta.title', 'workshop.model.meta.titleUnbranded'] as const + ) + .map((key) => t(key, locale, { name: model.name })) + .find((title) => title.length <= TITLE_MAX) + if (fitting) return fitting + const apiKey = 'workshop.model.meta.titleApi' + const nameBudget = TITLE_MAX - t(apiKey, locale, { name: '' }).length + return t(apiKey, locale, { name: shortenAtWord(model.name, nameBudget) }) +} diff --git a/apps/website/src/routes/models/[slug].astro b/apps/website/src/routes/models/[slug].astro index c54fcaefeba..714bae8c7f6 100644 --- a/apps/website/src/routes/models/[slug].astro +++ b/apps/website/src/routes/models/[slug].astro @@ -5,6 +5,7 @@ import ModelsPage from '../../components/workshop/ModelsPage.vue' import ModelsShowcase from '../../components/models/ModelsShowcase.astro' import { workshopPagePaths } from '../../config/workshop-page-content' import { t } from '../../i18n/translations' +import { modelTitle } from '../../lib/workshop/model-title' import { prepareModelPage } from './model-page' export const getStaticPaths: GetStaticPaths = () => @@ -19,7 +20,7 @@ const { model } = page ---