Skip to content
Closed
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
1 change: 1 addition & 0 deletions apps/website/src/components/workshop/WorkshopLoading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { label, minh = true } = defineProps<{
)
"
data-testid="workshop-loading"
data-twin-omit
>
<Loader2
class="size-8 text-primary-comfy-yellow motion-safe:animate-spin"
Expand Down
37 changes: 32 additions & 5 deletions apps/website/src/integrations/markdown-twins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface TwinReport {
written: string[]
/** A twin already existed at this path (hand-written by a page endpoint). */
existing: string[]
/** No HTML to twin, or the page is deliberately excluded from the sitemap. */
/** No HTML or no content to twin, or the page is excluded from the sitemap. */
skipped: string[]
}

Expand All @@ -55,7 +55,7 @@ async function exists(path: string): Promise<boolean> {
}
}

async function readBuiltPage(
async function builtPagePath(
root: string,
pathname: string
): Promise<string | undefined> {
Expand All @@ -65,11 +65,32 @@ async function readBuiltPage(
join(root, `${trimmed || 'index'}.html`)
]
for (const candidate of candidates) {
if (await exists(candidate)) return readFile(candidate, 'utf8')
if (await exists(candidate)) return candidate
}
return undefined
}

async function readBuiltPage(
root: string,
pathname: string
): Promise<string | undefined> {
const path = await builtPagePath(root, pathname)
return path === undefined ? undefined : readFile(path, 'utf8')
}

const TWIN_LINK = /<link\b[^>]*\btype="text\/markdown"[^>]*>/

async function unlinkTwin(root: string, pathname: string): Promise<void> {
const path = await builtPagePath(root, pathname)
if (path === undefined) return
const html = await readFile(path, 'utf8')
const unlinked = html.replace(TWIN_LINK, '')
if (unlinked.includes('type="text/markdown"')) {
throw new Error(`${path} kept a markdown twin link TWIN_LINK did not match`)
}
if (unlinked !== html) await writeFile(path, unlinked, 'utf8')
}

async function readBuiltTwinSource(
root: string,
pathname: string
Expand All @@ -88,8 +109,9 @@ async function readBuiltTwinSource(

/**
* Write a `.md` twin next to every built HTML page: `/cli/` → `/cli.md`,
* `/` → `/index.md`. Pages kept out of the sitemap get no twin, and a twin
* that already exists (hand-written by a page endpoint) is left alone.
* `/` → `/index.md`. Pages kept out of the sitemap get no twin, a page with no
* content loses its markdown link instead, and a twin that already exists
* (hand-written by a page endpoint) is left alone.
*/
export async function writeMarkdownTwins(
root: string,
Expand Down Expand Up @@ -125,6 +147,11 @@ export async function writeMarkdownTwins(
const page = htmlToTwin(source.html, canonical, {
canonical: source.alternate ? canonical : undefined
})
if (page.body === '') {
await unlinkTwin(root, pathname)
report.skipped.push(twinPath)
continue
}
await mkdir(dirname(target), { recursive: true })
await writeFile(target, renderTwin(page), 'utf8')
report.written.push(twinPath)
Expand Down
126 changes: 124 additions & 2 deletions apps/website/src/lib/markdown-twin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { mkdtemp, readFile, mkdir, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { h } from 'vue'
import { renderToString } from 'vue/server-renderer'

import WorkshopLoading from '../components/workshop/WorkshopLoading.vue'
import { isExcludedFromSitemap } from '../config/indexing'
import {
routerModelSlugAliases,
Expand All @@ -16,6 +19,10 @@ import { markdownTwinPath } from './markdown-twin-path'
const launch = vi.hoisted(() => ({ MODEL_PAGES_INDEXABLE: false }))
vi.mock(import('../config/model-page-launch'), () => launch)

const loadingSpinner = await renderToString(
h(WorkshopLoading, { label: 'Loading' })
)

const PAGE = `<!doctype html>
<html lang="en">
<head>
Expand All @@ -24,10 +31,11 @@ const PAGE = `<!doctype html>
<link rel="canonical" href="https://comfy.org/cli/">
</head>
<body>
<header><a href="/">Comfy home</a></header>
<astro-island><nav><a href="/mcp/">Comfy MCP</a><a href="/cloud/">Comfy Cloud</a></nav></astro-island>
<main>
<section>
<h1>Drive ComfyUI from your terminal. <br>Or your agent's.</h1>
<header><h1>Drive ComfyUI from your terminal. <br>Or your agent's.</h1></header>
<p>Generate from <a href="/cloud/">Comfy Cloud</a> or <a href="#setup">your own GPU</a> with <strong>comfy-cli</strong>.</p>
<div aria-hidden="true"><p>Claude Code Codex Cursor</p></div>
<div aria-hidden="true"><p>Claude Code Codex Cursor</p></div>
Expand All @@ -37,6 +45,8 @@ const PAGE = `<!doctype html>
<svg><title>icon</title></svg>
<video src="/clip.mp4"></video>
<button>Copy</button>
${loadingSpinner}
<p role="status">Saved to your library.</p>
</section>
<section>
<h2>Set up Comfy CLI</h2>
Expand Down Expand Up @@ -87,13 +97,19 @@ describe('htmlToTwin', () => {
"# Drive ComfyUI from your terminal. Or your agent's."
)
expect(page.body).not.toContain('Comfy MCP')
expect(page.body).not.toContain('Comfy home')
Comment thread
MaanilVerma marked this conversation as resolved.
Resolved
expect(page.body).not.toContain('Products')
expect(page.body).not.toContain('window.track')
expect(page.body).not.toContain('icon')
expect(page.body).not.toContain('Copy')
expect(page.body).not.toContain('clip.mp4')
})

it('drops the Workshop loading spinner but keeps other status copy', () => {
expect(page.body).not.toContain('Loading')
expect(page.body).toContain('Saved to your library.')
})

it('makes links absolute, drops anchor links, and keeps emphasis', () => {
expect(page.body).toContain(
'Generate from [Comfy Cloud](https://comfy.org/cloud/) or your own GPU with **comfy-cli**.'
Expand Down Expand Up @@ -125,6 +141,14 @@ describe('htmlToTwin', () => {
expect(page.body).toContain('> Method, not magic.')
})

it('drops the site banner but keeps page headers when there is no main', () => {
const noMain = htmlToTwin(
'<html><body><header><a href="/">Comfy home</a></header><article><header><h1>Pack</h1></header><p>Nodes.</p></article></body></html>',
'https://comfy.org/x/'
)
expect(noMain.body).toBe('# Pack\n\nNodes.')
})

it('falls back to the route when the page has no canonical link', () => {
const bare = htmlToTwin(
'<html><body><main><p>x</p></main></body></html>',
Expand Down Expand Up @@ -308,4 +332,102 @@ describe('writeMarkdownTwins', () => {
}
)
})

describe('model pages', () => {
const [{ slug: modelSlug }] = workshopModels
const route = `models/${modelSlug}/`
const twinPath = `/models/${modelSlug}.md`

async function buildModelPage(
main: string,
twinLink = `<link rel="alternate" type="text/markdown" href="${twinPath}">`
) {
const root = await mkdtemp(join(tmpdir(), 'twins-'))
await mkdir(join(root, route), { recursive: true })
await writeFile(
join(root, route, 'index.html'),
`<html lang="en"><head><title>${modelSlug}</title><link rel="canonical" href="https://comfy.org/${route}">${twinLink}</head><body><header><a href="/">Comfy home</a></header><main>${main}</main></body></html>`
)
return root
}

const readPage = (root: string) =>
readFile(join(root, route, 'index.html'), 'utf8')

beforeEach(() => {
launch.MODEL_PAGES_INDEXABLE = true
})

afterEach(() => {
launch.MODEL_PAGES_INDEXABLE = false
})

it('writes no twin and drops the markdown link for a spinner-only page', async () => {
const root = await buildModelPage(
`<astro-island>${loadingSpinner}</astro-island><noscript><h1>Grok Imagine in ComfyUI</h1></noscript>`
)

const report = await writeMarkdownTwins(root, [route])

expect(report.written).toEqual([])
expect(report.skipped).toEqual([twinPath])
await expect(readFile(join(root, twinPath))).rejects.toThrow()
const html = await readPage(root)
expect(html).not.toContain('text/markdown')
expect(html).toContain('rel="canonical"')
})

it('drops the markdown link whatever its attribute order', async () => {
const root = await buildModelPage(
`<astro-island>${loadingSpinner}</astro-island>`,
`<link href="${twinPath}"\n type="text/markdown" rel="alternate">`
)

await writeMarkdownTwins(root, [route])

expect(await readPage(root)).not.toContain('text/markdown')
})

it('leaves a contentless page that never linked a twin untouched', async () => {
const root = await buildModelPage(
`<astro-island>${loadingSpinner}</astro-island>`,
''
)
const before = await readPage(root)

const report = await writeMarkdownTwins(root, [route])

expect(report.skipped).toEqual([twinPath])
expect(await readPage(root)).toBe(before)
})

it('fails the build when a contentless page keeps a markdown link it cannot drop', async () => {
const root = await buildModelPage(
`<astro-island>${loadingSpinner}</astro-island>`,
`<LINK rel="alternate" type="text/markdown" href="${twinPath}">`
)

await expect(writeMarkdownTwins(root, [route])).rejects.toThrow(
join(root, route, 'index.html')
)
})

it('twins the server-rendered hero and drops the spinner (post-FE-2942 (#18899) shape)', async () => {
const root = await buildModelPage(
`<header data-testid="model-hero"><p>Provider</p><h1>Model name</h1><p>Model summary.</p></header><astro-island>${loadingSpinner}</astro-island>`
)

const report = await writeMarkdownTwins(root, [route])

expect(report.written).toEqual([twinPath])
const twin = await readFile(join(root, twinPath), 'utf8')
expect(twin).toContain(`canonical: https://comfy.org/${route}\n`)
expect(twin).toContain(
'---\n\nProvider\n\n# Model name\n\nModel summary.\n'
)
expect(twin).not.toContain('Loading')
expect(twin).not.toContain('Comfy home')
expect(await readPage(root)).toContain('text/markdown')
})
})
})
14 changes: 12 additions & 2 deletions apps/website/src/lib/markdown-twin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const DROPPED_TAGS = new Set([
'FORM',
'NAV',
'FOOTER',
'HEADER',
'DIALOG',
'SOURCE',
'TRACK'
Expand All @@ -46,6 +45,7 @@ const BLOCK_TAGS = new Set([
'DT',
'FIGCAPTION',
'FIGURE',
'HEADER',
'MAIN',
'P',
'SECTION',
Expand Down Expand Up @@ -86,10 +86,20 @@ function tag(element: Element): string {
return element.tagName.toUpperCase()
}

/** HTML-AAM: a header outside main, article, aside and section is the site banner. */
function isBanner(element: Element): boolean {
Comment thread
benjcooley marked this conversation as resolved.
Resolved
return (
tag(element) === 'HEADER' &&
element.closest('main, article, aside, section') === null
)
}

function isDropped(element: Element): boolean {
return (
DROPPED_TAGS.has(tag(element)) ||
element.getAttribute('aria-hidden') === 'true'
isBanner(element) ||
element.getAttribute('aria-hidden') === 'true' ||
element.hasAttribute('data-twin-omit')
)
}

Expand Down
5 changes: 0 additions & 5 deletions apps/website/src/pages/workshop/models/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ const { model, related } = Astro.props
</span>
</nav>

{/*
A div, not a header. `markdown-twin.ts` drops HEADER along with the
site chrome, so wrapping the hero in one leaves every model's llms.txt
entry with a title and nothing else. The h1 still carries the heading.
*/}
<div class="mb-12 max-w-4xl">
<p class="text-sm tracking-widest text-primary-comfy-yellow uppercase">
{model.provider}
Expand Down
Loading