Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 42 lines 1.63 KB
Raw
1 import { detectReferenceApiVersion } from './api-version';
2 import { resolveDocsProduct } from './home-navigation';
3
4 const DOCS_ORIGIN = 'https://docs.composio.dev';
5
6 /** JSON scalars are also YAML scalars, so URLs and strings remain safely quoted. */
7 export function withMarkdownMetadata(
8 body: string,
9 pathname: string,
10 lastUpdated: string | null,
11 init?: ResponseInit,
12 ): Response {
13 const canonicalUrl = new URL(pathname, DOCS_ORIGIN).href;
14 const metadata = {
15 canonical_url: canonicalUrl,
16 markdown_url: `${canonicalUrl}.md`,
17 product: resolveDocsProduct(pathname),
18 version: detectReferenceApiVersion(pathname),
19 last_updated: lastUpdated,
20 };
21 const frontmatter = Object.entries(metadata)
22 .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
23 .join('\n');
24 const headers = new Headers(init?.headers);
25 headers.set('Content-Type', 'text/markdown; charset=utf-8');
26 const vary = headers.get('Vary');
27 if (!vary?.split(',').some(value => ['accept', '*'].includes(value.trim().toLowerCase()))) {
28 headers.set('Vary', vary ? `${vary}, Accept` : 'Accept');
29 }
30 return new Response(`---\n${frontmatter}\n---\n\n${body}`, { ...init, headers });
31 }
32
33 export function missingMarkdownResponse(): Response {
34 return new Response(
35 '# Page not found\n\nThis documentation page does not exist. Check the URL or use these discovery links:\n\n' +
36 '- [Documentation index](/llms.txt)\n' +
37 '- [Complete page index](/llms-index.txt)\n' +
38 '- [Search support knowledge](/kb/search)\n' +
39 '- [Quickstart](/docs/quickstart.md)\n',
40 { status: 404, headers: { 'Content-Type': 'text/markdown; charset=utf-8' } },
41 );
42 }