@writedocs/generator 0.13.3 → 0.13.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -28
- package/astro.config.mjs +645 -645
- package/bin/writedocs.js +43 -12
- package/package.json +99 -99
- package/src/cli/base-path-layout.js +44 -44
- package/src/cli/build-auth.js +135 -135
- package/src/cli/build.js +203 -203
- package/src/cli/check-built-styles.js +41 -41
- package/src/cli/convert.js +1 -1
- package/src/cli/dev.js +268 -268
- package/src/cli/exit-when-flushed.js +4 -0
- package/src/cli/generate-api-pages.js +507 -507
- package/src/cli/og-image-worker.js +20 -20
- package/src/cli/og-images.js +70 -70
- package/src/cli/page-list-watch.js +69 -69
- package/src/cli/run-astro.js +97 -97
- package/src/cli/run-pagefind.js +154 -154
- package/src/cli/write-mcp-files.js +103 -103
- package/src/cli/write-redirects-file.js +158 -158
- package/src/components/ApiReferencePanel.astro +1792 -1792
- package/src/components/AppIcon.astro +51 -51
- package/src/components/Card.astro +193 -193
- package/src/components/CodeGroup.astro +63 -39
- package/src/components/CopyPageMenu.astro +374 -374
- package/src/components/Hint.astro +188 -188
- package/src/components/Icon.astro +72 -72
- package/src/components/Image.astro +165 -165
- package/src/components/Tile.astro +67 -67
- package/src/components/Video.astro +137 -137
- package/src/content.config.ts +203 -203
- package/src/layout/BaseLayout.astro +908 -908
- package/src/layout/components/AnalyticsScripts.astro +49 -49
- package/src/layout/components/Breadcrumbs.astro +98 -98
- package/src/layout/components/ConsentBanner.astro +119 -119
- package/src/layout/components/MobileMenu.astro +225 -225
- package/src/layout/components/NavTree.astro +408 -408
- package/src/layout/components/Sidebar.astro +232 -232
- package/src/layout/components/SiteFooter.astro +87 -87
- package/src/layout/components/TopBar.astro +386 -386
- package/src/layout/styles/base.css +332 -326
- package/src/layout/styles/mobile-menu.css +285 -285
- package/src/lib/agent-markdown.js +164 -164
- package/src/lib/asset-dimensions.js +89 -89
- package/src/lib/base-path.js +90 -90
- package/src/lib/cli-report.js +22 -22
- package/src/lib/code-line-numbers.js +13 -0
- package/src/lib/config-schema.js +414 -75
- package/src/lib/config-schema.ts +2509 -1905
- package/src/lib/config.ts +1316 -1314
- package/src/lib/consent.js +108 -108
- package/src/lib/content-store-reload-integration.js +101 -101
- package/src/lib/icon-data-dev.js +53 -53
- package/src/lib/image-size.js +152 -152
- package/src/lib/json-schema-descriptions.js +228 -228
- package/src/lib/link-check.js +544 -544
- package/src/lib/llms-index.ts +90 -90
- package/src/lib/mcp-dev-integration.js +65 -65
- package/src/lib/mcp-index.ts +102 -102
- package/src/lib/mintlify-convert.js +605 -605
- package/src/lib/moved-api-pages.js +49 -49
- package/src/lib/og-cards.js +33 -33
- package/src/lib/og-image.js +235 -235
- package/src/lib/openapi-dev-status.js +80 -80
- package/src/lib/openapi-reload-integration.js +193 -193
- package/src/lib/pages.js +312 -312
- package/src/lib/project-files-integration.js +116 -116
- package/src/lib/project-files.js +137 -137
- package/src/lib/rehype-base-path.js +62 -62
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/robots-txt.js +21 -21
- package/src/lib/search-scope.js +44 -44
- package/src/lib/selector-placement.js +50 -50
- package/src/lib/seo-tags.js +155 -155
- package/src/lib/shiki-code-block.js +7 -1
- package/src/lib/site-base.ts +27 -27
- package/src/lib/styles-asset-integration.js +291 -291
- package/src/lib/wdignore-cases.json +195 -195
- package/src/lib/wdignore.js +177 -177
- package/src/pages/404.astro +63 -63
- package/src/pages/[...slug].astro +1762 -1608
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -88
- package/src/pages/og/[...slug].png.ts +63 -63
- package/src/scripts/mobile-menu.ts +80 -80
- package/src/scripts/search.ts +245 -245
- package/src/scripts/sidebar-scroll.ts +61 -61
- package/src/styles/global.css +40 -40
package/src/lib/llms-index.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
// llms.txt and its child files (/llms/*.md) - the pages an AI tool can
|
|
2
|
-
// discover, in the site's navigation structure. Shared by llms.txt.ts (the
|
|
3
|
-
// root file) and llms/[...path].md.ts (the child files a big site's llms.txt
|
|
4
|
-
// links to), so both render from the same tree in the same run. The
|
|
5
|
-
// structure and the splitting live in lib/llms.js.
|
|
6
|
-
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
7
|
-
import fs from 'node:fs';
|
|
8
|
-
import path from 'node:path';
|
|
9
|
-
import { loadDocsConfig, normalizeEntryId, findAllPages, resolveSiteRoot, fileIdForEntry, contextMenuOptions } from './config';
|
|
10
|
-
import { buildLlmsTree, renderLlmsFiles } from './llms.js';
|
|
11
|
-
import { navigationPageOrder } from './pages.js';
|
|
12
|
-
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
13
|
-
|
|
14
|
-
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
15
|
-
|
|
16
|
-
// Mirrors Mintlify's own llms.txt behavior: a page's frontmatter
|
|
17
|
-
// `description` is cut at the first line break (a multi-paragraph
|
|
18
|
-
// description would blow out a one-line list entry) and at 300 characters,
|
|
19
|
-
// so every entry stays scannable.
|
|
20
|
-
const DESCRIPTION_MAX_CHARS = 300;
|
|
21
|
-
function truncateDescription(description: string | undefined): string | undefined {
|
|
22
|
-
if (!description) return undefined;
|
|
23
|
-
const firstLine = description.split('\n')[0].trim();
|
|
24
|
-
if (!firstLine) return undefined;
|
|
25
|
-
return firstLine.length > DESCRIPTION_MAX_CHARS ? firstLine.slice(0, DESCRIPTION_MAX_CHARS).trimEnd() + '…' : firstLine;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Most characters one file holds - llms.txt or a child file. The llms.txt
|
|
29
|
-
// spec sets no limit ("small enough to fit in context"); this is Mintlify's,
|
|
30
|
-
// about 25,000 tokens. Past it, sections move into child files instead of
|
|
31
|
-
// pages being dropped.
|
|
32
|
-
export const LLMS_MAX_CHARS = 100_000;
|
|
33
|
-
|
|
34
|
-
/** The generated files as Map<path, text> - 'llms.txt', then 'llms/<...>.md'
|
|
35
|
-
* when the site needs them - or null when the project has its own
|
|
36
|
-
* llms.txt, which replaces all of it. */
|
|
37
|
-
export async function llmsIndexFiles(): Promise<Map<string, string> | null> {
|
|
38
|
-
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
39
|
-
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
40
|
-
// A hand-authored llms.txt at the project root (next to writedocs.json)
|
|
41
|
-
// wins outright - same override convention Mintlify documents.
|
|
42
|
-
if (fs.existsSync(path.join(contentDir, 'llms.txt'))) return null;
|
|
43
|
-
|
|
44
|
-
const config = loadDocsConfig(contentDir);
|
|
45
|
-
// The site's URL if `domain` is set, else its base ("/docs", or "" at the
|
|
46
|
-
// root) - lib/config.ts.
|
|
47
|
-
const siteRoot = resolveSiteRoot(config);
|
|
48
|
-
|
|
49
|
-
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
50
|
-
const hasPages = findAllPages(contentDir).length > 0;
|
|
51
|
-
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
52
|
-
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
53
|
-
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
54
|
-
]);
|
|
55
|
-
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
56
|
-
|
|
57
|
-
const pages = new Map<string, { title: string; href: string; description?: string; slug: string }>();
|
|
58
|
-
for (const entry of entries) {
|
|
59
|
-
// A page that opted out of search-engine indexing (`seo.noindex`, also
|
|
60
|
-
// left out of sitemap.xml) is left out here too, and a frontmatter `url`
|
|
61
|
-
// page (Mintlify's external link) has no content of its own.
|
|
62
|
-
if (entry.data.seo?.noindex || entry.data.url) continue;
|
|
63
|
-
const slug = normalizeEntryId(entry.id);
|
|
64
|
-
// The .md route when it exists ([...slug].md.ts - unless `contextMenu` is off,
|
|
65
|
-
// and never for an OpenAPI page, which renders from the spec), else the
|
|
66
|
-
// HTML page.
|
|
67
|
-
const hasMarkdownRoute = contextMenuOptions(config.contextMenu).length > 0;
|
|
68
|
-
const href = siteRoot + (hasMarkdownRoute ? `/${slug}.md` : slug === 'index' ? '/' : `/${slug}/`);
|
|
69
|
-
let description = truncateDescription(entry.data.description);
|
|
70
|
-
// Mirrors Mintlify: an OpenAPI operation page's description gets its
|
|
71
|
-
// "METHOD /path" appended - the page itself renders from the spec.
|
|
72
|
-
if (entry.data.openapi) description = description ? `${description} (${entry.data.openapi})` : entry.data.openapi;
|
|
73
|
-
pages.set(fileIdForEntry(contentDir, packageRoot, entry), { title: entry.data.title, href, description, slug });
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const listed = new Set(navigationPageOrder(config.navigation));
|
|
77
|
-
const unlisted = [...pages.entries()]
|
|
78
|
-
.filter(([id]) => !listed.has(id))
|
|
79
|
-
.sort((a, b) => a[1].slug.localeCompare(b[1].slug))
|
|
80
|
-
.map(([id]) => id);
|
|
81
|
-
const tree = buildLlmsTree(config.navigation, (id: string) => pages.get(id) ?? null, unlisted);
|
|
82
|
-
|
|
83
|
-
return renderLlmsFiles({
|
|
84
|
-
name: config.name,
|
|
85
|
-
description: config.description,
|
|
86
|
-
tree,
|
|
87
|
-
maxChars: LLMS_MAX_CHARS,
|
|
88
|
-
urlFor: (file: string) => `${siteRoot}/${file}`,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
1
|
+
// llms.txt and its child files (/llms/*.md) - the pages an AI tool can
|
|
2
|
+
// discover, in the site's navigation structure. Shared by llms.txt.ts (the
|
|
3
|
+
// root file) and llms/[...path].md.ts (the child files a big site's llms.txt
|
|
4
|
+
// links to), so both render from the same tree in the same run. The
|
|
5
|
+
// structure and the splitting live in lib/llms.js.
|
|
6
|
+
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { loadDocsConfig, normalizeEntryId, findAllPages, resolveSiteRoot, fileIdForEntry, contextMenuOptions } from './config';
|
|
10
|
+
import { buildLlmsTree, renderLlmsFiles } from './llms.js';
|
|
11
|
+
import { navigationPageOrder } from './pages.js';
|
|
12
|
+
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
13
|
+
|
|
14
|
+
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
15
|
+
|
|
16
|
+
// Mirrors Mintlify's own llms.txt behavior: a page's frontmatter
|
|
17
|
+
// `description` is cut at the first line break (a multi-paragraph
|
|
18
|
+
// description would blow out a one-line list entry) and at 300 characters,
|
|
19
|
+
// so every entry stays scannable.
|
|
20
|
+
const DESCRIPTION_MAX_CHARS = 300;
|
|
21
|
+
function truncateDescription(description: string | undefined): string | undefined {
|
|
22
|
+
if (!description) return undefined;
|
|
23
|
+
const firstLine = description.split('\n')[0].trim();
|
|
24
|
+
if (!firstLine) return undefined;
|
|
25
|
+
return firstLine.length > DESCRIPTION_MAX_CHARS ? firstLine.slice(0, DESCRIPTION_MAX_CHARS).trimEnd() + '…' : firstLine;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Most characters one file holds - llms.txt or a child file. The llms.txt
|
|
29
|
+
// spec sets no limit ("small enough to fit in context"); this is Mintlify's,
|
|
30
|
+
// about 25,000 tokens. Past it, sections move into child files instead of
|
|
31
|
+
// pages being dropped.
|
|
32
|
+
export const LLMS_MAX_CHARS = 100_000;
|
|
33
|
+
|
|
34
|
+
/** The generated files as Map<path, text> - 'llms.txt', then 'llms/<...>.md'
|
|
35
|
+
* when the site needs them - or null when the project has its own
|
|
36
|
+
* llms.txt, which replaces all of it. */
|
|
37
|
+
export async function llmsIndexFiles(): Promise<Map<string, string> | null> {
|
|
38
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
39
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
40
|
+
// A hand-authored llms.txt at the project root (next to writedocs.json)
|
|
41
|
+
// wins outright - same override convention Mintlify documents.
|
|
42
|
+
if (fs.existsSync(path.join(contentDir, 'llms.txt'))) return null;
|
|
43
|
+
|
|
44
|
+
const config = loadDocsConfig(contentDir);
|
|
45
|
+
// The site's URL if `domain` is set, else its base ("/docs", or "" at the
|
|
46
|
+
// root) - lib/config.ts.
|
|
47
|
+
const siteRoot = resolveSiteRoot(config);
|
|
48
|
+
|
|
49
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
50
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
51
|
+
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
52
|
+
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
53
|
+
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
54
|
+
]);
|
|
55
|
+
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
56
|
+
|
|
57
|
+
const pages = new Map<string, { title: string; href: string; description?: string; slug: string }>();
|
|
58
|
+
for (const entry of entries) {
|
|
59
|
+
// A page that opted out of search-engine indexing (`seo.noindex`, also
|
|
60
|
+
// left out of sitemap.xml) is left out here too, and a frontmatter `url`
|
|
61
|
+
// page (Mintlify's external link) has no content of its own.
|
|
62
|
+
if (entry.data.seo?.noindex || entry.data.url) continue;
|
|
63
|
+
const slug = normalizeEntryId(entry.id);
|
|
64
|
+
// The .md route when it exists ([...slug].md.ts - unless `contextMenu` is off,
|
|
65
|
+
// and never for an OpenAPI page, which renders from the spec), else the
|
|
66
|
+
// HTML page.
|
|
67
|
+
const hasMarkdownRoute = contextMenuOptions(config.contextMenu).length > 0;
|
|
68
|
+
const href = siteRoot + (hasMarkdownRoute ? `/${slug}.md` : slug === 'index' ? '/' : `/${slug}/`);
|
|
69
|
+
let description = truncateDescription(entry.data.description);
|
|
70
|
+
// Mirrors Mintlify: an OpenAPI operation page's description gets its
|
|
71
|
+
// "METHOD /path" appended - the page itself renders from the spec.
|
|
72
|
+
if (entry.data.openapi) description = description ? `${description} (${entry.data.openapi})` : entry.data.openapi;
|
|
73
|
+
pages.set(fileIdForEntry(contentDir, packageRoot, entry), { title: entry.data.title, href, description, slug });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const listed = new Set(navigationPageOrder(config.navigation));
|
|
77
|
+
const unlisted = [...pages.entries()]
|
|
78
|
+
.filter(([id]) => !listed.has(id))
|
|
79
|
+
.sort((a, b) => a[1].slug.localeCompare(b[1].slug))
|
|
80
|
+
.map(([id]) => id);
|
|
81
|
+
const tree = buildLlmsTree(config.navigation, (id: string) => pages.get(id) ?? null, unlisted);
|
|
82
|
+
|
|
83
|
+
return renderLlmsFiles({
|
|
84
|
+
name: config.name,
|
|
85
|
+
description: config.description,
|
|
86
|
+
tree,
|
|
87
|
+
maxChars: LLMS_MAX_CHARS,
|
|
88
|
+
urlFor: (file: string) => `${siteRoot}/${file}`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
// `writedocs dev`: the site's MCP server at /mcp, as the build serves it - the
|
|
2
|
-
// same handler (src/mcp/server.js), answering from the dev server's own
|
|
3
|
-
// /mcp-index.json, so an edit shows up on the next request. Lets an author
|
|
4
|
-
// point an MCP client at http://localhost:4321/mcp before deploying.
|
|
5
|
-
import { handleMcpHttp } from '../mcp/server.js';
|
|
6
|
-
import { withoutBase } from './base-path.js';
|
|
7
|
-
|
|
8
|
-
function readBody(req) {
|
|
9
|
-
return new Promise((resolve, reject) => {
|
|
10
|
-
const chunks = [];
|
|
11
|
-
req.on('data', (chunk) => chunks.push(chunk));
|
|
12
|
-
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
13
|
-
req.on('error', reject);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// `basePath`: writedocs.json's ("" or "/docs") - the server is at /docs/mcp.
|
|
18
|
-
// Astro's dev server takes the base off req.url before this middleware runs
|
|
19
|
-
// (its devBaseMiddleware), so /docs/mcp and a bare /mcp would look the same
|
|
20
|
-
// there: the path asked for is req.originalUrl.
|
|
21
|
-
export function mcpDevServer({ version, basePath = '' }) {
|
|
22
|
-
return {
|
|
23
|
-
name: 'writedocs-mcp-dev',
|
|
24
|
-
hooks: {
|
|
25
|
-
'astro:server:setup': ({ server }) => {
|
|
26
|
-
server.middlewares.use(async (req, res, next) => {
|
|
27
|
-
const pathname = withoutBase(basePath, (req.originalUrl ?? req.url ?? '').split('?')[0]);
|
|
28
|
-
if (pathname !== '/mcp' && pathname !== '/mcp/') return next();
|
|
29
|
-
try {
|
|
30
|
-
const origin = `http://${req.headers.host}`;
|
|
31
|
-
const headers = new Headers();
|
|
32
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
33
|
-
if (typeof value === 'string') headers.set(key, value);
|
|
34
|
-
}
|
|
35
|
-
const body = req.method === 'POST' ? await readBody(req) : undefined;
|
|
36
|
-
const request = new Request(origin + (req.originalUrl ?? req.url), { method: req.method, headers, body });
|
|
37
|
-
let missing = false;
|
|
38
|
-
const response = await handleMcpHttp(request, {
|
|
39
|
-
version,
|
|
40
|
-
loadIndex: async () => {
|
|
41
|
-
const index = await fetch(`${origin}${basePath}/mcp-index.json`);
|
|
42
|
-
if (!index.ok) {
|
|
43
|
-
missing = true;
|
|
44
|
-
return { meta: {}, docs: [] };
|
|
45
|
-
}
|
|
46
|
-
return index.json();
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
if (missing) {
|
|
50
|
-
res.statusCode = 404;
|
|
51
|
-
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
52
|
-
res.end('No MCP server: writedocs.json has "mcp": false.');
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
res.statusCode = response.status;
|
|
56
|
-
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
57
|
-
res.end(Buffer.from(await response.arrayBuffer()));
|
|
58
|
-
} catch (err) {
|
|
59
|
-
next(err);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
1
|
+
// `writedocs dev`: the site's MCP server at /mcp, as the build serves it - the
|
|
2
|
+
// same handler (src/mcp/server.js), answering from the dev server's own
|
|
3
|
+
// /mcp-index.json, so an edit shows up on the next request. Lets an author
|
|
4
|
+
// point an MCP client at http://localhost:4321/mcp before deploying.
|
|
5
|
+
import { handleMcpHttp } from '../mcp/server.js';
|
|
6
|
+
import { withoutBase } from './base-path.js';
|
|
7
|
+
|
|
8
|
+
function readBody(req) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
req.on('data', (chunk) => chunks.push(chunk));
|
|
12
|
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
13
|
+
req.on('error', reject);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// `basePath`: writedocs.json's ("" or "/docs") - the server is at /docs/mcp.
|
|
18
|
+
// Astro's dev server takes the base off req.url before this middleware runs
|
|
19
|
+
// (its devBaseMiddleware), so /docs/mcp and a bare /mcp would look the same
|
|
20
|
+
// there: the path asked for is req.originalUrl.
|
|
21
|
+
export function mcpDevServer({ version, basePath = '' }) {
|
|
22
|
+
return {
|
|
23
|
+
name: 'writedocs-mcp-dev',
|
|
24
|
+
hooks: {
|
|
25
|
+
'astro:server:setup': ({ server }) => {
|
|
26
|
+
server.middlewares.use(async (req, res, next) => {
|
|
27
|
+
const pathname = withoutBase(basePath, (req.originalUrl ?? req.url ?? '').split('?')[0]);
|
|
28
|
+
if (pathname !== '/mcp' && pathname !== '/mcp/') return next();
|
|
29
|
+
try {
|
|
30
|
+
const origin = `http://${req.headers.host}`;
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
33
|
+
if (typeof value === 'string') headers.set(key, value);
|
|
34
|
+
}
|
|
35
|
+
const body = req.method === 'POST' ? await readBody(req) : undefined;
|
|
36
|
+
const request = new Request(origin + (req.originalUrl ?? req.url), { method: req.method, headers, body });
|
|
37
|
+
let missing = false;
|
|
38
|
+
const response = await handleMcpHttp(request, {
|
|
39
|
+
version,
|
|
40
|
+
loadIndex: async () => {
|
|
41
|
+
const index = await fetch(`${origin}${basePath}/mcp-index.json`);
|
|
42
|
+
if (!index.ok) {
|
|
43
|
+
missing = true;
|
|
44
|
+
return { meta: {}, docs: [] };
|
|
45
|
+
}
|
|
46
|
+
return index.json();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
if (missing) {
|
|
50
|
+
res.statusCode = 404;
|
|
51
|
+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
52
|
+
res.end('No MCP server: writedocs.json has "mcp": false.');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
res.statusCode = response.status;
|
|
56
|
+
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
57
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
58
|
+
} catch (err) {
|
|
59
|
+
next(err);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
package/src/lib/mcp-index.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
// mcp-index.json - everything the site's MCP server (src/mcp/server.js)
|
|
2
|
-
// answers from: every page an agent may read, as it should read it. Built
|
|
3
|
-
// from the same pipeline as llms-full.txt (markdownForAgents(): snippets
|
|
4
|
-
// inlined, `variables` filled in, <Visibility> for agents), in navigation
|
|
5
|
-
// order, with each page's real URL and the version/language it sits under in
|
|
6
|
-
// the navigation - what search_docs filters on.
|
|
7
|
-
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
8
|
-
import fs from 'node:fs';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import {
|
|
11
|
-
loadDocsConfig,
|
|
12
|
-
normalizeEntryId,
|
|
13
|
-
findAllPages,
|
|
14
|
-
resolveSiteRoot,
|
|
15
|
-
fileIdForEntry,
|
|
16
|
-
resolveSections,
|
|
17
|
-
resolveLandings,
|
|
18
|
-
flattenNav,
|
|
19
|
-
} from './config';
|
|
20
|
-
import { pageMarkdown } from './openapi-markdown';
|
|
21
|
-
import { orderByNavigation } from './llms.js';
|
|
22
|
-
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
23
|
-
|
|
24
|
-
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
25
|
-
|
|
26
|
-
export interface McpDoc {
|
|
27
|
-
path: string;
|
|
28
|
-
url: string;
|
|
29
|
-
title: string;
|
|
30
|
-
description: string;
|
|
31
|
-
content: string;
|
|
32
|
-
version: string;
|
|
33
|
-
language: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface McpIndex {
|
|
37
|
-
meta: { name: string; description: string; siteUrl: string; generator: 'writedocs' };
|
|
38
|
-
docs: McpDoc[];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function mcpIndex(): Promise<McpIndex> {
|
|
42
|
-
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
43
|
-
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
44
|
-
const config = loadDocsConfig(contentDir);
|
|
45
|
-
// {domain}{basePath} - or the base alone ("/docs", or "") with no `domain`.
|
|
46
|
-
const siteUrl = resolveSiteRoot(config);
|
|
47
|
-
|
|
48
|
-
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
49
|
-
const hasPages = findAllPages(contentDir).length > 0;
|
|
50
|
-
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
51
|
-
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
52
|
-
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
53
|
-
]);
|
|
54
|
-
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
55
|
-
|
|
56
|
-
// The version and language each page sits under, from its section's path
|
|
57
|
-
// through the navigation (tabs > versions > languages > ...).
|
|
58
|
-
// An item's own page that no sidebar lists sits under that item.
|
|
59
|
-
const sections = resolveSections(config.navigation);
|
|
60
|
-
const landings = resolveLandings(config.navigation);
|
|
61
|
-
const placeOf = (fileId: string) => {
|
|
62
|
-
const section =
|
|
63
|
-
sections.find((s) => flattenNav(s.pages).some((e) => e.slug === fileId)) ??
|
|
64
|
-
landings.find((landing) => landing.slug === fileId);
|
|
65
|
-
const place = { version: '', language: '' };
|
|
66
|
-
for (const segment of section?.path ?? []) {
|
|
67
|
-
const item = segment.items[segment.index] as { version?: string; language?: string };
|
|
68
|
-
if (segment.kind === 'version' && item.version) place.version = item.version;
|
|
69
|
-
if (segment.kind === 'language' && item.language) place.language = item.language;
|
|
70
|
-
}
|
|
71
|
-
return place;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const docs = entries
|
|
75
|
-
// Same pages llms.txt lists: not `noindex`, not a frontmatter `url`
|
|
76
|
-
// (an external link, with no content of its own).
|
|
77
|
-
.filter((entry) => !entry.data.seo?.noindex && !entry.data.url)
|
|
78
|
-
.map((entry) => {
|
|
79
|
-
const slug = normalizeEntryId(entry.id);
|
|
80
|
-
const fileId = fileIdForEntry(contentDir, packageRoot, entry);
|
|
81
|
-
// An API page's source is a near-empty stub - its operation, written
|
|
82
|
-
// out from the spec, follows its own text (lib/openapi-markdown.ts).
|
|
83
|
-
const content = pageMarkdown(entry, { contentDir, packageRoot, variables: config.variables, siteUrl: siteUrl || null });
|
|
84
|
-
return {
|
|
85
|
-
slug,
|
|
86
|
-
fileId,
|
|
87
|
-
doc: {
|
|
88
|
-
path: fileId,
|
|
89
|
-
url: `${siteUrl}${slug === 'index' ? '/' : `/${slug}/`}`,
|
|
90
|
-
title: entry.data.title,
|
|
91
|
-
description: entry.data.description ?? '',
|
|
92
|
-
content,
|
|
93
|
-
...placeOf(fileId),
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
meta: { name: config.name, description: config.description ?? '', siteUrl, generator: 'writedocs' },
|
|
100
|
-
docs: orderByNavigation(docs, config.navigation).map((d) => d.doc),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
1
|
+
// mcp-index.json - everything the site's MCP server (src/mcp/server.js)
|
|
2
|
+
// answers from: every page an agent may read, as it should read it. Built
|
|
3
|
+
// from the same pipeline as llms-full.txt (markdownForAgents(): snippets
|
|
4
|
+
// inlined, `variables` filled in, <Visibility> for agents), in navigation
|
|
5
|
+
// order, with each page's real URL and the version/language it sits under in
|
|
6
|
+
// the navigation - what search_docs filters on.
|
|
7
|
+
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import {
|
|
11
|
+
loadDocsConfig,
|
|
12
|
+
normalizeEntryId,
|
|
13
|
+
findAllPages,
|
|
14
|
+
resolveSiteRoot,
|
|
15
|
+
fileIdForEntry,
|
|
16
|
+
resolveSections,
|
|
17
|
+
resolveLandings,
|
|
18
|
+
flattenNav,
|
|
19
|
+
} from './config';
|
|
20
|
+
import { pageMarkdown } from './openapi-markdown';
|
|
21
|
+
import { orderByNavigation } from './llms.js';
|
|
22
|
+
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
23
|
+
|
|
24
|
+
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
25
|
+
|
|
26
|
+
export interface McpDoc {
|
|
27
|
+
path: string;
|
|
28
|
+
url: string;
|
|
29
|
+
title: string;
|
|
30
|
+
description: string;
|
|
31
|
+
content: string;
|
|
32
|
+
version: string;
|
|
33
|
+
language: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface McpIndex {
|
|
37
|
+
meta: { name: string; description: string; siteUrl: string; generator: 'writedocs' };
|
|
38
|
+
docs: McpDoc[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function mcpIndex(): Promise<McpIndex> {
|
|
42
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
43
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
44
|
+
const config = loadDocsConfig(contentDir);
|
|
45
|
+
// {domain}{basePath} - or the base alone ("/docs", or "") with no `domain`.
|
|
46
|
+
const siteUrl = resolveSiteRoot(config);
|
|
47
|
+
|
|
48
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
49
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
50
|
+
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
51
|
+
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
52
|
+
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
53
|
+
]);
|
|
54
|
+
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
55
|
+
|
|
56
|
+
// The version and language each page sits under, from its section's path
|
|
57
|
+
// through the navigation (tabs > versions > languages > ...).
|
|
58
|
+
// An item's own page that no sidebar lists sits under that item.
|
|
59
|
+
const sections = resolveSections(config.navigation);
|
|
60
|
+
const landings = resolveLandings(config.navigation);
|
|
61
|
+
const placeOf = (fileId: string) => {
|
|
62
|
+
const section =
|
|
63
|
+
sections.find((s) => flattenNav(s.pages).some((e) => e.slug === fileId)) ??
|
|
64
|
+
landings.find((landing) => landing.slug === fileId);
|
|
65
|
+
const place = { version: '', language: '' };
|
|
66
|
+
for (const segment of section?.path ?? []) {
|
|
67
|
+
const item = segment.items[segment.index] as { version?: string; language?: string };
|
|
68
|
+
if (segment.kind === 'version' && item.version) place.version = item.version;
|
|
69
|
+
if (segment.kind === 'language' && item.language) place.language = item.language;
|
|
70
|
+
}
|
|
71
|
+
return place;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const docs = entries
|
|
75
|
+
// Same pages llms.txt lists: not `noindex`, not a frontmatter `url`
|
|
76
|
+
// (an external link, with no content of its own).
|
|
77
|
+
.filter((entry) => !entry.data.seo?.noindex && !entry.data.url)
|
|
78
|
+
.map((entry) => {
|
|
79
|
+
const slug = normalizeEntryId(entry.id);
|
|
80
|
+
const fileId = fileIdForEntry(contentDir, packageRoot, entry);
|
|
81
|
+
// An API page's source is a near-empty stub - its operation, written
|
|
82
|
+
// out from the spec, follows its own text (lib/openapi-markdown.ts).
|
|
83
|
+
const content = pageMarkdown(entry, { contentDir, packageRoot, variables: config.variables, siteUrl: siteUrl || null });
|
|
84
|
+
return {
|
|
85
|
+
slug,
|
|
86
|
+
fileId,
|
|
87
|
+
doc: {
|
|
88
|
+
path: fileId,
|
|
89
|
+
url: `${siteUrl}${slug === 'index' ? '/' : `/${slug}/`}`,
|
|
90
|
+
title: entry.data.title,
|
|
91
|
+
description: entry.data.description ?? '',
|
|
92
|
+
content,
|
|
93
|
+
...placeOf(fileId),
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
meta: { name: config.name, description: config.description ?? '', siteUrl, generator: 'writedocs' },
|
|
100
|
+
docs: orderByNavigation(docs, config.navigation).map((d) => d.doc),
|
|
101
|
+
};
|
|
102
|
+
}
|