mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Closes DOCS-1756 ## Problem No docs page tells an agent that `llms.txt` exists or that a markdown version is available. Agents that send `Accept: text/markdown` already get markdown, but agents that fetch HTML have no signal to try the markdown path. ## Solution - **HTML:** add a visually hidden directive as the first child of `<body>` in the root layout, so it comes before the navigation on every guide and reference page. It links to `https://supabase.com/docs/llms.txt` and says guide pages have a markdown version at the same path with `.md` appended, before any query string or fragment. It's `sr-only` and `aria-hidden`, and the link has `tabindex="-1"`. - **Crawler HTML:** bot requests for SDK reference pages are rewritten to `/api/crawlers`, which skips the root layout. The same directive goes before its `<nav>`. - **Markdown:** add a sentence linking `llms.txt` to the "Note for AI agents" blockquote in the guides markdown route. The agent feedback page skips the feedback text to avoid linking to itself, but it keeps the `llms.txt` line. - Add `LLMS_TXT_URL` and `LLMS_TXT_DIRECTIVE_HTML` to `common/markdown-affordance`, and reuse `LLMS_TXT_URL` in `proxy.ts`. The directive limits the `.md` claim to guides, because reference `.md` requests 404. It doesn't say `llms.txt` lists every page. The link targets `/docs/llms.txt`, the docs index that AFDocs scores for the `/docs` base URL. #53968 makes it a real file. Until that merges, `/docs/llms.txt` 308-redirects to the site-wide `/llms.txt`, so the link works in either merge order. ## Manual testing 1. Open a Vercel preview guide page and view source. Confirm the `llms.txt` directive comes before the navigation markup. 2. Open a reference page and confirm the same. 3. Confirm the directive isn't visible and isn't tabbable. 4. Request a reference section page with a bot user agent, such as `curl -A Googlebot`, and confirm the directive comes before `<nav>`. 5. Request a guide with `.md` appended and confirm the opening blockquote links `llms.txt`. 6. Request `/docs/guides/ai-tools/agent-feedback.md` and confirm the opening blockquote links `llms.txt`. 7. Run AFDocs against the preview and confirm `llms-txt-directive-html` and `llms-txt-directive-md` pass.
272 lines
8.4 KiB
TypeScript
272 lines
8.4 KiB
TypeScript
import { REFERENCES } from '~/content/navigation.references'
|
|
import {
|
|
getFlattenedSections,
|
|
getFunctionsList,
|
|
getTypeSpec,
|
|
} from '~/features/docs/Reference.generated.singleton'
|
|
import { getRefMarkdown } from '~/features/docs/Reference.mdx'
|
|
import type { MethodTypes, VariableTypes } from '~/features/docs/Reference.typeSpec'
|
|
import type { AbbrevApiReferenceSection } from '~/features/docs/Reference.utils'
|
|
import { PROD_URL } from '~/lib/constants'
|
|
import { LLMS_TXT_DIRECTIVE_HTML } from 'common/markdown-affordance'
|
|
import { toHtml } from 'hast-util-to-html'
|
|
import { fromMarkdown } from 'mdast-util-from-markdown'
|
|
import { mdxFromMarkdown } from 'mdast-util-mdx'
|
|
import { toHast } from 'mdast-util-to-hast'
|
|
import { mdxjs } from 'micromark-extension-mdxjs'
|
|
import { notFound } from 'next/navigation'
|
|
import { visit } from 'unist-util-visit'
|
|
|
|
export async function GET(request: Request) {
|
|
const url = new URL(request.url)
|
|
let [, , lib, maybeVersion, slug] = url.pathname.split('/')
|
|
|
|
const libraryMeta = REFERENCES[lib]
|
|
|
|
const isVersion = /^v\d+$/.test(maybeVersion)
|
|
const version = isVersion ? maybeVersion : libraryMeta.versions[0]
|
|
if (!isVersion) {
|
|
slug = maybeVersion
|
|
}
|
|
|
|
let section: AbbrevApiReferenceSection | undefined
|
|
let sectionsWithUrl: Array<AbbrevApiReferenceSection & { url: URL }> = []
|
|
try {
|
|
const flattenedSections = (await getFlattenedSections(lib, version)) ?? []
|
|
sectionsWithUrl = flattenedSections.map((section) => {
|
|
const url = new URL(
|
|
[PROD_URL, 'reference', lib, isVersion ? version : null, section.slug]
|
|
.filter(Boolean)
|
|
.join('/')
|
|
)
|
|
|
|
return {
|
|
...section,
|
|
url,
|
|
}
|
|
})
|
|
const contentSections = flattenedSections.filter(
|
|
(section) => section.type === 'markdown' || section.type === 'function'
|
|
)
|
|
section = contentSections.find((section) => section.slug === slug)
|
|
if (!section) {
|
|
const legacySlug = slug?.toLowerCase()
|
|
const replacement = legacyReferenceSlug(lib, version, legacySlug)
|
|
|
|
if (replacement) {
|
|
section = contentSections.find((section) => section.slug?.toLowerCase() === replacement)
|
|
} else if (legacySlug && version === 'v2' && (lib === 'javascript' || lib === 'dart')) {
|
|
const matches = contentSections.filter((section) =>
|
|
section.slug?.toLowerCase().endsWith(`-${legacySlug}`)
|
|
)
|
|
if (matches.length === 1) section = matches[0]
|
|
}
|
|
}
|
|
} catch {}
|
|
|
|
if (!section) {
|
|
notFound()
|
|
}
|
|
|
|
const html = htmlShell(
|
|
lib,
|
|
isVersion ? version : null,
|
|
section.slug ?? slug,
|
|
section,
|
|
libraryNav(sectionsWithUrl) + (await sectionDetails(lib, isVersion ? version : null, section))
|
|
)
|
|
const response = new Response(html)
|
|
response.headers.set('Content-Type', 'text/html; charset=utf-8')
|
|
|
|
return response
|
|
}
|
|
|
|
function legacyReferenceSlug(lib: string, version: string, slug?: string) {
|
|
if (!slug || slug === 'start') return 'introduction'
|
|
if (lib === 'swift' && version === 'v2' && slug === 'get-user') return 'auth-getuser'
|
|
if (version !== 'v2' || (lib !== 'javascript' && lib !== 'dart')) return undefined
|
|
if (slug === 'admin-api') return 'auth-admin'
|
|
if (slug === 'get-user') return lib === 'dart' ? 'auth-currentuser' : 'auth-getuser'
|
|
if (slug.startsWith('storage-from-')) return `file-buckets-${slug.slice('storage-from-'.length)}`
|
|
if (slug.startsWith('storage-')) return `file-buckets-${slug.slice('storage-'.length)}`
|
|
return undefined
|
|
}
|
|
|
|
function htmlShell(
|
|
lib: string,
|
|
version: string | null,
|
|
slug: string,
|
|
section: AbbrevApiReferenceSection,
|
|
body: string
|
|
) {
|
|
const libraryName = REFERENCES[lib].name
|
|
const versionPath = version && version !== REFERENCES[lib].versions[0] ? '/' + version : ''
|
|
let title = libraryName + ': ' + (section.title ?? '')
|
|
|
|
return (
|
|
'<!doctype html><html>' +
|
|
'<head>' +
|
|
`<title>${title} | Supabase Docs</title>` +
|
|
`<meta name="description" content="Supabase API reference for ${libraryName}${section.title ? ': ' + section.title : ''}">` +
|
|
`<meta name="og:image" content="https://supabase.com/docs/img/supabase-og-image.png">` +
|
|
`<meta name="twitter:image" content="https://supabase.com/docs/img/supabase-og-image.png">` +
|
|
`<link rel="canonical" href="https://supabase.com/docs/reference/${lib}` +
|
|
versionPath +
|
|
(slug ? '/' + slug : '') +
|
|
`">` +
|
|
'</head>' +
|
|
'<body>' +
|
|
`<p>${LLMS_TXT_DIRECTIVE_HTML}</p>` +
|
|
body +
|
|
'</body></html>'
|
|
)
|
|
}
|
|
|
|
function libraryNav(sections: Array<AbbrevApiReferenceSection & { url: URL }>) {
|
|
return (
|
|
'<nav><ul>' +
|
|
sections
|
|
.map((section) => `<li><a href="${section.url}">${section.title ?? ''}</a></li>`)
|
|
.join('') +
|
|
'</ul></nav>'
|
|
)
|
|
}
|
|
|
|
async function sectionDetails(lib: string, version: string, section: AbbrevApiReferenceSection) {
|
|
const libraryName = REFERENCES[lib].name
|
|
let result = '<h1>' + (libraryName + ': ' + (section.title ?? '')) + '</h1>'
|
|
|
|
if (section.type === 'markdown') {
|
|
result += await markdown(lib, version, section)
|
|
} else {
|
|
result += await functionDetails(lib, version, section)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
async function markdown(lib: string, version: string | null, section: AbbrevApiReferenceSection) {
|
|
const dir = !!section.meta?.shared ? 'shared' : lib + (version ? '/' + version : '')
|
|
|
|
let content = await getRefMarkdown(dir + '/' + section.slug)
|
|
content = mdxToHtml(content)
|
|
return content
|
|
}
|
|
|
|
async function functionDetails(
|
|
lib: string,
|
|
version: string | null,
|
|
section: AbbrevApiReferenceSection
|
|
) {
|
|
const libraryMeta = REFERENCES[lib]
|
|
|
|
const fns = await getFunctionsList(lib, version ?? libraryMeta.versions[0])
|
|
const fn = fns!.find((fn) => fn.id === section.id)
|
|
if (!fn) return ''
|
|
|
|
let types: MethodTypes | VariableTypes | undefined
|
|
if (libraryMeta.typeSpec && '$ref' in fn) {
|
|
types = await getTypeSpec(lib, version ?? libraryMeta.versions[0], fn['$ref'] as string)
|
|
}
|
|
|
|
const fullDescription = [
|
|
types?.comment?.shortText,
|
|
'description' in fn && (fn.description as string),
|
|
'notes' in fn && (fn.notes as string),
|
|
]
|
|
.filter((x) => typeof x === 'string')
|
|
.map(mdxToHtml)
|
|
.join('')
|
|
|
|
const parameters = parametersToHtml(fn, types)
|
|
const examples = examplesToHtml(fn, types)
|
|
|
|
return fullDescription + parameters + examples
|
|
}
|
|
|
|
function mdxToHtml(markdown: string): string {
|
|
const mdast = fromMarkdown(markdown, {
|
|
extensions: [mdxjs()],
|
|
mdastExtensions: [mdxFromMarkdown()],
|
|
})
|
|
|
|
visit(mdast, 'text', (node) => {
|
|
node.value = node.value.replace(/\n/g, ' ')
|
|
})
|
|
if (!mdast) return ''
|
|
|
|
const hast = toHast(mdast)
|
|
if (!hast) return ''
|
|
|
|
// @ts-ignore
|
|
const html = toHtml(hast)
|
|
|
|
return html
|
|
}
|
|
|
|
function parametersToHtml(fn: any, types: MethodTypes | VariableTypes | undefined) {
|
|
let result = '<h2 id="parameters">Parameters</h2>'
|
|
|
|
if ('overwriteParams' in fn || 'params' in fn) {
|
|
const params = fn.overwriteParams ?? fn.params
|
|
if (params.length === 0) return ''
|
|
|
|
result +=
|
|
'<ul>' +
|
|
params
|
|
.map(
|
|
(param) =>
|
|
'<li>' +
|
|
`<h3>${param.name}</h3>` +
|
|
`<span>${param.isOptional ? '(Optional)' : '(Required)'}</span>` +
|
|
`<p>${param.description}</p>` +
|
|
'</li>'
|
|
)
|
|
.join('') +
|
|
'</ul>'
|
|
|
|
return result
|
|
}
|
|
|
|
if (!types || !('params' in types) || !types.params || types.params.length === 0) return ''
|
|
|
|
result +=
|
|
'<ul>' +
|
|
types.params
|
|
.map(
|
|
(param) =>
|
|
'<li>' +
|
|
`<h3>${String(param.name)}</h3>` +
|
|
`<span>${param.isOptional ? '(Optional)' : '(Required)'}</span>` +
|
|
`<p>${param.comment?.shortText ?? ''}</p>` +
|
|
'</li>'
|
|
)
|
|
.join('') +
|
|
'</ul>'
|
|
|
|
return result
|
|
}
|
|
|
|
function examplesToHtml(fn: any, types?: MethodTypes | VariableTypes) {
|
|
// Prefer hand-authored YAML/JSON examples on the section entry; fall back to
|
|
// TSDoc-extracted `@example` blocks on the method's normalised comment. The
|
|
// page renderer in `Reference.sections.tsx` does the same merge, so the
|
|
// crawler stays consistent with what a browser sees.
|
|
const examples =
|
|
Array.isArray(fn.examples) && fn.examples.length > 0
|
|
? fn.examples
|
|
: (types?.comment?.examples ?? [])
|
|
if (examples.length === 0) return ''
|
|
|
|
let result = '<h2 id="examples">Examples</h2>'
|
|
|
|
result += examples
|
|
.map(
|
|
(example: { name?: string; code?: string }) =>
|
|
`<h3>${example.name ?? ''}</h3>` + mdxToHtml(example.code ?? '')
|
|
)
|
|
.join('')
|
|
|
|
return result
|
|
}
|